vue.js 数组过滤二维数组的问题?

1.我的需求:在搜索框输入内容时能过滤出包含内容的数据,删除内容时能返回数据初始状态

       {A商品,B商品,C商品},{B商品}
        搜索:'A'    返回:A商品;  
        退格:''     返回  {A商品,B商品,C商品},{B商品}

2.数组结构:

https://img4.mukewang.com/5bac40ff000149e705530391.jpg

3.我的代码:

https://img4.mukewang.com/5bac410e000120f004950533.jpg

3.我的问题:
在退格后不会返回数据初始状态.
是因为进行了两次filter的原因吗?但我没有修改原数组,而是修改遍历复制的数组啊?
若只对数组进行一次filter,退格是会返回初始状态

如:return arrs.filter(function(item){return item==1})

希望能得到解决方案.谢谢!


紫衣仙女
浏览 2220回答 1
1回答

慕斯王

我认为你要对标题进行过滤的话直接把过滤器设置在每一个商品上比较好,没必要对整个全部进行过滤处理,比如我这样。也能实现业务场景。至于你的代码的错误,我不太懂明白为什么在computed里定义methods,我也刚开始学,如果可以给下源码,也想看看你的方法错在哪<template>&nbsp; <div class="hello">&nbsp; &nbsp; <input type="text" v-model="searchGood">&nbsp; &nbsp; <ul>&nbsp; &nbsp; &nbsp; <template v-for="arr in arrs " v-show='arr.showtr'>&nbsp; &nbsp; &nbsp; &nbsp; <li v-for="good in arr.goods | filterBy goodFilter">{{good.title}}</li>&nbsp; &nbsp; &nbsp; </template>&nbsp; &nbsp; </ul>&nbsp; </div></template><script>&nbsp; export default {&nbsp; &nbsp; data () {&nbsp; &nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; &nbsp; // note: changing this line won't causes changes&nbsp; &nbsp; &nbsp; &nbsp; // with hot-reload because the reloaded component&nbsp; &nbsp; &nbsp; &nbsp; // preserves its current state and we are modifying&nbsp; &nbsp; &nbsp; &nbsp; // its initial state.&nbsp; &nbsp; &nbsp; &nbsp; msg: 'Hello World!',&nbsp; &nbsp; &nbsp; &nbsp; searchGood: '',&nbsp; &nbsp; &nbsp; &nbsp; items: [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {message: 'Foo'},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {message: 'Bar'}&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; arrs: [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; goods: [{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title: 'A标题'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title: 'B标题'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title: '这是标题'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; orderNo: '111111111111',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; showtr: true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; goods: [{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title: 'AA标题'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title: 'BB标题'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; orderNo: '222222222222',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; showtr: true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }]&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; },&nbsp; &nbsp; methods: {&nbsp; &nbsp; &nbsp; goodFilter (good) {&nbsp; &nbsp; &nbsp; &nbsp; if (this.searchGood === '') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (good.title.indexOf(this.searchGood) !== -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }</script><!-- Add "scoped" attribute to limit CSS to this component only --><style scoped>&nbsp; h1 {&nbsp; &nbsp; color: #42b983;&nbsp; }</style>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript