gpt4 book ai didi

javascript - 搜索栏 vue.js

转载 作者:行者123 更新时间:2023-12-05 04:44:42 25 4
gpt4 key购买 nike

我想在 vue.js 中包含搜索栏。我对 vue 很陌生。我需要在前端过滤数据。在脚本部分获取数据时如何过滤。

这是我的代码:

<template>
<div>
<div class="search-wrapper panel-heading col-sm-12">
<input type="text" v-model="search" placeholder="Search" /> <br />
<br />
</div>
<table class="table" id="myTable">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Category</th>
<th>Quantity</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr v-for="product in products" :key="product.id">
<td>{{ product.id }}</td>
<td>{{ product.name }}</td>
<td>{{ product.category }}</td>
<td>{{ product.quantity }}</td>
<td>{{ product.status }}</td>
</tr>
</tbody>
</table>
</div>
</template>

<script>
export default {
data() {
return {
products: []
};
}
};
</script>

最佳答案

在 vue 中一个简单的方法是使用 computed property搜索文本更改时自动过滤您的产品。

例如:

Vue.createApp({
data() {
return {
products: [
{id: 1, name: "Foo"},
{id: 2, name: "Bar"},
{id: 3, name: "Baz"},
{id: 4, name: "Foobar"}
],
search: ""
};
},
computed: {
filteredProducts() {
return this.products.filter(p => {
// return true if the product should be visible

// in this example we just check if the search string
// is a substring of the product name (case insensitive)
return p.name.toLowerCase().indexOf(this.search.toLowerCase()) != -1;
});
}
}
}).mount('#app');
<script src="https://unpkg.com/vue@next"></script>
<div id="app">
<div class="search-wrapper panel-heading col-sm-12">
<input type="text" v-model="search" placeholder="Search" /> <br> <br>
</div>
<table class="table" id="myTable">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr v-for="product in filteredProducts" :key="product.id">
<td>{{ product.id }}</td>
<td>{{ product.name }}</td>
</tr>
</tbody>
</table>
</div>

关于javascript - 搜索栏 vue.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69287834/

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