//全选
data: function() {
return {
selectItems: [], // 从服务器拿到的数据
}
},
computed: {
// 全选checkbox绑定的model
selectAll: {
get: function() {
return this.selectCount == this.selectItems.length;
},
set: function(value) {
this.selectItems.forEach(function(item) {
item.checked = value;
});
return value;
}
},
//选中的数量
selectCount: {
get: function() {
var i = 0;
this.selectItems.forEach(function(item) {
if (item.checked) {
i++;
}
});
return i;
}
},
//选中的数组
checkedGroups: {
get: function() {
var checkedGroups = [];
this.selectItems.forEach(function(item) {
if (item.checked) {
checkedGroups.push(item);
}
});
return checkedGroups;
}
}
}
export default {
'check-all': {
twoWay: true,
params: ['checkData'],
bind() {
/**
- 如果所有的列表的checked属性都为true,则选中全选框,否则不选中全选框
*/
this.vm.$watch(this.params.checkData, (checkData) => {
if (checkData.every((item) => item.checked)) {
this.set(true);
} else {
this.set(false);
}
}, { deep: true });
},
// checkAll发生更改时
update(checkAll) {
/**
- 如果全选框被选中,则将列表的所有checked属性转为true,否则转为false
*/
if (checkAll) {
this.vm[this.params.checkData].forEach((item) => {
item.checked = true;
});
} else {
this.vm[this.params.checkData].forEach((item) => {
item.checked = false;
});
}
},
},
};
<input type="checkbox" v-model="checkAll" v-check-all="checkAll" check-data="checkData">
<ul>
<li v-for="item in checkData">
<input type="checkbox" v-model="item.checked">
{{item.text}}
</li>
</ul>
Directive.prototype._setupParamWatcher = function (key, expression) {
var self = this;
var called = false;
var unwatch = (this._scope || this.vm).$watch(expression, function (val, oldVal) {
self.params[key] = val;
// since we are in immediate mode,
// only call the param change callbacks if this is not the first update.
if (called) {
var cb = self.paramWatchers && self.paramWatchers[key];
if (cb) {
cb.call(self, val, oldVal);
}
} else {
called = true;
}
}, {
immediate: true,
user: false
});(this._paramUnwatchFns || (this._paramUnwatchFns = [])).push(unwatch);
};
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有