我有一个用 bootstrap-vue 生成的表格,显示了系统搜索的结果。
结果表向用户显示记录,用户可以对它们进行排序和过滤。
如何在表格标题下方添加搜索字段 <th>
使用 bootstrap-vue 生成 <b-table>
元素?
最佳答案
您可以使用 top-row
插槽来自定义您自己的第一行。请参阅下面的基本示例。
new Vue({
el: '#app',
data: {
filters: {
id: '',
issuedBy: '',
issuedTo: ''
},
items: [{id:1234,issuedBy:'Operator',issuedTo:'abcd-efgh'},{id:5678,issuedBy:'User',issuedTo:'ijkl-mnop'}]
},
computed: {
filtered () {
const filtered = this.items.filter(item => {
return Object.keys(this.filters).every(key =>
String(item[key]).includes(this.filters[key]))
})
return filtered.length > 0 ? filtered : [{
id: '',
issuedBy: '',
issuedTo: ''
}]
}
}
})
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css"/><link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.css"/><script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.min.js"></script><script src="//unpkg.com/babel-polyfill@latest/dist/polyfill.min.js"></script><script src="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.js"></script>
<div id="app">
<b-table striped show-empty :items="filtered">
<template slot="top-row" slot-scope="{ fields }">
<td v-for="field in fields" :key="field.key">
<input v-model="filters[field.key]" :placeholder="field.label">
</td>
</template>
</b-table>
</div>
注意:我使用计算属性来过滤项目,而不是 b-table
中的 :filter
属性,因为它不' 如果所有项目都被过滤掉,则呈现行,包括您的自定义第一行。这样,如果结果为空,我可以提供一个虚拟数据行。
关于vue.js - Bootstrap-vue b-table 在标题中带有过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52959195/