我是个初学者,所以这对你们来说可能是显而易见的,但是...我正在 Vue js 2.0 中创建一个过滤器,可以在任何列中进行过滤。我想出了这个
computed: {
filteredAndSortedData() {
let result = this.testData;
if (this.filterValue) {
result = result.filter(item =>
item.round.includes(this.filterValue) ||
item.cat.includes(this.filterValue) ||
item.player1.includes(this.filterValue) ||
item.player2.includes(this.filterValue));
}
这里是 jsfiddle 示例:https://jsfiddle.net/ebxsvac0/我正在尝试做的事情。
我的问题是如何重写此代码而不对列变量进行硬编码。 谢谢
最佳答案
您可以使用以下内容:
if (this.filterValue) {
result = result.filter(item => Object.keys(item).map((key) => item[key].includes(this.filterValue)).includes(true));
}
对于每个项目 - 检查所有键(它们是列名称)并检查每个项目是否包含您要在该列中过滤的值。
这是您的 jsfiddle 的更新:
https://jsfiddle.net/ebxsvac0/1/
关于javascript - Vue js - 过滤具有多列的表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45775658/