使用 bootstrap-vue 组件和 bootstrap 3 动态显示/隐藏列

Lou*_*les 1 twitter-bootstrap-3 vue.js

我目前正在尝试使用 bootstrap-vue 表(https://bootstrap-vue.js.org/docs/components/table)动态显示/隐藏。

到目前为止,我只设法隐藏了标题,但单元格不会消失,这是一个问题,因为单元格不在好的标题前面(数字应该在天以下,而“成分”应该在项目下。

在此处输入图片说明

这是“有效”的内容:

fields: [
{key: "day", label: "Day",sortable: true, thStyle:"display:none"},
{key: "item", label: "Item",sortable: true}
]
Run Code Online (Sandbox Code Playgroud)

我想知道是否有可能在 vueJS 之外执行此操作,并直接使用 CSS 更改列的属性。

谢谢您的帮助 !

路易斯

Hiw*_*iws 9

您可以向字段添加其他属性。例如visible,创建一个计算属性,过滤掉所有带有visible: false.

这样你就可以简单地切换这个属性来显示/隐藏你的列。

window.onload = () => {
  new Vue({
    el: '#app',
    computed: {
      visibleFields() {
        return this.fields.filter(field => field.visible)
      }
    },
    data() {
      return {
        items: [
          { id: 1, first: 'Mike', last: 'Kristensen', age: 16 },
          { id: 2, first: 'Peter', last: 'Madsen', age: 52 },
          { id: 3, first: 'Mads', last: 'Mikkelsen', age: 76 },
          { id: 4, first: 'Mikkel', last: 'Hansen', age: 34 },
        ],
        fields: [
          { key: 'id', label: 'ID', visible: true },
          { key: 'first', label: 'First Name', visible: true },
          { key: 'last', label: 'Last Name', visible: true },
          { key: 'age', label: 'Age', visible: true },
        ]
      }
    }
  })
}
Run Code Online (Sandbox Code Playgroud)
<link href="https://unpkg.com/bootstrap@4.4.1/dist/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://unpkg.com/bootstrap-vue@2.2.2/dist/bootstrap-vue.css" rel="stylesheet"/>

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.js"></script>
<script src="https://unpkg.com/bootstrap-vue@2.2.2/dist/bootstrap-vue.min.js"></script>

<div id='app'>
<b-checkbox
    :disabled="visibleFields.length == 1 && field.visible"
    v-for="field in fields" 
    :key="field.key" 
    v-model="field.visible" 
    inline
  >
  	{{ field.label }}
  </b-checkbox>
  <br /><br />
  <b-table :items="items" :fields="visibleFields" bordered>
  </b-table>
</div>
Run Code Online (Sandbox Code Playgroud)