【问题标题】:Computed properties in Vuetify Datatable rowsVuetify Datatable 行中的计算属性
【发布时间】:2018-08-19 05:52:04
【问题描述】:

我正在为网站使用 Vuetify 和 Vuetify/Datatables。 现在我想在表格的每一行上添加一些 computed 属性。

为此,我可能需要创建<template> 元素的组件并将计算属性添加到该组件。我试过<template is="myComponent" :m="props.item">,但这不起作用。

<v-data-table
  :headers="headers"
  :items="items"
  hide-actions
  class="elevation-1"
>
  <template slot="items" slot-scope="props">
    <td>{{ THIS_VALUE_COMPUTED }}</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>

【问题讨论】:

  • 您要计算什么类型的数据以及您要寻找的结果是什么?
  • 我确实找到了解决方案,请参阅下面的答案。我想对表格中每一行的值进行计算,比如将脂肪、碳水化合物和蛋白质加在一起。我希望不必循环遍历数组,因为过去这给我带来了麻烦。
  • 为什么不使用获取您想要使用的参数的方法而不是计算属性?
  • @SimonD 也不起作用。

标签: vue-component vuetify.js computed-properties


【解决方案1】:

通过包含一个额外的组件(带有计算属性)和一个额外的&lt;template&gt; 元素,我能够使用计算属性。我对在彼此内部有两个 &lt;template&gt; 元素并不满意,但这是使其正常工作的唯一方法。任何更好的解决方案仍然非常受欢迎。

(Working codepen example)

JS(对 Vuetify Datatable 示例的修改):

let myComponent = Vue.component('my-component', {
  props: {
    item: {
      type: Object,
      required: true,
    }
  },
  mounted: function() {
    console.log('mounted', this.item)
  },
  computed: {
    COMPUTED_PROPERTY: function() {
      return this.item.fat +
        this.item.carbs +
        this.item.protein
    }
  },
  template: `<tr>
    <td>{{ item.name }}</td>
    <td>{{ item.calories }}</td>
    <td>{{ COMPUTED_PROPERTY }}</td>
    <td>{{ item.iron }}</td>
  </tr>`
})

new Vue({
  el: '#app',
  mounted: function() {
    console.log('loaded')
  },
  components: { myComponent },
  data: () => ({
    pagination: {
      sortBy: 'name'
    },
    selected: [],
    headers: [
      {
        text: 'Dessert (100g serving)',
        align: 'left',
        value: 'name'
      },
      { text: 'Calories', value: 'calories' },
      { text: 'Fat + Carbs + Protein (g)', value: 'total' },
      { 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%'
      }
    ]
  }),

  methods: {
    changeSort (column) {
      if (this.pagination.sortBy === column) {
        this.pagination.descending = !this.pagination.descending
      } else {
        this.pagination.sortBy = column
        this.pagination.descending = false
      }
    }
  }
})

HTML:

<div id="app">
<v-app id="inspire">
  <v-data-table
    v-model="selected"
    :headers="headers"
    :items="items"
    select-all
    :pagination.sync="pagination"
    item-key="name"
    class="elevation-1"
  >
    <template slot="headers" slot-scope="props">
      <tr>
        <th
          v-for="header in props.headers"
          :key="header.text"
          :class="['column sortable', pagination.descending ? 'desc' : 'asc', header.value === pagination.sortBy ? 'active' : '']"
          @click="changeSort(header.value)"
        >
          <v-icon small>arrow_upward</v-icon>
          {{ header.text }}
        </th>
      </tr>
    </template>
    <template slot="items" slot-scope="props">
      <template :active="props.selected" @click="props.selected = !props.selected">
        <my-component :item="props.item">
        </my-component>
      </template>
    </template>
  </v-data-table>
</v-app>
</div>

【讨论】:

    【解决方案2】:

    这是一个有点老的问题,但这可能有效,它来自 vuetify 2.0.19 文档。在这里,您可以对表的单个属性之一执行计算值。

    <v-data-table
      :headers="headers"
      :items="items"
      hide-actions
      class="elevation-1"
    >
       <template
                v-slot:item.calories="{ item }" >
        {{ THIS_VALUE_COMPUTED }}
      </template>
    </v-data-table>
    

    【讨论】:

    • 更简单的方法,它适用于我。谢谢 !我不知道如何使用计算属性。我不能不使用item,我必须将它作为参数传递。因此,我只能使用method,因为当我使用计算属性时,比如说{{ myComputed(item) }},我在控制台中得到myComputed is not a function。我切换到method,它对我来说就像这样。
    • 嘿@JrmDel,你有一个完整的代码笔来说明这一点。我无法使其工作,使用计算或方法,它不起作用。您能说明 myComputed() 方法的语法是什么吗?
    猜你喜欢
    • 2020-08-01
    • 1970-01-01
    • 2010-09-25
    • 2020-12-01
    • 2019-02-22
    • 1970-01-01
    • 2019-10-06
    • 2020-02-05
    • 2014-01-23
    相关资源
    最近更新 更多