vue.js - Vue Good Table 过滤复合列
问题描述
我有一个 vue-good-table,我想在其中稍微限制列并制作一些复合列,这些列由行的多个属性组成。现在的问题是,filterFn 采用列名来填充方法的数据参数。所以我只能过滤我决定用作列名的那个。有没有办法像在 sortFn 中一样向 filterFn 提供整行对象?
模板:
<template v-if="props.column.field === 'flags'">
<div v-if="props.row.isGlobal">Global</div>
<div v-if="props.row.isMobile">Mobile</div>
</template>
列定义(我想显示设置了 Global 或 Mobile 标志的所有行):
{
label: 'Flags',
field: 'flags',
sortable: false,
filterOptions: {
enabled: true,
filterDropdownItems: ['Global', 'Mobile'],
filterFn: this.flagFilter,
},
},
功能(未定义不工作data
):
public flagFilter(data, filterString) {
if ('Global' === filterString) {
return data.isGlobal;
} else if ('Mobile' === filterString) {
return data.isMobile;
}
return true;
}
解决方案
由于我认为没有办法将整行放入过滤器函数中,因此您可以将计算属性中的行映射到正确的数据结构
const vm = new Vue({
el: "#app",
name: "my-component",
data: {
columns: [{
label: "Flags",
field: "flags",
sortable: false,
filterOptions: {
enabled: true,
filterDropdownItems: ["Global", "Mobile"]
}
}],
rows: [{
isGlobal: true
},
{
isMobile: true
}
]
},
computed: {
actualRows() {
return this.rows.map((r) => {
if (r.isMobile) {
r.flags = "Mobile"
}
if (r.isGlobal) {
r.flags = "Global"
}
return r
})
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-good-table@2.17.3/dist/vue-good-table.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vue-good-table@2.17.3/dist/vue-good-table.css">
<div id="app">
<vue-good-table :columns="columns" :rows="actualRows">
</vue-good-table>
</div>
推荐阅读
- database - 如何仅安装 YugaByte 客户端程序
- c++ - does g++ compiler treats constexpr functions as regular in a case their return value isnt being used?
- sql - 在此 SQL 代码中使用“INTERSECT”有什么问题
- docker - 暂时禁用 docker 容器网络接口
- javascript - Firebase SDK 删除整个集合而不是只删除一个文档
- mysql - 将四个 MySQL 查询减少到一个
- visual-studio-code - 错误:(jupyter 服务器崩溃。无法连接。来自 jupyter 的错误代码:1)
- sql - SQL 视图,默认值为 null
- azure - 如何在 Powershell 运行时设置、重置或覆盖 Azure Pipelines 中的变量?
- php - VS Code 包含带有 auto_prepend_file 的函数