gpt4 book ai didi

javascript - 尝试使用 Vuejs 中的计算属性过滤数组值时出现问题?

转载 作者:行者123 更新时间:2023-12-05 05:51:16 27 4
gpt4 key购买 nike

<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
}
}
})

从多个 v-for 开始,我想显示数据,后来我有两个过滤器,其中一个用于过滤数组,第二个用于从下拉列表中选择特定数组,我为所有这些都编写了一些逻辑,但是我不确定如何在过滤器中组合我的循环逻辑,以便它相应地工作?

这是我的代码链接:- https://codepen.io/dhanunjayt/pen/vYeeorm

最佳答案

您不需要创建另一个数组来存储搜索结果。 Vuejs 为此提供了计算属性。

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

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()
);
}
}
});

关于javascript - 尝试使用 Vuejs 中的计算属性过滤数组值时出现问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70405134/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com