v-data-table项目上的调用函数

问题描述 投票:0回答:1

假设我有一个对象列表:

[teacher {
   name: '',
   department: 'this is department id',
   .......
   .......
}]

我有部门作为另一个对象:

[department {
  id: '',
  name: '',
  ......
  .....
  }]

我已将部门的getter实现为:

state: {
    departments: [],
  },
  getters: {
    getDepartmentById: (state) => (id) => {
        return state.departments.find(d => d.id == id)
      }
  },

我正在使用vuetify v-data-table渲染项目。现在,如何调用getDepartmentById函数将部门名称加载到教师列表中。我要显示的部门名称是教师列表,现在我有部门编号。

vue.js vue-component vuex vuetify.js
1个回答
0
投票

您可以将这段代码用于模板:

<template>
  <v-data-table
    :headers="headers"
    :items="teachers"
    :items-per-page="5"
    class="elevation-1"
  ></v-data-table>
</template>

以及这段脚本代码:

data: () => ({
    headers: [
      {
        text: 'Teacher',
        value: 'name' // Must contain this attribute in each teacher
      },
      {
        text: 'Teacher',
        value: 'departmentName' // Must contain this attribute in each teacher
      }
    ],
    teachers: [
    ]
}),
methods: {
    created: async() => {
        const teachersJson = await fetch(...) // call to an API
        this.teachers = teachersJson
                           .map(teacher => 
                             ({ 
                               ...teacher, // Association, same as Object.assign
                               departmentName: this.getDepartmentName(teacher.department)
                             })
                           )
    },
    getDepartmentName: (id) => {
        const department = this.$store.state.departmentsStore.findById(id)
        return department && departname.name
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.