如何仅显示 show/hide 此 bootstrapvue table 的第 2 和第 3 列?

How to show only show/hide 2nd and 3rd column of this bootstrapvue table?

下面的代码将 show/hide BootstrapVue table 中的所有列。代码归功于此处的答案;

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 },
        ]
      }
    }
  })
}

<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>

下面是 table 的样子;

我想要的是 show/hide 只有 First NameLast Name 列,而不是所有列。

我正在使用 Vue v2.6 和 BootstrapVue

您可以添加另一个计算 属性 并过滤需要的字段:

new Vue({
  el: '#app',
  computed: {
    visibleFields() {
      return this.fields.filter(field => field.visible)
    },
    showFields() {
      return this.fields.filter(field => field.key.includes('first') || field.key.includes('last'))
    }
  },
  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 },
      ]
    }
  }
})
<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 showFields" 
    :key="field.key" 
    v-model="field.visible" 
    inline
  >
    {{ field.label }}
  </b-checkbox>
  <br /><br />
  <b-table :items="items" :fields="visibleFields" bordered>
  </b-table>
</div>