Noo*_*ter 5 html javascript vue.js
我想在 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>
Run Code Online (Sandbox Code Playgroud)
在 vue 中执行此操作的一个简单方法是使用计算属性,该属性会在搜索文本更改时自动过滤您的产品。
例如:
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');Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14541 次 |
| 最近记录: |