JavaScript
x
49
49
1
const app = new Vue({
2
el: '#app',
3
data: {
4
search: '',
5
itemsList: [],
6
isLoaded: false,
7
selectNum: status,
8
userList: [{
9
id: 1,
10
name: "Prem",
11
status: "ok"
12
},
13
{
14
id: 2,
15
name: "Chandu",
16
status: "notok"
17
},
18
{
19
id: 3,
20
name: "Shravya",
21
status: "ok"
22
},
23
{
24
id: 4,
25
name: "kirt",
26
status: "notok"
27
}
28
]
29
},
30
created() {
31
vm.itemsList = [];
32
vm.isLoaded = true;
33
},
34
computed: {
35
filteredAndSorted() {
36
// function to compare names
37
function compare(a, b) {
38
if (a.name < b.name) return -1;
39
if (a.name > b.name) return 1;
40
return 0;
41
}
42
43
return this.userList.filter(user => {
44
return user.name.toLowerCase().includes(this.search.toLowerCase()) &&
45
user.status === this.selectNum
46
}).sort(compare)
47
}
48
}
49
})
JavaScript
1
15
15
1
<div id="app">
2
<div v-if="isLoaded">
3
<select v-model="selectNum" name="text">
4
<option :value="status">ok</option>
5
<option :value="status">notok</option>
6
</select>
7
</div>
8
<div class="search-wrapper">
9
<input type="text" v-model="search" placeholder="Search title.." />
10
<label>Search Users:</label>
11
</div>
12
<ul>
13
<li v-for="user in filteredAndSorted">{{user.name}}</li>
14
</ul>
15
</div>
I am trying to achieve functionality like,
Initially the array will be loaded with the help of v-for. At the top I have two options called search-bar and dropdown. Where With the help of those I am trying to filler the array.
Where with search-bar, I want to filter array values. With dropdown, I want to filter the status present in each array.
Advertisement
Answer
try like following snippet:
JavaScript
1
52
52
1
const app = new Vue ({
2
el: '#app',
3
data() {
4
return {
5
search: '',
6
itemsList: [],
7
isLoaded: false,
8
selectNum: '',
9
userList: [
10
{
11
id: 1,
12
name: "Prem",
13
status:"ok"
14
},
15
{
16
id: 2,
17
name: "Chandu",
18
status:"notok"
19
},
20
{
21
id: 3,
22
name: "Shravya",
23
status:"ok"
24
},
25
{
26
id: 4,
27
name: "kirt",
28
status:"notok"
29
}
30
]
31
}
32
},
33
created(){
34
this.isLoaded = true;
35
},
36
computed: {
37
filteredAndSorted(){
38
function compare(a, b) {
39
if (a.name < b.name) return -1;
40
if (a.name > b.name) return 1;
41
return 0;
42
}
43
const res = this.userList.filter(user => {
44
return user.name.toLowerCase().includes(this.search.toLowerCase())
45
}).sort(compare)
46
if (this.selectNum) {
47
return res.filter(user => user.status === this.selectNum )
48
}
49
return res
50
}
51
}
52
})
JavaScript
1
17
17
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
2
<div id="app">
3
<div v-if="isLoaded">
4
<select v-model="selectNum" name="text">
5
<option value="" selected="selected">Select status</option>
6
<option value="ok">ok</option>
7
<option value="notok">notok</option>
8
</select>
9
</div>
10
<div class="search-wrapper">
11
<input type="text" v-model="search" placeholder="Search title.."/>
12
<label>Search Users:</label>
13
</div>
14
<ul>
15
<li v-for="user in filteredAndSorted">{{user.name}}</li>
16
</ul>
17
</div>