Below I will share with you a method for implementing Vue custom instructions to implement the checkbox selection function. It has a good reference value and I hope it will be helpful to everyone.
A project I recently worked on needed to use Vue to implement the select-all function. I looked at the online practices and found that the reusability of using attribute calculations was not high, so I chose custom instructions. However, most of the online practices are There will be certain format requirements for the original data, and no results will be returned, so improvements were made.
The above code:
Usually we have to get a certain key value in the original data, which can be found in Fill in "key" with the key value you want to obtain, and "result" is the selected item.
Sometimes, we need to return a complete object and modify the code. When the key is not entered, a complete object array is returned.
this.setValue=()=>{ let result=[] this.vm[this.params.checkData].forEach((item) => { //删除checked属性 let temp={}; (()=>delete Object.assign(temp,item).checked)(); item.checked?result.push(item[this.params.key]||temp):""; }); this.vm[this.params.result]=result }
But when, At this time, the object in the returned array does not have the same reference address as the original data. When the array.$remove() function needs to be used, it will fail. A new relative parameter is added, and the user can customize the returned data. Whether it is associated with the original data
this.setValue = () => { let result = [] this.vm[this.params.checkData].forEach((item) => { if(this.params.relative) { item.checked ? result.push(item) : ""; }else{ //删除checked属性 let temp = {}; (() => delete Object.assign(temp, item).checked)(); item.checked ? result.push(item[this.params.key] || temp) : ""; } }); this.vm[this.params.result] = result }
When the data length is greater than 2, it needs to be judged 2N times, which consumes a lot of performance. Optimize it:
'check-all', { twoWay: true, params: ['checkData', 'result', 'key','relative'], /*checkData:列表数据, result:返回的结果, key:列表数据中需要返回的字段, relative:是否返回与列表数据相同引用地址的选中结果*/ bind() { /*提取被选中的项*/ this.setValue = () => { let result = [] if (this.params.relative) { this.vm[this.params.checkData].forEach((item) => { item.checked ? result.push(item) : ""; }); } else { this.vm[this.params.checkData].forEach((item) => { //删除checked属性 let temp = {}; (() => delete Object.assign(temp, item).checked)(); item.checked ? result.push(item[this.params.key] || temp) : ""; }); } this.vm[this.params.result] = result }; /*为原始数据的每一个对象添加一个checked属性*/ this.addChecked = () => { this.vm[this.params.checkData].forEach((item) => { Vue.set(item, 'checked', false) }); }; /*如果所有的列表的checked属性都为true,则选中全选框,否则不选中全选框 */ this.vm.$watch(this.params.checkData, (data) => { if(!data.length) return; data.every((item) => item.checked) ? this.set(true) : this.set(false); this.setValue() }, {deep: true}); //当列表发生变化时重新绑定 this.vm.$watch(this.params.checkData, (data) => { if(!data.length) return this.addChecked(); }); }, // checkAll发生更改时 update(checkAll) { /*如果全选框被选中,则将列表的所有checked属性转为true,否则转为false */ checkAll ? this.vm[this.params.checkData].forEach((item) => { item.checked = true }) : this.vm[this.params.checkData].forEach((item) => { item.checked = false }); this.setValue() }, }
At this time, you only need to judge N 1 times.
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
p5.js introductory tutorial - Example of mouse interaction
Four ways to implement two-way binding with Vue A method
Vue-Routing navigation menu bar highlight setting method
The above is the detailed content of How to operate custom instructions in Vue to implement checkbox selection function. For more information, please follow other related articles on the PHP Chinese website!