Kha*_*oni 1 javascript search filter vue.js vuejs3
我有这个搜索输入:
<input
class="form-control"
type="text"
name="search"
placeholder="search..."
v-model="search"
/>
Run Code Online (Sandbox Code Playgroud)
这个“输出”区域:
<my-comp
v-for="item in filter"
:key="item.id"
:id="item.id"
:imgsrc="item.imgsrc"
:price="item.price"
:city="item.city"
:country="item.country"
:reviewnum="item.reviewnum"
:daynum="item.daynum"
/>
Run Code Online (Sandbox Code Playgroud)
我从 json 文件导入数据,这就是数据
[
{"id": 1, "city":"California", "country": "United State of America", "price": "700", "reviewnum": "890", "daynum": "5", "imgsrc": "img/place/1.png"},
{"id": 2, "city":"london", "country": "United Kingdom", "price": "550", "reviewnum": "900", "daynum": "4", "imgsrc": "img/place/2.png"},
{"id": 3, "city":"Korola Megna", "country": "Nepal", "price": "350", "reviewnum": "150", "daynum": "5", "imgsrc": "img/place/3.png"},
{"id": 4, "city":"Miami Beach", "country": "United State of America", "price": "850", "reviewnum": "660", "daynum": "7", "imgsrc": "img/place/4.png"},
{"id": 5, "city":"California", "country": "United State of America", "price": "600", "reviewnum": "380", "daynum": "6", "imgsrc": "img/place/5.png"},
{"id": 6, "city":"Saintmartine Iceland", "country": "Kingdom of the Netherlands", "price": "450", "reviewnum": "340", "daynum": "3", "imgsrc": "img/place/6.png"}
]
Run Code Online (Sandbox Code Playgroud)
这个想法是,用户搜索城市或国家或任何数据,输出应该只显示他正在搜索的卡片。
这是vue.js代码:
import data from "@/assets/data.json";
export default {
name: "Home",
data: function () {
return {
allData: data,
search: "",
};
},
components: { myComp, foooter, headeer },
computed: {
filter() {
if (!this.search) {
return this.allData;
} else {
return this.allData.filter(({ country }) =>
(country).toLowerCase().includes(this.search.toLowerCase())
);
}
},
},
};
Run Code Online (Sandbox Code Playgroud)
但我的函数只接受一个变量。我应该怎么办?
搜索所有列:
computed: {
filteredData() {
return this.allData
.filter(
(entry) => this.allData.length
? Object.keys(this.allData[0])
.some(key => ('' + entry[key]).toLowerCase().includes(this.search))
: true
);
}
}
Run Code Online (Sandbox Code Playgroud)
看看它的工作原理:
computed: {
filteredData() {
return this.allData
.filter(
(entry) => this.allData.length
? Object.keys(this.allData[0])
.some(key => ('' + entry[key]).toLowerCase().includes(this.search))
: true
);
}
}
Run Code Online (Sandbox Code Playgroud)
new Vue({
el: '#app',
data: () => ({
search: '',
allData: [
{"id": 1, "city":"California", "country": "United State of America", "price": "700", "reviewnum": "890", "daynum": "5", "imgsrc": "img/place/1.png"},
{"id": 2, "city":"london", "country": "United Kingdom", "price": "550", "reviewnum": "900", "daynum": "4", "imgsrc": "img/place/2.png"},
{"id": 3, "city":"Korola Megna", "country": "Nepal", "price": "350", "reviewnum": "150", "daynum": "5", "imgsrc": "img/place/3.png"},
{"id": 4, "city":"Miami Beach", "country": "United State of America", "price": "850", "reviewnum": "660", "daynum": "7", "imgsrc": "img/place/4.png"},
{"id": 5, "city":"California", "country": "United State of America", "price": "600", "reviewnum": "380", "daynum": "6", "imgsrc": "img/place/5.png"},
{"id": 6, "city":"Saintmartine Iceland", "country": "Kingdom of the Netherlands", "price": "450", "reviewnum": "340", "daynum": "3", "imgsrc": "img/place/6.png"}
]
}),
computed: {
filteredData() {
return this.allData
.filter(
(entry) => this.allData.length
? Object.keys(this.allData[0])
.some(key => ('' + entry[key]).toLowerCase().includes(this.search.toLowerCase()))
: true
);
}
}
})Run Code Online (Sandbox Code Playgroud)
搜索特定列:
computed: {
filteredData() {
return this.allData
.filter(
({ country, city }) => [country, city]
.some(val => val.toLowerCase().includes(this.search.toLowerCase()))
);
}
}
Run Code Online (Sandbox Code Playgroud)
看看它的工作原理:
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14"></script>
<div id="app">
<input type="search" v-model="search">
<div v-for="entry in filteredData" :key="entry.id">
<pre v-text="entry" />
</div>
</div>Run Code Online (Sandbox Code Playgroud)
computed: {
filteredData() {
return this.allData
.filter(
({ country, city }) => [country, city]
.some(val => val.toLowerCase().includes(this.search.toLowerCase()))
);
}
}
Run Code Online (Sandbox Code Playgroud)
第一个获取第一个条目中的所有键(如果有任何数据),将所有值转换为字符串(以便它可以.toLowerCase()在其上运行),并检查this.search每个条目是否包含在该字段中保存的值中。
第二个不太通用,因此更准确。当您确切知道要搜索哪些字段时,您可能需要使用它,以防止误报。
另一种选择是为用户提供一个下拉列表来选择他们想要过滤的列。
new Vue({
el: '#app',
data: () => ({
search: '',
allData: [
{"id": 1, "city":"California", "country": "United State of America", "price": "700", "reviewnum": "890", "daynum": "5", "imgsrc": "img/place/1.png"},
{"id": 2, "city":"london", "country": "United Kingdom", "price": "550", "reviewnum": "900", "daynum": "4", "imgsrc": "img/place/2.png"},
{"id": 3, "city":"Korola Megna", "country": "Nepal", "price": "350", "reviewnum": "150", "daynum": "5", "imgsrc": "img/place/3.png"},
{"id": 4, "city":"Miami Beach", "country": "United State of America", "price": "850", "reviewnum": "660", "daynum": "7", "imgsrc": "img/place/4.png"},
{"id": 5, "city":"California", "country": "United State of America", "price": "600", "reviewnum": "380", "daynum": "6", "imgsrc": "img/place/5.png"},
{"id": 6, "city":"Saintmartine Iceland", "country": "Kingdom of the Netherlands", "price": "450", "reviewnum": "340", "daynum": "3", "imgsrc": "img/place/6.png"}
]
}),
computed: {
filteredData() {
return this.allData
.filter(
({ country, city }) => [country, city]
.some(val => val.toLowerCase().includes(this.search))
);
}
}
})Run Code Online (Sandbox Code Playgroud)
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14"></script>
<div id="app">
<input type="search" v-model="search">
<div v-for="entry in filteredData" :key="entry.id">
<pre v-text="entry" />
</div>
</div>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8453 次 |
| 最近记录: |