Home >Web Front-end >JS Tutorial >How does vue implement the function of single selection, multiple selection, inverse selection of all, and no selection of all (with code)

How does vue implement the function of single selection, multiple selection, inverse selection of all, and no selection of all (with code)

不言
不言Original
2018-09-08 17:50:087926browse

The content of this article is about how vue implements the single-select, multiple-select, inverse-select, and select-all functions (with code attached). It has certain reference value. Friends in need can refer to it. I hope It will help you.

Radio selection

When we use v-for to render a set of data, we can bring index to distinguish them. We use this index here to simply implement radio selection

0bd6836808e453d7044797e79f4db189{{item}}bed06894275b65c1ab86501b08a632eb

The first choice is to define a selectedNum. When we click on the item, we change the selectedNum to the index of the item we clicked, and then add a judgment to each item to determine whether the selectedNum is equal to itself. If it is equal, it is selected.
Equivalent to each person having a number. Clicking the mouse generates a winning number, and then each person can judge whether the winning number is his or her own. If it is his or her own, wow, that’s incredible~

The code is as follows:

data() {
    return {
      selectedNum:"",
      radioList: ["某个元素", "某个元素", "某个元素", "某个元素", "某个元素"],
    };
  },
methods: {
    //单选
    select(i) {
      this.selectedNum = i;
    },
  }

Multiple selection

The principle of multi-selection is the same as that of single selection, except that this time we want to maintain an array, not a single selectedNum

<li v-for="(item,index) in checkboxList" :key="item" :class="checkbox.includes(index)?&#39;active&#39;:&#39;&#39;" @click="check(index)">{{item}}</li>

The same is used index~ The same person is selected for the prize, but this time there are many winners. We click once and one person will win. If this person's index appears on our winner list checkbox, then he is the chosen one~
The code is written by clicking once to push and once to index into the checkbox. If there is an index in the checkbox, then do not need it. This achieves clicking once to select and then clicking again to cancel.

//多选
    check(i){
      var idx = this.checkbox.indexOf(i);
      //如果已经选中了,那就取消选中,如果没有,则选中
      if(idx>-1){
        this.checkbox.splice(idx,1);
      }else{
        this.checkbox.push(i);
      }
    },

Next, let’s write about the implementation of selecting all, canceling all, and inverse selection.

//选中全部
checkAll(){
    //中奖的人就这么多,而且他们的index都是0到length-1的(v-for渲染),一顿数组基本操作即可
    var len = this.checkboxList.length;
    this.checkbox = [];
    for(var i=0;i311cd1cb4c7f915bb77f171abfb1a2fc-1){
        this.checkbox.splice(idx,1);
      }else{
        this.checkbox.push(i);
      }
    }
  }

Many times, only one button is needed to select all and cancel all, so we need to judge whether it has been fully selected. Automatically calculate whether to select all in computed.

c93d9d746228ad54d582b9b52a1546f4{{isCheckAll?'取消全选':'选择全部'}}65281c5ac262bf6d81768915a4a77ac0

computed: {
    //判断是否全部选中
    isCheckAll(){
      if(this.checkbox.length==this.checkboxList.length){
        return true;
      }
      return false;
    }
  },

Then we only need to bind such a function to this dual-function button

letsGetThisFuckingCheck(){
//如果全选,就是清空选择;如果不是,那就全都安排一下
      if(this.isCheckAll){
        this.clearCheckbox();
      }else{
        this.checkAll()
      }
    },

Complete code

d477f9ce7bf77f53fbcf36bec1b69b7a
  dc6dce4a544fdca2df29d5ac0ea9906b
    e388a4556c0f65e1904146cc1a846bee单选框94b3e26ee717c64999d7867364b1b4a3
    ff6d136ddc5fdfeffaf53ff6ee95f185
      0bd6836808e453d7044797e79f4db189{{item}}bed06894275b65c1ab86501b08a632eb
    929d1f5ca49e04fdcb27f9465b944689

    e388a4556c0f65e1904146cc1a846bee多选框94b3e26ee717c64999d7867364b1b4a3
    ff6d136ddc5fdfeffaf53ff6ee95f185
      37d349a997ba060025b794f498115081{{item}}bed06894275b65c1ab86501b08a632eb
    929d1f5ca49e04fdcb27f9465b944689
    c93d9d746228ad54d582b9b52a1546f4{{isCheckAll?'取消全选':'选择全部'}}65281c5ac262bf6d81768915a4a77ac0
    79162f310a08887208df5d3e33f513a3反选65281c5ac262bf6d81768915a4a77ac0
  16b28748ea4df4d9c2150843fecfba68
21c97d3a051048b8e55e3c8f199a54b2

3f1c4e4b6b16bbbd69b2ee476dc4f83a
export default {
  components: {},

  data() {
    return {
      selectedNum:"",
      radioList: ["某个元素", "某个元素", "某个元素", "某个元素", "某个元素"],
      checkbox:[],
      checkboxList:["某个元素", "某个元素", "某个元素", "某个元素", "某个元素","某个元素", "某个元素"],
    };
  },

  computed: {
    //判断是否全部选中
    isCheckAll(){
      if(this.checkbox.length==this.checkboxList.length){
        return true;
      }
      return false;
    }
  },

  methods: {
    //单选
    select(i) {
      this.selectedNum = i;
    },
    //多选
    check(i){
      var idx = this.checkbox.indexOf(i);
      //如果已经选中了,那就取消选中,如果没有,则选中
      if(idx>-1){
        this.checkbox.splice(idx,1);
      }else{
        this.checkbox.push(i);
      }
    },
    
    letsGetThisFuckingCheck(){
      if(this.isCheckAll){
        this.clearCheckbox();
      }else{
        this.checkAll()
      }
    },
    //选中全部
    checkAll(){
      var len = this.checkboxList.length;
      this.checkbox = [];
      for(var i=0;i363dfbc6f5fc95588c3de13971e480d8-1){
          this.checkbox.splice(idx,1);
        }else{
          this.checkbox.push(i);
        }
      }
    }
  }
};
2cacc6d41bbb37262a98f745aa00fbf0
c977fa5678fe78cf54b097005108eb8c
li{
  display: inline-block;
  font-size: 16px;
  padding: 5px;
  background-color: #e6e6e6;
  margin: 5px 10px;
  cursor: pointer;
}
.active {
  border: 2px solid red;
}
531ac245ce3e4fe3d50054a55f265927

Related recommendations:

Use vue to implement the select all and inverse selection function

## Based on jQuery to implement the all selection, none selection and inverse selection function of the check box_jquery

The above is the detailed content of How does vue implement the function of single selection, multiple selection, inverse selection of all, and no selection of all (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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