vue2.0如何实现一个一行四列的数据,已知总数和每列的数量,如何计算出每列所占每行总数的整数比,并且计算出来的比例要在最大值和最小值的范围之内
时间: 2024-02-06 13:13:02 浏览: 134
可以使用计算属性和watch来计算出每列所占每行总数的整数比,并且计算出来的比例要在最大值和最小值的范围之内。具体实现步骤如下:
1. 在data中定义总数、每列的数量、最大比例和最小比例的范围:
```
data() {
return {
total: 20,
columnCount: 4,
maxRatio: 50, // 最大比例
minRatio: 10, // 最小比例
maxTotal: 0, // 最大值
minTotal: 0, // 最小值
}
}
```
2. 在mounted钩子函数中计算最大值和最小值:
```
mounted() {
this.updateMinMaxTotal();
}
```
```
methods: {
updateMinMaxTotal() {
const { total, columnCount, maxRatio, minRatio } = this;
this.maxTotal = maxRatio * columnCount;
this.minTotal = minRatio * columnCount;
if (total > this.maxTotal) {
this.total = this.maxTotal;
} else if (total < this.minTotal) {
this.total = this.minTotal;
}
}
}
```
3. 在计算属性中计算每列所占每行总数的整数比:
```
computed: {
columnRatio() {
const { total, columnCount, maxRatio, minRatio, maxTotal, minTotal } = this;
const columnTotal = Math.floor(total / columnCount);
const remainder = total % columnCount;
const ratios = Array(columnCount).fill(columnTotal);
for (let i = 0; i < remainder; i++) {
ratios[i]++;
}
const totalRatio = ratios.reduce((prev, curr) => prev + curr, 0);
if (totalRatio > maxTotal) {
const ratio = maxTotal / totalRatio;
return ratios.map(val => Math.floor(val * ratio));
} else if (totalRatio < minTotal) {
const ratio = minTotal / totalRatio;
return ratios.map(val => Math.floor(val * ratio));
} else {
return ratios.map(val =>
Math.max(Math.min(val, maxRatio), minRatio)
);
}
}
}
```
4. 在watch中监测总数、最大比例和最小比例的变化,更新数据:
```
watch: {
total(newVal) {
if (newVal > this.maxTotal) {
this.total = this.maxTotal;
return;
} else if (newVal < this.minTotal) {
this.total = this.minTotal;
return;
}
this.updateColumnRatio();
},
maxRatio(newVal) {
if (newVal < this.minRatio) {
this.maxRatio = this.minRatio;
return;
}
this.updateMinMaxTotal();
this.updateColumnRatio();
},
minRatio(newVal) {
if (newVal > this.maxRatio) {
this.minRatio = this.maxRatio;
return;
}
this.updateMinMaxTotal();
this.updateColumnRatio();
}
},
methods: {
updateColumnRatio() {
const ratios = this.columnRatio;
this.columnRatio = ratios.map(val =>
Math.max(Math.min(val, this.maxRatio), this.minRatio)
);
}
}
```
5. 在模板中使用v-for循环渲染数据,并将每列所占每行总数的整数比作为样式flex-basis的值:
```
<template>
<div class="container">
<div v-for="(ratio, index) in columnRatio"
:key="index"
class="column"
:style="{ flexBasis: ratio + '%' }">
{{ ratio }}
</div>
</div>
</template>
```
完整代码示例如下:
```
<template>
<div class="container">
<div v-for="(ratio, index) in columnRatio"
:key="index"
class="column"
:style="{ flexBasis: ratio + '%' }">
{{ ratio }}
</div>
</div>
</template>
<script>
export default {
data() {
return {
total: 20,
columnCount: 4,
maxRatio: 50,
minRatio: 10,
maxTotal: 0,
minTotal: 0,
}
},
mounted() {
this.updateMinMaxTotal();
},
computed: {
columnRatio() {
const { total, columnCount, maxRatio, minRatio, maxTotal, minTotal } = this;
const columnTotal = Math.floor(total / columnCount);
const remainder = total % columnCount;
const ratios = Array(columnCount).fill(columnTotal);
for (let i = 0; i < remainder; i++) {
ratios[i]++;
}
const totalRatio = ratios.reduce((prev, curr) => prev + curr, 0);
if (totalRatio > maxTotal) {
const ratio = maxTotal / totalRatio;
return ratios.map(val => Math.floor(val * ratio));
} else if (totalRatio < minTotal) {
const ratio = minTotal / totalRatio;
return ratios.map(val => Math.floor(val * ratio));
} else {
return ratios.map(val =>
Math.max(Math.min(val, maxRatio), minRatio)
);
}
}
},
watch: {
total(newVal) {
if (newVal > this.maxTotal) {
this.total = this.maxTotal;
return;
} else if (newVal < this.minTotal) {
this.total = this.minTotal;
return;
}
this.updateColumnRatio();
},
maxRatio(newVal) {
if (newVal < this.minRatio) {
this.maxRatio = this.minRatio;
return;
}
this.updateMinMaxTotal();
this.updateColumnRatio();
},
minRatio(newVal) {
if (newVal > this.maxRatio) {
this.minRatio = this.maxRatio;
return;
}
this.updateMinMaxTotal();
this.update
阅读全文