vue.js - Bootstrap-vue b-table 在标题中带有过滤器

标签 vue.js html-table bootstrap-4 bootstrap-vue

我有一个用 bootstrap-vue 生成的表格,显示了系统搜索的结果。

结果表向用户显示记录,用户可以对它们进行排序和过滤。

如何在表格标题下方添加搜索字段 <th>使用 bootstrap-vue 生成 <b-table>元素?

当前表的截图: enter image description here

想要的表的模型: Mockup of the wanted 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/

相关文章:

javascript - Typescript:通过将函数参数传递给接口(interface)来推断类型

html - 从表格单元格中删除边框

html - Bootstrap 4 导航栏在 div 内打开

javascript - 如何通过 JavaScript 获取 html 表格 td 单元格值?

html - 当表 tbody 设置为显示 :block; 时,表 Colspan 不工作

html - bootstrap 4 中的水平滚动

css - 翻译自定义文件输入 Bootstrap 4

javascript - 在组件加载之前初始化 Vue.prototype

javascript - 动态创建模型

vue.js - 从 Vue.js 中的另一个组件调用方法