标题中带有过滤器的Bootstrap-Vue B表 [英] Bootstrap-vue b-table with filter in header
问题描述
我有一个用bootstrap-vue生成的表,该表显示了系统搜索的结果.
I have a table generated with bootstrap-vue that shows the results of a system search.
结果表向用户显示记录,用户可以对记录进行排序和过滤.
The Results Table shows the records to the user, and the user can sort them and filter them.
如何在用bootstrap-vue <b-table>
元素生成的表头<th>
下添加搜索字段?
How can I add the search field underneath the table header <th>
generated with the bootstrap-vue <b-table>
element?
当前表的屏幕截图:
想要的表的模型:
推荐答案
您可以使用top-row
插槽自定义自己的第一行.参见下面的简单示例.
You can use the top-row
slot to customise your own first-row. See below for a bare-bones example.
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
道具,因为如果所有项目都被滤除(包括您自定义的第一行.这样,如果结果为空,我可以提供一个虚拟数据行.
Note: I've used a computed property to filter the items instead of the :filter
prop in b-table
because it doesn't render rows if all the items are filtered out, including your custom first-row. This way I can provide a dummy data row if the result is empty.
这篇关于标题中带有过滤器的Bootstrap-Vue B表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!