通过 Axios 接收的列表中的 Vue2 搜索 [英] Vue2 Search in List received via Axios
问题描述
由于过滤比在 Vue 1 中复杂得多,我有一个问题.
这是我的组件,其中显示了绵羊列表,其中包含搜索/过滤名称或家庭的选项.但我不知道如何实现这一点.
<ul><li v-for="绵羊中的羊"><!-- 也尝试过:'sheep in filtersSheeps'-->{{sheep.name }} ({{sheep.type }}/{{sheep.family }} )<脚本>从 'axios' 导入 axios;导出默认{数据() {返回 {羊:[],搜索: '',};},安装(){this.getSheeps();}方法: {getSheeps() {var self = this;const url = '/api/sheeps';axios.get(url).then(function(response) {self.sheeps = response.data;});},},计算:{过滤的羊:函数(){var self = this;返回 this.sheeps.filter(function(item) {返回 item.family.toLowerCase().indexOf(self.search.toLowerCase()) >-1})}}}}
我认为它需要 v-for
中的计算方法 filteredSheeps
,但也不是这样.然后直接报错:TypeError: null is not an object (evaluating 'item.family.toLowerCase')"
这里有一个计算,可以保护您免受 family
和/或 name
不在的情况人口稠密.
filteredSheeps: function() {让 searchTerm = (this.search || "").toLowerCase()返回 this.sheeps.filter(function(item) {let family = (item.family || "").toLowerCase()let name = (item.name || "").toLowerCase()返回 family.indexOf(searchTerm) >-1 ||name.indexOf(searchTerm) >-1})}
还有一个工作示例.
console.clear()const 羊 = [{名称:百合",类型:杜泊",家庭:家庭 1"},{名称:乔",类型:美利奴",家庭:家庭 2"},{name: "Bob", type: "Dorper", family: null},]新的 Vue({el: "#app",数据:{羊:[],搜索:空},计算:{过滤的羊:函数(){让 searchTerm = (this.search || "").toLowerCase()返回 this.sheeps.filter(function(item) {let family = (item.family || "").toLowerCase()let name = (item.name || "").toLowerCase()返回 family.indexOf(searchTerm) >-1 ||name.indexOf(searchTerm) >-1})}},创建(){setTimeout(() => this.sheeps = 羊,500)}})
<script src="https://unpkg.com/vue@2.4.2"></script><div id="应用程序"><input type="search" v-model="search" placeholder="Search for Name OR Family"/><ul><li v-for="过滤羊中的羊">{{sheep.name }} ({{sheep.type }}/{{sheep.family }} )
since filtering is way more complex then in Vue 1, I have a question.
This is my Component, where a list of Sheeps is shown with the option to search/filter on Name or Family. But I can't figure out how to achieve this.
<input type="search" v-model="search" placeholder="Search for Name OR Family" />
<ul>
<li v-for="sheep in sheeps"> <!-- Tried also: 'sheep in filteredSheeps' -->
{{ sheep.name }} ({{ sheep.type }}/{{ sheep.family }} )
</li>
</ul>
<script>
import axios from 'axios';
export default {
data() {
return {
sheeps: [],
search: '',
};
},
mounted() {
this.getSheeps();
}
methods: {
getSheeps() {
var self = this;
const url = '/api/sheeps';
axios.get(url).then(function(response) {
self.sheeps = response.data;
});
},
},
computed: {
filteredSheeps: function() {
var self = this;
return this.sheeps.filter(function(item) {
return item.family.toLowerCase().indexOf(self.search.toLowerCase()) > -1
})
}
}
}
}
</script>
I thought that it needed the computed method filteredSheeps
in the v-for
, but that's not it either. Getting an error directly then:
TypeError: null is not an object (evaluating 'item.family.toLowerCase')"
Here is a computed that will protect you from cases where the family
and/or name
is not populated.
filteredSheeps: function() {
let searchTerm = (this.search || "").toLowerCase()
return this.sheeps.filter(function(item) {
let family = (item.family || "").toLowerCase()
let name = (item.name || "").toLowerCase()
return family.indexOf(searchTerm) > -1 || name.indexOf(searchTerm) > -1
})
}
And a working example.
console.clear()
const sheeps = [
{name: "Lily", type: "Dorper", family: "Family 1"},
{name: "Joe", type: "Merino", family: "Family 2"},
{name: "Bob", type: "Dorper", family: null},
]
new Vue({
el: "#app",
data:{
sheeps:[],
search: null
},
computed: {
filteredSheeps: function() {
let searchTerm = (this.search || "").toLowerCase()
return this.sheeps.filter(function(item) {
let family = (item.family || "").toLowerCase()
let name = (item.name || "").toLowerCase()
return family.indexOf(searchTerm) > -1 || name.indexOf(searchTerm) > -1
})
}
},
created(){
setTimeout(() => this.sheeps = sheeps, 500)
}
})
<script src="https://unpkg.com/vue@2.4.2"></script>
<div id="app">
<input type="search" v-model="search" placeholder="Search for Name OR Family" />
<ul>
<li v-for="sheep in filteredSheeps">
{{ sheep.name }} ({{ sheep.type }}/{{ sheep.family }} )
</li>
</ul>
</div>
这篇关于通过 Axios 接收的列表中的 Vue2 搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!