简体   繁体   English

单击带有模板行的 v-data-table 中的行

[英]Clicking on row in v-data-table with template rows

When I added template rows (so that they have special formatting based on condition):当我添加模板行(以便它们根据条件具有特殊格式)时:

  <v-data-table
    :headers="headers"
    :items="desserts"
    :items-per-page="5"
    class="elevation-1"
    @click:row="clickedHandle"
  >
    <template v-slot:item="props">
      <tr v-if="props.item.name.includes('Fro')">
        <td>{{ props.item.name }}</td>
        <td v-for="itemx in myheaders">{{ toThousands(props.item[itemx]) }}</td>
      </tr>
      <tr v-else>
        <td v-for="itemx in props.item">{{ itemx }}</td>
      </tr>
    </template>
  </v-data-table>

The function that is supposed to be called on row click clickedHandle does not fire up at all.应该在行 clickedHandle 上调用的clickedHandle根本不会启动。 Why is that so?为什么呢?

Here is the example where you can check this behaviour: https://codesandbox.io/s/vuetify-playground-forked-lro4w?file=/src/layout.vue这是您可以检查此行为的示例: https://codesandbox.io/s/vuetify-playground-forked-lro4w?file=/src/layout.vue

The table does not seem to react on row click at all.该表似乎根本没有对行点击做出反应。

clickedHandle(value) {
  console.log(value);
},

clickedHandle should log some value on row click. clickedHandle应该在行点击时记录一些值。

Customize each cell individually as they do in the v-data-table row customization docs , not the whole row.像在v-data-table行自定义文档中那样单独自定义每个单元格,而不是整行。 You can use a v-for to dynamically target the cell slots you want (in conjunction with your myheaders array):您可以使用v-for来动态定位您想要的单元格插槽(与您的myheaders数组一起):

<template v-for="header in myheaders" v-slot:[`item.${header}`]="props">
  <template v-if="props.item.name.includes('Fro')">
    {{ toThousands(props.item[header]) }}
  </template>
  <template v-else>
    {{ props.item[header] }}
  </template>
</template>

You could avoid this slot and filter complexity by preparing your data ahead of time in a computed.您可以通过提前在计算中准备好数据来避免这个槽和过滤器的复杂性。

Here is an updated CodeSandbox这是一个更新的CodeSandbox

try with this code:试试这个代码:

<v-data-table
      :headers="headers"
      :items="desserts"
      hide-actions
      class="elevation-1"
    >
     <template slot="items" slot-scope="myprops">
  <tr v-if="myprops.item.name.includes('Fro')">
 ...
  </tr>
  <tr v-else>
    <td v-for="itemx in props.item">{{ itemx }}</td>
  </tr>
</template>
    </v-data-table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM