使用vuetifyJS数据表动态构建表

mor*_*tal 16 javascript vue.js vuetify.js

我有一个动态更改列的表.因此,表格的模板不能像这样硬编码 -

<template>
  <v-data-table
    :headers="headers"
    :items="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>
Run Code Online (Sandbox Code Playgroud)

我在响应中获得了此部分的代码.无法弄清楚如何向前传达它.

小智 21

我在看同样的问题,发现了一种避免在标记中硬编码数据结构的典型方法; 你可以使用标题的内容来使用v-for循环来编写项目模板的脚本,如下所示:

<div id="app">
  <v-app id="inspire">
    <v-data-table
      :headers="headers"
      :items="desserts"
      hide-actions
      class="elevation-1"
    >
      <template slot="items" slot-scope="myprops">
        <td v-for="header in headers">
        {{ myprops.item[header.value] }}
        </td>
      </template>
    </v-data-table>
  </v-app>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢@Bart,它很有魅力。简单有效。 (2认同)

Sul*_*man 6

我知道这个问题很老,但我遇到了同样的问题,我偶然发现了这个页面。谢天谢地,我通过编辑 Bart 给出的代码来解决我的问题,以满足 Vue 2 中的最新语法。

<v-data-table :headers="headers"
 :items="myDataObject"
 class="elevation-24">
    <template v-slot:body="props">
      <tr v-for="index in props.items">
        <td v-for="header in headers" class="text-left font-weight-black">
          {{ index[header.value]}}
        </td>
      </tr>
    </template>
</v-data-table>
Run Code Online (Sandbox Code Playgroud)


Jay*_*aya 5

这是您可以尝试的方法,我知道它可以工作,因为我使用了类似的方法。但是要了解它是如何工作的,请阅读vue js中的动态组件

警告:您必须自己配置每个数据绑定项,这可能会让人不知所措,但是,如果您有许多数据表,则值得这样做。不要放弃 :)

可以使用标题scoped-slot配置标题。在此处阅读文档以获取更多详细信息。查找“作用域插槽”选项卡,然后查看可以配置的内容。

现在,对于该列,您必须使用动态组件进行配置。也就是说,根据数据表列的类型返回组件,比如说是否返回文本<td>text</td>,依此类推。只需为您提出一个想法,您就可以配置所需的方法。

<v-data-table
  v-model="tableRowsSelected" 
  :items="tableItems"
  :headers="tableHeaders"
  :pagination.sync="tablePagination" 
  :rows-per-page-items="tablePaginationDropdown"
  item-key="name" 
  class="elevation-1"
> 
   <template v-if="tableHeaders" slot="headers" slot-scope="row">
    <tr>
      <th
        v-for="header in row.headers"
        :key="header.text"
        :class="['column sortable', tablePagination.descending ? 'desc' : 'asc', header.value === tablePagination.sortBy ? 'active' : '']"
        @click="changeSort(header.value)"
      >
        <v-icon small>arrow_upward</v-icon>
        {{ header.text }}
      </th>
    </tr>
  </template>
  <template template slot="items" slot-scope="row">
      <tr>
        <component v-for="header in Object.keys(row.item)" :key="header" :is="getComponentByColumnType(header, row.item)"></component>
      </tr>
  </template>

export default {
data () {
 return {
        tableItems: []
 }
 computed: {
  tableHeaders: function () { }
  tablePagination: functin() {}
  // and other properties here or you could simply configure them as part of 
  data.
  },
  method:{
    getComponentByColumnType(header, data) {
     // return the component per your column type here.
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


rol*_*oli 1

我无法回答你的问题,但我假设你想创建一个 vuetify 表。

所以下面是模板:

 <template>
  <v-data-table
    :headers="headers"
    :items="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>
Run Code Online (Sandbox Code Playgroud)

以及脚本下方:

 <script>
  export default {
    data () {
      return {
        headers: [
          {
            text: 'Dessert (100g serving)',
            align: 'left',
            sortable: false,
            value: 'name'
          },
          { text: 'Calories', value: 'calories' },
          { text: 'Fat (g)', value: 'fat' },
          { text: 'Carbs (g)', value: 'carbs' },
          { text: 'Protein (g)', value: 'protein' },
          { text: 'Iron (%)', value: 'iron' }
        ],
        items: [
          {
            value: false,
            name: 'Frozen Yogurt',
            calories: 159,
            fat: 6.0,
            carbs: 24,
            protein: 4.0,
            iron: '1%'
          },
          {
            value: false,
            name: 'Ice cream sandwich',
            calories: 237,
            fat: 9.0,
            carbs: 37,
            protein: 4.3,
            iron: '1%'
          },
          {
            value: false,
            name: 'Eclair',
            calories: 262,
            fat: 16.0,
            carbs: 23,
            protein: 6.0,
            iron: '7%'
          },
          {
            value: false,
            name: 'Cupcake',
            calories: 305,
            fat: 3.7,
            carbs: 67,
            protein: 4.3,
            iron: '8%'
          },
          {
            value: false,
            name: 'Gingerbread',
            calories: 356,
            fat: 16.0,
            carbs: 49,
            protein: 3.9,
            iron: '16%'
          },
          {
            value: false,
            name: 'Jelly bean',
            calories: 375,
            fat: 0.0,
            carbs: 94,
            protein: 0.0,
            iron: '0%'
          },
          {
            value: false,
            name: 'Lollipop',
            calories: 392,
            fat: 0.2,
            carbs: 98,
            protein: 0,
            iron: '2%'
          },
          {
            value: false,
            name: 'Honeycomb',
            calories: 408,
            fat: 3.2,
            carbs: 87,
            protein: 6.5,
            iron: '45%'
          },
          {
            value: false,
            name: 'Donut',
            calories: 452,
            fat: 25.0,
            carbs: 51,
            protein: 4.9,
            iron: '22%'
          },
          {
            value: false,
            name: 'KitKat',
            calories: 518,
            fat: 26.0,
            carbs: 65,
            protein: 7,
            iron: '6%'
          }
        ]
      }
    }
  }
</script>
Run Code Online (Sandbox Code Playgroud)

这是从 vuetify文档复制粘贴

现在,如果您想使用动态标头,只需更改 headers 属性即可。

我的建议是,在您的表中使用vuetify 多重选择用表列填充多重选择,并让用户选择或取消选择。然后在 :headers 中的数据表中使用与多重选择对应的属性

例如,如果 mutpiple select 绑定到 e6(属性名称),则 v-data-table 将查找:

  <v-data-table
    :headers="e6" /*this changed*/
    :items="items"
    hide-actions
    class="elevation-1"
  >
Run Code Online (Sandbox Code Playgroud)