Home > Web Front-end > JS Tutorial > body text

How to operate custom instructions in Vue to implement checkbox selection function

亚连
Release: 2018-06-02 17:26:10
Original
2323 people have browsed it

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:

<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title></title>
 </head>
 <body>
  <p id="app">
  <input type="checkbox" v-model="allCheck" v-check-all="allCheck" check-data="list" result="customerResult" key="demo"> 全选
  <ul> 
   <li v-for="item in list"> 
    <input type="checkbox" v-model="item.checked"> 
    {{item.demo}}
   </li> 
  </ul> 
  <p >
   customerResult: {{customerResult}}
  </p>
  </p>
  <script src="vue.js"></script>
  <script>
   var vm = new Vue({
    el: "#app",
    data:function(){
     return {
      list:[{demo:1},
      {demo:2},
      {demo:3}],
      allCheck:&#39;&#39;,
      customerResult:&#39;&#39;,
     }
    },
    directives: {
     &#39;check-all&#39;: {
      twoWay: true,
      params: [&#39;checkData&#39;,&#39;result&#39;,&#39;key&#39;],
      bind() {
       /*为原始数据的每一个对象添加一个checked属性*/
       this.vm[this.params.checkData].forEach((item)=>{
        Vue.set(item,&#39;checked&#39;,false)
       });
       /*提取被选中的项*/
       this.setValue=function(){
        let result=[]
        this.vm[this.params.checkData].forEach((item) => {
         if(item.checked){
          result.push(item[this.params.key])
         }
        });
        this.vm[this.params.result]=result
       }
        /*如果所有的列表的checked属性都为true,则选中全选框,否则不选中全选框 */
       this.vm.$watch(this.params.checkData, (data) => {
        if(data.every((item) => item.checked)) {
         this.set(true);
        } else {
         this.set(false);
        }
        this.setValue()
       }, {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;
        });
       }
       this.setValue()
      },
     },
    }
   })
  </script>
 </body>
</html>
Copy after login

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
}
Copy after login

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
}
Copy after login

When the data length is greater than 2, it needs to be judged 2N times, which consumes a lot of performance. Optimize it:

&#39;check-all&#39;, {
  twoWay: true,
  params: [&#39;checkData&#39;, &#39;result&#39;, &#39;key&#39;,&#39;relative&#39;],
  /*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, &#39;checked&#39;, 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()
  },
 }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!