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

Time:03-13

The code below will show/hide all the columns in a BootstrapVue table. Credit of the code goes to the answer here;

enter image description here

What I want is to show/hide only the First Name and Last Name column, not all the columns.

I am using Vue v2.6 and BootstrapVue.

CodePudding user response:

You can add another computed property and filter needed fields:

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/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://unpkg.com/[email protected]/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/[email protected]/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>

  • Related