[英]Issue when trying to filter the array values using computed property in Vuejs?
<div id="app"> <div v-if="isLoaded"> <select v-model="selectNum" name="text"> <option value="" selected="selected">Select status</option> <option value="ok">ok</option> <option value="notok">notok</option> </select> </div> <div class="search-wrapper"> <input type="text" v-model="search" placeholder="Search title.."/> <label>Search Users:</label> </div> <ul> <li v-for="user in userList"></li> <li v-for="manage in manageList"></li> </ul> </div>
const app = new Vue ({ el: '#app', data: { search: '', itemsList: [], isLoaded: false, selectNum: '', userList: [ { id: 1, name: "Prem", status:"ok" }, { id: 2, name: "Chandu", status:"notok" }, { id: 3, name: "Shravya", status:"ok" }, { id: 4, name: "kirt", status:"notok" } ], manageList: [ { id: 1, name: "cc", status:"ok" }, { id: 2, name: "aa", status:"notok" }, { id: 3, name: "a", status:"ok" }, { id: 4, name: "vv", status:"notok" } ] }, created(){ this.isLoaded = true; }, computed: { filteredAndSorted(){ function compare(a, b) { if (a.name < b.name) return -1; if (a.name > b.name) return 1; return 0; } const res = this.userList.filter(user => { return user.name.toLowerCase().includes(this.search.toLowerCase()) }).sort(compare) if (this.selectNum) { return res.filter(user => user.status === this.selectNum ) } return res } } })
From multiple v-for, I want to display data initially, later I have two filters where one is for filtering array and second one is for, selecting particular array from dropdown, for all of them I have written some logic, But I not sure how to combine my looping logic in filters, so that it work accordingly?从多个 v-for 开始,我想显示数据,后来我有两个过滤器,一个用于过滤数组,第二个用于从下拉列表中选择特定数组,对于所有这些,我已经编写了一些逻辑,但我不确定如何在过滤器中组合我的循环逻辑,使其相应地工作?
This is my code link:- https://codepen.io/dhanunjayt/pen/vYeeorm这是我的代码链接:- https://codepen.io/dhanunjayt/pen/vYeeorm
You don't need to create another array to store search results.您无需创建另一个数组来存储搜索结果。 Vuejs provided computed property for that.
Vuejs 为此提供了计算属性。
HTML HTML
<div id="app">
<div v-if="isLoaded">
<select v-model="selectNum" name="text"> <!-- HERE -->
<option value="" selected="selected">Select status</option>
<option value="ok">ok</option>
<option value="notok">notok</option>
</select>
</div>
<div class="search-wrapper">
<input type="text" v-model="search" placeholder="Search title.."/>
<label>Search Users:</label>
</div>
<span>Search Results</span>
<ul>
<li v-for="user in search_results">Name:{{user.name}} and Status:{{user.status}}</li>
</ul>
<span>All Users</span>
<ul>
<li v-for="user in userList">Name:{{user.name}} and Status:{{user.status}}</li>
</ul>
</div>
JS JS
const app = new Vue({
el: "#app",
data: {
search: "",
isLoaded: false,
selectNum: "",
userList: [
{
id: 1,
name: "Prem",
status: "ok"
},
{
id: 2,
name: "Chandu",
status: "notok"
},
{
id: 3,
name: "Shravya",
status: "ok"
},
{
id: 4,
name: "kirt",
status: "notok"
}
],
manageList: [
{
id: 1,
name: "cc",
status: "ok"
},
{
id: 2,
name: "aa",
status: "notok"
},
{
id: 3,
name: "a",
status: "ok"
},
{
id: 4,
name: "vv",
status: "notok"
}
]
},
created() {
this.isLoaded = true;
},
computed: {
search_results: function () {
let that = this;
if (that.search.length == 0 && that.selectNum.length == 0) return;
return this.userList.filter(
(item) =>
item.name
.toLocaleLowerCase()
.indexOf(that.search.toLocaleLowerCase()) >= 0 &&
item.status.toLocaleLowerCase() == that.selectNum.toLocaleLowerCase()
);
}
}
});
If i understand your question correcttly.Try this:如果我正确理解您的问题。试试这个:
search_results: function () { let filterList = ['userList','manageList'] let resultList = [] let that = this; filterList.forEach(filter => { const result = that[filter].filter((item) => item.name.toLocaleLowerCase().indexOf(that.search.toLocaleLowerCase()) >= 0 && item.status.toLocaleLowerCase() == that.selectNum.toLocaleLowerCase()) resultList = [...resultList,...result] }) return resultList }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.