插槽标题 Vuetify 2.0

3

早上好,我正在修改vuetify.js中数据表的槽头以添加工具提示,这一切都做得很好,但是升序和降序的箭头没有显示出来,我想知道我做错了什么。

<template  v-slot:header="{ props: { headers } }">
    <thead>
        <tr>
            <th 
                style="white-space: nowrap"
                :class="['column sortable', pagination.descending ? 'desc' : 'asc', header.value === pagination.sortBy ? 'active' : '']"
                @click="changeSort(header.value)"
                v-for="element in headers" 
                :key="element.text">
                <v-tooltip top>
                    <template v-slot:activator="{ on }">
                        <span v-on="on">{{element.text}}</span>
                    </template>
                    <span>{{element.text}}</span>
                </v-tooltip>
            </th>
        </tr>
    </thead>
</template>

你应该提到你正在使用的Vuetify版本,2.0有很多变化。 - Keegan
抱歉,版本是2.2.21。 - Almic
1个回答

7

文档 中所述:

需要注意的是,某些插槽(例如:item/body/header)将完全接管组件的内部渲染,这将要求您重新实现诸如选择和展开等功能。

但是您可以自定义默认标题

您可以使用动态插槽 header. 来仅自定义某些列。其中 headers 是发送给标题的相应头部项目中的值属性的名称。

修改文档中第二个部分的代码片段,您可以了解如何使用动态插槽名为每个标题添加工具提示。在这里,我为前三列设置了一个工具提示,而其余列则采用默认文本,但是在 headerTooltips 中为所有标题填充工具提示文本将为所有标题生成工具提示。

<div id="app">
  <v-app id="inspire">
    <v-data-table
      :headers="headers"
      :items="desserts"
      class="elevation-1"
    >
      <template v-for="header in headers" v-slot:[`header.${header.value}`]="{ header }">
        <v-tooltip v-if="headerTooltips[header.value]" bottom :key="header.value">
          <template v-slot:activator="{ on }">
            <span v-on="on">{{ header.text }}</span>
          </template>
          <span>{{ headerTooltips[header.value] }}</span>
        </v-tooltip>
        <span v-else>{{ header.text }}<span>
      </template>
    </v-data-table>
  </v-app>
</div>

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data: () => ({
    headers: [
      {
        text: 'Dessert (100g serving)',
        align: 'start',
        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' },
    ],
    headerTooltips: {
      name: 'Name tooltip',
      calories: 'Calories tooltip',
      fat: 'Fat tooltip'
    },
    desserts: [
      {
        name: 'Frozen Yogurt',
        calories: 159,
        fat: 6.0,
        carbs: 24,
        protein: 4.0,
        iron: '1%',
      },
      {
        name: 'Ice cream sandwich',
        calories: 237,
        fat: 9.0,
        carbs: 37,
        protein: 4.3,
        iron: '1%',
      },
      {
        name: 'Eclair',
        calories: 262,
        fat: 16.0,
        carbs: 23,
        protein: 6.0,
        iron: '7%',
      },
      {
        name: 'Cupcake',
        calories: 305,
        fat: 3.7,
        carbs: 67,
        protein: 4.3,
        iron: '8%',
      },
      {
        name: 'Gingerbread',
        calories: 356,
        fat: 16.0,
        carbs: 49,
        protein: 3.9,
        iron: '16%',
      },
      {
        name: 'Jelly bean',
        calories: 375,
        fat: 0.0,
        carbs: 94,
        protein: 0.0,
        iron: '0%',
      },
      {
        name: 'Lollipop',
        calories: 392,
        fat: 0.2,
        carbs: 98,
        protein: 0,
        iron: '2%',
      },
      {
        name: 'Honeycomb',
        calories: 408,
        fat: 3.2,
        carbs: 87,
        protein: 6.5,
        iron: '45%',
      },
      {
        name: 'Donut',
        calories: 452,
        fat: 25.0,
        carbs: 51,
        protein: 4.9,
        iron: '22%',
      },
      {
        name: 'KitKat',
        calories: 518,
        fat: 26.0,
        carbs: 65,
        protein: 7,
        iron: '6%',
      },
    ],
  }),
})

这是那个 Codepen: https://codepen.io/keeganwitt/pen/ExVVyqa


这太棒了!然而,当我尝试在这个例子中添加内容时:https://vuetifyjs.com/en/components/data-tables/#crud-actions 它会引发异常"Unexpected identifier",我认为这是因为 v-slot header 语法的问题。提供一个关于如何解释 [\header.${header.value}`]v-slot:[`header.${header.value}`]="{ header }"` 中的参考链接将非常有帮助。谢谢! - Adam Cox
顺便说一下...我正在使用内部模板:`....`,所以我认为我需要转义反引号或者实现替代方案? - Adam Cox
1
我想到的是转义反引号和$符号 - 这个 <template v-for="h in headers" v-slot:[\header.${h.value}`]="{ header }">变成了<template v-for="h in headers" v-slot:[\header.\${h.value}\\]="{ header }">`。 - Adam Cox
抱歉,我对此一无所知。我需要了解更多关于它在该环境中如何打包的信息。如果它能正常工作,我猜应该没问题 /耸肩 - Keegan
我正在使用Vue组件,并使用反引号将模板分配给组件的属性。如果有另一种方法可以将工作分解,以便我可以使用单独的HTML文件,我会很感兴趣学习。Angular允许这样做,但是在Vue中我没有看到同样的功能。干杯! - Adam Cox
显示剩余2条评论

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