Vuetify数据表格:如何将动态插槽和行/项插槽结合使用

5

我正在尝试制作一个可重复使用的vuetify数据表格。如果需要的话,我希望有能力传递动态插槽或项目插槽,并且两者都应该可以工作。以下是代码:

<v-data-table
    :headers="headers"
    :items="items"
    :dense="dense"
    :hide-default-header="hideDefaultHeader"
    :loading="loading"
    :options.sync="settings"
    :items-per-page="itemsPerPage"
    :server-items-length="itemLength"
    @click:row="handleClick"
    :height="height"
>
    <-- 1st part-->
    <template v-for="field in slots" v-slot:[field.field]="{ item }">
        <slot :name="field.field" :item="item"></slot>
    </template>
    <-- 2nd part -->
    <template v-slot:item="{item}">
        <slot name="item" v-bind="item"></slot>
    </template>
</v-data-table>

这样就创建了一个叫做DataTable的组件,然后我可以像这样使用它:

<DataTable
    v-if="tabs == n"
    :items="items"
    :loading="loading"
    :headers="headers"
    :options="serverOptions"
    :slots="slots"
    :total="toatl"
    @updateOptions="updateOptions($event, n)"
>

我也有一个像这样的插槽数组

slots: [{ field: "item.CREATOR" }, { field: "item.BODY" }]

我希望能够使用第二部分来使用item插槽或使用在插槽数组中定义的特定列插槽第一部分。目前,动态插槽只有在我注释掉第二部分时才能够使用。这是我当前对每个部分进行插槽的方式。
<template v-slot:item.CREATOR="{ item }">
    <strong>{{item.FIRSTNAME}} {{item.LASTNAME}}</strong>
</template>

2nd part:

<template v-slot:item="item">
    <tr>
        <td>{{item.MESSAGESTATUS}}</td>
        <td>sdsd</td>
        <td>sfsf</td>
    </tr>
</template>
2个回答

10
您希望实现的功能可以通过使用计算属性来解决,以满足您对动态插槽的需求:
 computed: {
   creatorSlots: function () {
    //Filters the slots of type CREATOR
    return this.slots.filter(slot => slot.field==='CREATOR');
   }
 }

现在,您可以将第一部分用于槽数组上的每个“CREATOR”插槽。
<template v-for="(slot,i) in creatorSlots" v-slot:[`item.${slot.field}`]="{ item }">
    <strong :key="i">{{item.FIRSTNAME}} {{item.LASTNAME}}</strong>
</template>

如果您有其他需要以另一种方式处理的插槽,您只需要为其他插槽使用另一个计算属性并重复这个过程即可...

 computed: {
   bodySlots: function () {
    //Filters the slots of type BODY
    return this.slots.filter(slot => slot.field==='BODY');
   }
 }

<template v-for="(slot,i) in bodySlots" v-slot:[`item.${slot.field}`]="{ item }">
    <tr>
        <td>{{item.MESSAGESTATUS}}</td>
        <td>sdsd</td>
        <td>sfsf</td>
    </tr>
</template>

希望这能帮到您,我遇到了类似的问题,我是通过以下方式解决的。

0

这可能会对你有所帮助。

<div id="app">
   <v-app id="inspire">
     <v-container>
    <v-row><v-btn @click="add">ADD</v-btn><v-btn 
          @click="remove">REMOVE</v-btn></v-row>
            <v-data-table
  :headers="headers"
  :items="desserts"  
>
  <template v-slot:body="props">
    <tr v-for="item in props.items">
      <td v-for="(header, index) in headers">
        <span v-if="index">{{ item.cols[index-1] }}</span>
        <span v-else>{{ item.name }}</span>
      </td>
    </tr>
  </template>
 </v-data-table>
      </v-container>
  </v-app>
</div>

codepen

{{ link1:codepen }}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接