[英]Can't filter deep array object in computed with Vue in a table
I have been trying a lot of ways to filter the data, like using only methods or watchers but I can't achieve what I want.我一直在尝试很多方法来过滤数据,比如只使用方法或观察者,但我无法实现我想要的。 Basically, when I search for something in the input I want only the table to display that item and its way to it.
基本上,当我在输入中搜索某些内容时,我只希望表格显示该项目及其路径。 Now I'm trying with computed and I get always an error saying 'filter of undefined' and things like that.
现在我正在尝试计算,我总是收到一个错误,说“未定义的过滤器”之类的。 This is my computed:
这是我的计算:
computed: {
filterData() {
return this.testData.filter(
data =>
!this.search ||
data.building.toLowerCase().includes(this.search.toLowerCase()) ||
data.children.some(item =>
item.floor.toLowerCase().includes(this.search.toLowerCase())
)
);
}}
And this is my data to filter:这是我要过滤的数据:
data: () => ({
search: "",
testData: [
{
id: 1,
building: "foo 1",
children: [
{ id: 11, floor: "bar 1" },
{ id: 12, floor: "bar 2" },
{
id: 13,
floor: "bar 3",
children: [
{
id: 131,
door: "cor 1"
}
]
}
]
},
{
id: 2,
building: "foo 2",
children: [
{ id: 21, floor: "bar 3" },
{ id: 22, floor: "bar 4" },
{
id: 23,
floor: "bar 5",
children: [
{
id: 231,
door: "cor 2"
},
{
id: 232,
door: "cor 3"
}
]
}
]
}
]
I created a sandbox in order to make more easy my explanation => https://codesandbox.io/s/modest-paper-vwes5我创建了一个沙箱,以便更容易地解释=> https://codesandbox.io/s/modest-paper-vwes5
Thank you in advance!先感谢您!
Edit: I was wrong, filtering works, but your data are very similar, so entering eg.编辑:我错了,过滤有效,但你的数据非常相似,所以输入例如。 "bar" will seem to do nothing.
“酒吧”似乎什么也不做。 You need to enter like "foo 2" or "4" or "5" to see effect.
您需要像“foo 2”或“4”或“5”这样的输入才能看到效果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.