【发布时间】:2019-06-21 07:49:52
【问题描述】:
我有一个 json 文件,我渲染到 table ,我有 11 列,如 id , name 等,我想按每一列搜索,但它只适用于一列,当我开始过滤数据时时间例如我按 id 排序,然后我想按名称排序,它崩溃了。
我有用于从 json 渲染数据的 v-for 结构
<div class="document__json" v-for="(item,index) in filteredJson" :key="index" >
<div class="document__data" :title=item.priority>{{item.priority}}</div>
<div class="document__data" :title=item.refid_number>{{item.refid_number}}</div>
</div>
我尝试使用 v-model 搜索 json,我发送到数组的每个字母/单词
data() {
return {
myJson: json,
search: []
};
},
filteredJson: function() {
let new_json;
return this.myJson.filter((x) => {
new_json = x;
console.log(x);
for (let i in this.search) {
console.log(new_json[i])
console.log(this.search)
new_json = new_json[i].toLowerCase().match(this.search[i].toLowerCase());
}
return new_json
});
}
<input type="text" v-model="search['priority']" class="document_search">
<input type="text" v-model="search['refid_number']" class="document_search">
我尝试通过与搜索数组中的数据进行比较来过滤 json,但是如果我将按优先级进行搜索,则只有当我也尝试按 refid_number 进行搜索时,它才会用错误填充粉碎:
Cannot read property 'refid_number' of null"
【问题讨论】:
-
我认为你需要一个新的过滤器变量。因为当您在
myJson变量中过滤数据时,该变量中将不存在数据。尝试创建let jsonForFilter = this.myJson并通过`jsonForFilter`进行过滤。祝你好运。
标签: javascript json vue.js vuejs2