Search code examples
javascriptvue.jsvuejs2vue-componentbootstrap-vue

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


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

Show/Hide columns dynamically with a bootstrap-vue component and bootstrap 3

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>

Here's how the table will look like;

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.


Solution

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