首页 > 解决方案 > Vuetify 数据表通用组件

问题描述

<template>
  <v-data-table
    :headers="this.headers"
    :items="this.items"
    hide-actions
    class="elevation-1"
  >
    <template slot="items" slot-scope="props">
      <td>{{ props.item.name }}</td>
      <td class="text-xs-right">{{ props.item.calories }}</td>
      <td class="text-xs-right">{{ props.item.fat }}</td>
      <td class="text-xs-right">{{ props.item.carbs }}</td>
      <td class="text-xs-right">{{ props.item.protein }}</td>
      <td class="text-xs-right">{{ props.item.iron }}</td>
    </template>
  </v-data-table>
</template>

<script>
export default {
  data () {
    return {
      items: [],
      headers: [],
      attributes: []
    }
  },
  methods: {
    loadItems (items) {
      items.forEach(item => {
        var counter = 0
        var obj = `{\n`
        attributes.forEach(attribute => {
          obj += '"' + attribute + '" : ' + item[counter]
          if (counter < attributes.lenght)
            obj += ',\n'
          else
            obj += '\n'
          counter++;
        })
        obj += '}'
        this.items.push(obj)
      })
    },
    loadHeaders (headers) {
      headers.forEach(header => {
        this.headers.push({ text: header, value: header.toLowerCase()})
        this.attributes.push(headers)
      })
    },
    loadTable (items, headers) {
      this.loadHeaders(headers)
      this.loadItems(items)
    }
  }
}
</script>

<style scoped>
</style>

这些函数是否可以动态构建带有自定义项目和标题的数据表?我想构建一个数据表组件,该组件对于我的 SQLlite3 数据库中的每个数据表都是通用的。如何更改模板以显示属性数组中项目的自定义属性?

标签: javascriptvue.jsdatatablevuetify.js

解决方案


推荐阅读