Home >Web Front-end >Front-end Q&A >How to get the value of custom table vue

How to get the value of custom table vue

WBOY
WBOYOriginal
2023-05-24 10:16:37604browse

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.

  1. In the created life cycle function, we need to define the data model of the table, including table headers and table rows. The following code implements a table data model, including table headers and table rows.
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' }
  ]
}
  1. In the mounted life cycle function, we need to create a function to get the value in the custom table. Use Vue.js's $refs attribute to reference DOM elements in the table and get values ​​from DOM elements. The following code implements a function to get custom data values ​​in the table:
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.

  1. Finally, add the custom data in the table and make sure to add the ref attribute to the DOM elements of the custom columns so they can be referenced using the $refs attribute. The following code demonstrates how to add custom data and reference DOM elements to a table.
<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!

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