use*_*981 5 html javascript vue.js vuetify.js v-slot
我有一个包含可扩展行的 vuetify 数据表。我与演示的唯一真正区别是,我希望该item.name
列能够像 V 形图标一样打开/关闭可扩展行。当我在该列的 v 槽上放置一个@click
处理程序时,我收到错误Error in v-on handler: "TypeError: expand is not a function"
。这是我需要自定义的唯一列,因此我不想<tr>
手动构建整个 v 槽。下面是缩小的代码示例。谢谢。
<v-data-table
:headers="headers"
:items="products"
item-key="productCode"
show-expand
:expanded.sync="expanded"
>
<template v-slot:item.name="{ item, expand, isExpanded }" >
<h4 class="my-2" @click="expand(!isExpanded)">{{ item.name }} located in {{ item.depot | camelToWords }}</h4>
</template>
<template v-slot:expanded-item="{ headers, item }">
<ProductDetailExpandedRow :currentProduct="item" :headers="headers"/>
</template>
</v-data-table>
<script>
export default {
data() {
return {
headers: [
{
text: 'Name',
value: 'name',
},
{
text: 'Product ID',
value: 'productCode',
},
{
text: 'Stock',
value: 'stock',
},
6 more columns continue on here...
],
products: [],
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
以下是通过单击特定列来完成此操作的方法。将@click
处理程序放入列的槽模板中。该处理程序在单击时接收列数据。在本例中,列的名称是name
:
<template v-slot:item.name="slotData">
<div @click="clickColumn(slotData)">{{ slotData.item.name }}</div>
</template>
Run Code Online (Sandbox Code Playgroud)
在数组中跟踪扩展的行expanded
,因此添加该行的数据。但如果它已经存在,请将其删除(因为那时您正在尝试折叠已经展开的列)
clickColumn(slotData) {
const indexRow = slotData.index;
const indexExpanded = this.expanded.findIndex(i => i === slotData.item);
if (indexExpanded > -1) {
this.expanded.splice(indexExpanded, 1)
} else {
this.expanded.push(slotData.item);
}
}
Run Code Online (Sandbox Code Playgroud)
这是代码笔(单击填充内的第一列时行会展开)
以下是如何通过单击行(即任何列)来完成此操作。在模板中,为事件添加监听<v-data-table>
器click:row
:
<v-data-table @click:row="clickRow">
...
</v-data-table>
Run Code Online (Sandbox Code Playgroud)
此事件传递两个参数:项目和项目槽数据,包括单击的行的索引。使用此信息修改this.expanded
跟踪所有扩展行的数组:
clickRow(item, event) {
if(event.isExpanded) {
const indexExpanded = this.expanded.findIndex(i => i === item);
this.expanded.splice(indexExpanded, 1)
} else {
this.expanded.push(item);
}
}
Run Code Online (Sandbox Code Playgroud)
这会将项目添加到expanded
数组中,或者通过查找索引并使用 来删除它splice
。
这是代码笔(单击行中任意位置时行会展开)