Home > Mobile >  How to hide column of this BootstrapVue table during page load?
How to hide column of this BootstrapVue table during page load?

Time:03-17

This question is a follow-up question to the answer provided here;

enter image description here

What I want is to disable the checkbox of First Name only so that during initial page load, the First Name column does not appear.

What I did is modify the fields visible property belonging to First Name as below;

  fields: [
    { key: 'id', label: 'ID', visible: true },
    { key: 'first', label: 'First Name', visible: false }, //modify visible from true to false
    { key: 'last', label: 'Last Name', visible: true },
    { key: 'age', label: 'Age', visible: true },
  ]

What happens is that the First Name checkbox will be unchecked but the First Name column will still appear during page load which is not what I want. If things work according to what I want, then First Name checkbox will be unchecked and the First Name column will not appear after initial page load.

I am using Vue v2.6 and BootstrapVue.

CodePudding user response:

It seems like it's working as expected in the snippet below. All I did was changing the visibility of first and last names' to false.

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: false },
        { key: 'last', label: 'Last Name', visible: false },
        { 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