Home > Article > Web Front-end > How to use the select all command of Vue.js to implement the select all operation of multiple selection boxes
In Vue.js development, it is often necessary to handle the select-all operation of multi-select boxes. You can easily implement the single-select and multi-select functions of multi-select boxes using the v-model directive of Vue.js. However, to implement the select-all function of the multi-select box, you need to use Vue.js's custom instructions. This article will introduce how to use the select all command of Vue.js to implement the select all operation of multi-select boxes.
First, define a select-all directive in the Vue.js instance, named v-check-all:
Vue.directive('check-all', { bind: function(el, binding) { el.checked = binding.value; el.indeterminate = true; // 中间状态 }, update: function(el, binding) { el.checked = binding.value; } });
This command is bound to the multi-select box element. It has two life cycle functions, namely bind and update.
In the bind life cycle function, we set the checked status of the multi-select box element to the value of the instruction binding, and set the indeterminate attribute to true. The indeterminate attribute indicates that the multi-select box element is in an intermediate state, that is, it is neither a selected state nor an unselected state. This is to achieve the partially selected state in the select all function.
In the update life cycle function, we listen to the value bound to the instruction. Once the value of the instruction changes, we set the checked status of the multi-select box element to that value. In this way, the select-all function is implemented.
Next, use the v-check-all command in the multi-select box list that requires the select all function to achieve the select all operation. As shown in the following code:
<div id="app"> <input type="checkbox" v-model="allChecked" v-check-all="allChecked"> <span>全选/取消全选</span> <ul> <li v-for="(item, index) in list" :key="index"> <input type="checkbox" v-model="item.checked"> <span>{{ item.name }}</span> </li> </ul> </div>
In the above example, we use the v-model directive to bind the checked state of the full selection box to the allChecked attribute of the Vue.js instance. When the all-select box is selected, the value of the allChecked attribute is true, otherwise it is false.
Then, in the ul tag, we use the v-for directive to iterate through each element in the list. Each list item element contains a checkbox and a name.
Use the v-model directive on the multi-select box to bind the checked status of each element to the checked attribute of the element. When the element is selected, the value of the checked attribute is true, otherwise it is false.
Finally, use the v-check-all instruction on the all-select box and set the value bound to the instruction to the allChecked attribute to achieve the all-select function.
In the above example, we used the indeterminate attribute to achieve the partially selected state. When all the multi-select boxes in the list are selected, the all-select box becomes selected; when one or more multi-select boxes in the list are unselected, the all-select box becomes unselected. When some of the multi-select boxes in the list are selected, the all-select box is in the middle state.
In order to achieve the partially selected state, we also need to define a calculated property in the Vue.js instance to determine whether the number of multi-select boxes in the list is equal to the number selected. If they are equal, it means that all the multi-select boxes are selected, and the all-select boxes should be selected at this time; if the number of selected items is 0, it means that all the multi-select boxes are unselected, and the all-select boxes should be unselected at this time. state; otherwise, the all-select box is in the middle state.
computed: { allChecked: { get: function() { return this.list.every(function(item) { return item.checked }); }, set: function(value) { this.list.forEach(function(item) { item.checked = value; }); } } }
In the above example, we use the calculated property of Vue.js to implement all selection and define an allChecked calculated property. In the get method, we iterate through each element in the list. Once the checked attribute of an element is false, we return false, indicating that not all multi-select boxes are selected. Otherwise, returns true, indicating that all checkboxes are selected. In the set method, we set the checked attribute of each element in the list to the specified value, that is, to achieve the function of selecting all and deselecting all.
In general, it is a very convenient way to use custom instructions to implement the all-select operation of multi-select boxes. Through the above examples, I believe readers have mastered the use of Vue.js select all instructions.
The above is the detailed content of How to use the select all command of Vue.js to implement the select all operation of multiple selection boxes. For more information, please follow other related articles on the PHP Chinese website!