Home >Web Front-end >Front-end Q&A >How to get the value of custom table vue
How to get the value of a custom table vue
In Vue.js, the table is rendered using the v-for instruction loop. Once custom data exists in a table (such as drop-down list boxes, radio buttons, checkboxes, etc.), you need a convenient way to get the values of these custom data from the table. In this article, we will introduce how to get the value of a custom table in Vue.js.
First of all, we need to understand the life cycle of Vue.js components, because custom tables usually contain a component. The Vue.js component has 9 life cycle functions, which are:
beforeCreate, created, beforeMount, mounted, beforeUpdate, updated, beforeDestroy, destroyed, errorCaptured
Among them, we will use created and Mounted two life cycle functions to implement the value of the custom table.
created() { this.headers = [ { text: 'Name', value: 'name' }, { text: 'Age', value: 'age' }, { text: 'Gender', value: 'gender' }, { text: 'Hobby', value: 'hobby' } ] this.items = [ { name: 'John', age: 25, gender: 'Male', hobby: 'Basketball' }, { name: 'Mary', age: 30, gender: 'Female', hobby: 'Singing' }, { name: 'David', age: 28, gender: 'Male', hobby: 'Dancing' }, { name: 'Rachel', age: 22, gender: 'Female', hobby: 'Reading' } ] }
mounted() { this.getSelectedValues = function() { let items = this.$refs.table.getSelectedItems() let selectedValues = [] for (let item of items) { selectedValues.push(item.hobby) } return selectedValues } }
In this code, we call the $refs.table.getSelectedItems() function to get the selected data items. We then use a for loop to iterate through the selected data items and add their hobby attribute values to the selectedValues array, and finally return the array.
<v-data-table :headers="headers" :items="items" ref="table"> <template v-slot:item.hobby="{ item }"> <v-select v-model="item.hobby" :items="hobbies" ref="hobby" label="Hobby"></v-select> </template> <template v-slot:body> <tbody> <tr v-for="(item, index) in items" :key="index" v-bind:style="[ index % 2 == 0 ? { background: '#f2f2f2' } : {} ]"> <td>{{ item.name }}</td> <td>{{ item.age }}</td> <td>{{ item.gender }}</td> <td><v-select v-model="item.hobby" :items="hobbies" label="Hobby"></v-select></td> </tr> </tbody> </template> </v-data-table>
In this example, we use the v-select component as custom data. To reference a DOM element, you need to add the ref attribute to the v-select element. We also demonstrated how to add a v-select column to the table. Finally, we added a v-model directive to the v-select element so that the user's selection can be bound in both directions.
Summary
Custom tables in Vue.js can provide us with more flexibility to present and process more data and information. Using the features of Vue.js, we can easily get and process data from custom tables. In this article, we use the two life cycle functions created and mounted to obtain the value of the custom table. We also covered how to use the $refs attribute to reference DOM elements.
The above is the detailed content of How to get the value of custom table vue. For more information, please follow other related articles on the PHP Chinese website!