Search code examples
javascriptvue.jsbootstrap-vue

How to use BootstrapVue's layout and grid system to arrange these checkboxes?


I have a BootstrapVue table like this;

enter image description here

Here's the code for the 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>

I want to use BootstrapVue layout and grid system to arrange the checkboxes.

https://bootstrap-vue.org/docs/components/layout

I want the checkboxes to be placed using BootstrapVue's layout and grid system as shown below;

<b-container class="bv-example-row">
  <b-row class="justify-content-md-center">
    <b-col col lg="12">1 of 3</b-col>
    <b-col cols="12" md="auto">Variable width content</b-col>
    <b-col col lg="2">3 of 3</b-col>
    <b-col col lg="14">3 of 3</b-col>
  </b-row>
</b-container>

What complicates matters is the html code for the checkbox which contains v-for. See below;

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

I am using vue v2.6, BootstrapVue.


Solution

  • It looks tricky, but doable.

    1. Use v-for loop on <b-col> instead of <b-checkbox>
    2. put <b-col> props like col lg=12 as fields property as an object {col:true,lg:12}
    3. use v-bind to user style props to <b-col>

    Here is my attempt in Codepen , I hope that I understood your intention correctly.

    I assume that you are trying to render like below.

        <b-container class="bv-example-row">
          <b-row class="justify-content-md-center">
            <b-col col lg="12">
              <b-checkbox :disabled="visibleFields.length == 1 && fields[0].visible" v-model="fields[0].visible" inline>
                {{ fields[0].label }}
              </b-checkbox>
            </b-col>
            <b-col cols="12" md="auto">
              <b-checkbox :disabled="visibleFields.length == 1 && fields[1].visible" v-model="fields[1].visible" inline>
                {{ fields[1].label }}
              </b-checkbox>
            </b-col>
            <b-col col lg="2">
              <b-checkbox :disabled="visibleFields.length == 1 && fields[2].visible" v-model="fields[2].visible" inline>
                {{ fields[2].label }}
              </b-checkbox>
            </b-col>
            <b-col col lg="14">
              <b-checkbox :disabled="visibleFields.length == 1 && fields[3].visible" v-model="fields[3].visible" inline>
                {{ fields[3].label }}
              </b-checkbox>
            </b-col>
          </b-row>
        </b-container>
    

    Adds new props property to your fields

    fields:[ 
     { key: 'id', label: 'ID', visible: true, props:{ col:true, lg:12}},
     { key: 'first', label: 'First Name', visible: true, props:{cols:12, md:'auto' }},
     { key: 'last', label: 'Last Name', visible: true, props:{ col:true ,lg:2 }},
     { key: 'age', label: 'Age', visible: true, props:{col:true, lg:14} },
    ]
    

    v-for loop with v-bind props

      <div id='app'>
        <b-container class="bv-example-row">
          <b-row class="justify-content-md-center">
            <b-col v-for="(field,index) in fields" :key="index" v-bind="field.props">
              <b-checkbox :disabled="visibleFields.length == 1 && field.visible" v-model="field.visible" inline>
                {{ field.label }}
              </b-checkbox>
            </b-col>    
          </b-row>
        </b-container>
    
        <br /><br />
        <b-table :items="items" :fields="visibleFields" bordered>
        </b-table>
      </div>