需求是这样的:
这里有20个任务需要将其平均分配给6个人员,现在点击平均分配按钮实现的效果如下图所示
希望实现的效果:将多余的两条分给最开始人员
已有代码如下
countAvgNum() {
if (this.distribute == null || this.distribute.length == 0) {
this.$message.warning("请添加分配对象");
return;
}
// 防止缓存不变
this.distribute.forEach(item => {
item.disNum = 0;
});
let total = this.remainNum,
dataLength = this.distribute.length;
//当分配的数量大于总数的时候
if (total < dataLength) {
// 前面几个都是1,后面的是0
var num = total;
this.distribute.forEach(item => {
if (num > 0) {
item.disNum = 1;
num--;
} else {
item.disNum = 0;
}
});
} else {
this.avgNum = parseInt(total / dataLength)
? parseInt(total / dataLength)
: 0;
this.distribute = this.distribute.map(value => {
value.disNum = this.avgNum;
return value;
});
// this.distribute[dataLength - 1].disNum =
// total - this.avgNum * (dataLength - 1);
}
},