vue2.0如何实现一个一行四列的数据,已知总数和每列的数量,如何计算出每列所占每行总数的整数比,并且可以设置最大比例和最小比例
时间: 2024-02-06 11:13:01 浏览: 159
可以使用计算属性和watch来计算出每列所占每行总数的整数比,并且可以设置最大比例和最小比例。具体实现步骤如下:
1. 在data中定义总数、每列的数量、最大比例和最小比例:
```
data() {
return {
total: 20,
columnCount: 4,
maxRatio: 50, // 最大比例
minRatio: 10, // 最小比例
}
}
```
2. 在计算属性中计算每列所占每行总数的整数比:
```
computed: {
columnRatio() {
const { total, columnCount, maxRatio, minRatio } = this;
const maxTotal = maxRatio * columnCount;
const minTotal = minRatio * columnCount;
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;
}
}
}
```
3. 在watch中监测最大比例和最小比例的变化,更新数据:
```
watch: {
maxRatio(newVal) {
if (newVal < this.minRatio) {
this.maxRatio = this.minRatio;
return;
}
this.updateColumnRatio();
},
minRatio(newVal) {
if (newVal > this.maxRatio) {
this.minRatio = this.maxRatio;
return;
}
this.updateColumnRatio();
}
},
methods: {
updateColumnRatio() {
const ratios = this.columnRatio;
this.columnRatio = ratios.map(val =>
Math.max(Math.min(val, this.maxRatio), this.minRatio)
);
}
}
```
4. 在模板中使用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,
}
},
computed: {
columnRatio() {
const { total, columnCount, maxRatio, minRatio } = this;
const maxTotal = maxRatio * columnCount;
const minTotal = minRatio * columnCount;
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;
}
}
},
watch: {
maxRatio(newVal) {
if (newVal < this.minRatio) {
this.maxRatio = this.minRatio;
return;
}
this.updateColumnRatio();
},
minRatio(newVal) {
if (newVal > this.maxRatio) {
this.minRatio = this.maxRatio;
return;
}
this.updateColumnRatio();
}
},
methods: {
updateColumnRatio() {
const ratios = this.columnRatio;
this.columnRatio = ratios.map(val =>
Math.max(Math.min(val, this.maxRatio), this.minRatio)
);
}
}
}
</script>
<style>
.container {
display: flex;
width: 100%;
height: 100%;
}
.column {
display: flex;
justify-content: center;
align-items: center;
border: 1px solid #ccc;
}
</style>
```
阅读全文