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

Vue implements sample code to select all and none_vue.js

不言
Release: 2018-03-31 16:38:43
Original
1870 people have browsed it

This article mainly introduces the sample code for implementing vue to select all and none. Now I share it with you and give you a reference. Let’s take a look together

The select all function can be said to be a very common function in front-end development. In the past, jQuery was mostly used in project development. Recently I was refactoring my previous project using the vue front-end framework. The transition from jQuery to Vue is mainly a change of thinking. It is to transform the original idea of ​​directly operating DOM into operating data. Using data to drive DOM is also a core idea of ​​the Vue framework. The change of thinking will lead to the realization of functions. Naturally easier to understand.

For example, in the simple demo below


If you do it according to the jQuery idea, you need to select the select all checkbox and All checkbox items register selected events respectively, determine the selected status to set the corresponding status for the relevant checkbox, which involves a lot of DOM operations.

Let’s take a look at the idea of ​​​​vue data-driven dom to achieve this function.

vue data-driven dom implementation function


Copy after login


Using vue’s two-way data binding v-model command, when checked, the value of the checkbox will be automatically pushed to the bound array checkData, which saves a lot of operations on the DOM.

If it is a fixed option, this can be achieved, but this method has some disadvantages. Two-way binding of array data is hard-coded and not very flexible. If the checkbox option is added, the wach needs to be changed. Determine the length of the bound array.

Sometimes the checkbox option is dynamically obtained from the background, which makes it more flexible.

For example, the background data is like this:


  ajaxData: [{
    name: 'a',
    value: 'apple'
  },{
    name: 'b',
    value: 'banana'
  },{
    name: 'c',
    value: 'orange'
  }]
Copy after login


You need to dynamically render the checkbox option first , performing data binding.


Copy after login



## method is not the optimal way to write , there are also some disadvantages. You are welcome to give us some advice and discuss them together.

github address: https://github.com/zhangqian00/

Related recommendations:

laravel5.3 vue implements the favorites function

Vue implements a cool menu plug-in detailed explanation



##

The above is the detailed content of Vue implements sample code to select all and none_vue.js. 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!