Skip to content
Advertisement

VUE component for Select all option

In my application I have multiple tables on same page (also multiple pages). Each table has “Select all” option and checkboxes in each row. Code reuse would be really helpfull but I cant get this working. Currently I have following methods, but I always get Error in render: “TypeError: Cannot read property ‘includes’ of undefined”. Right now this is code is inside one component, but should be available to another one. How can I properly extra this to standalone component and then use it in others?

In mounted method there is a field selected : {}.

Vue HTML template:

JavaScript

Vue methods:

JavaScript

Advertisement

Answer

Your code expects this.selected to be populated with each group before isSelected() is called. You’ll need to add logic into that method to check if this.selected[group] exists, and add it if not.

Also, Vue already offers most of the functionality you are writing, for example this should take care of the logic for toggling each checkbox:

JavaScript

(Ref: https://v2.vuejs.org/v2/guide/forms.html#Checkbox)

For the “toggle all” functionality, you could create a helper method like this in a separate file, and then import it as needed:

JavaScript

You would use it like this inside the component:

JavaScript

However, for this to work, first you would need to make sure this.selected already contains every group name that it needs, as I mentioned earlier.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement