What is the best way to organize these checkboxes using BootstrapVue's layout and grid system?

My BootstrapVue table setup looks like this:

https://i.sstatic.net/K3Rwy.png

This is 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/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82e0ededf6f1f6f0e3f2c2b6acb6acb3">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5b3934342f282f293a2b762d2e3e1b6975697569">[email protected]</a>/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/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cdafa2a2b9beb9bfacbde0bbb8a88dffe3ffe3ff">[email protected]</a>/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 aim to utilize BootstrapVue layout and grid system to organize the checkboxes.

The goal is to position the checkboxes using BootstrapVue's layout and grid system depicted 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>

An additional challenge arises from the html code for the checkbox which includes v-for. Look at the example 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 working with vue v2.6, BootstrapVue.

Answer №1

While it may seem complex, it is achievable.

  1. Instead of using <b-checkbox>, implement a v-for loop on <b-col>
  2. Assign the props of <b-col> like col lg=12 to the fields property as an object {col:true,lg:12}
  3. Utilize v-bind to apply style props to <b-col>

You can view my attempt on Codepen. Hopefully, I have correctly interpreted your intentions.

I presume that you are aiming to render something similar to the following:

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

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can a button be linked directly to a particular list item?

I currently have a HTML tag within my React application that looks something like this: <ul> <li> Item 1 <button> Delete </button> </li> <li> Item 2 <button> ...

Add rows to the table dynamically and then execute the script

My table dynamically creates rows when a button is clicked, and there is an input box with an auto suggest script. The auto complete works fine on the default first box, but not on the dynamically added row. How can I make the auto complete script work o ...

The function of style.marginRight differs from that of style.marginLeft

One function aligns content to the left while the other doesn't align it to the right function openNavLeft() { document.getElementById("mySidenavLeft").style.width = "100vw"; document.getElementById("main").style.marginLeft = "100vw"; } function ...

Sending data from Laravel Blade to Vue component resulting in repetitive content

I have encountered an issue where I am passing 6 routes from a blade file to a Vue component, but when I do so and output the routes, the same 6 routes appear multiple times in groups of 6. Each group of routes is displayed as its own separate component in ...

Steps to insert a personalized attribute into a TypeScript interface

UPDATED EXPLANATION: I'm fairly new to TypeScript, so please bear with me if this question seems basic. I'm working with an existing library (ngx-logger) that I don't want to or can't modify. My goal is to create a service that generat ...

I can't figure out why I'm receiving undefined even though all the variables are populated with the necessary

I have been working on a project that involves implementing email and password authentication using Firebase. However, I encountered an error when the user submits their credentials: FirebaseError: Firebase: Error (auth/admin-restricted-operation). at ...

Calculating the total of n natural numbers by utilizing a for loop

I have a question that involves calculating the sum of ten natural numbers, but for some reason the code provided is not working as expected. <!DOCTYPE html> <html> <head> <title>Sum of first 10 Natural numbers</title> & ...

Is there a way to showcase the data of each table row within the tr tag in an Angular 8 application?

I have been developing an application using Angular 8. The employee-list component is responsible for presenting data in a table format. Within the employee-list.component.ts file, I have defined: import { Component } from '@angular/core'; impo ...

Error encountered: The Bootstrap Carousel function is causing a TypeError as e[g] is not defined

I am attempting to build a dynamic bootstrap carousel using my json data, and I have implemented jQuery-Template for rendering. Essentially, I am generating the carousel slider on-the-fly from my json data with the help of jQuery-Template. Here is a snippe ...

Securing pathways and pages using NextJs

I recently completed a project where I implemented route protection for a website using the if and else statements, assigning each page a function withAuth(). However, I have concerns about whether this is the most effective method for securing routes in n ...

Error: Trying to modify a property that is set as read-only while attempting to override the toString() function

I have a specific object that includes an instance variable holding a collection of other objects. Right now, my goal is to enhance this list of elements by adding a customized toString() method (which each Element already possesses). I experimented with t ...

switching the content of an element using Javascript

I'd like to switch between two icons when clicking on .switch and apply the style of .nightTextNight to .nightText, my JavaScript code is working well everywhere except here. Is there a simpler way to achieve this? I currently have to create two clas ...

Customize the appearance of a list item using its attributes in Vuejs

As a newcomer to Vue.js, I hope you can overlook my lack of experience with this. My goal is to customize the appearance of an item based on whether its name matches another object. <template> <div id="subMenuWrapper"> <ul id ...

React not functioning properly when packaged with Webpack

I tried implementing React in the simplest way possible, but I am facing some issues. After bundling React and opening the index.html page, it shows up completely blank with no console errors to indicate any mistakes. Below is my webpack.config.js: const ...

Updating the image sources of a group of image tags with a predetermined list

Looking to update a series of image source references within a specific div tag. For example: <!-- language-all: lang-html --> <div id="Listofimages"> <img src="images\2page_img_3.jpg"> <img src="images\2page_img_3 ...

The table refuses to load

I've been troubleshooting this issue for the past two days. The array is visible in the console, but it refuses to show up on the table. I've tried multiple approaches, but none seem to work. I suspect that "tobodyHtml" is not defined properly, a ...

Issue with React Hot Toast not displaying properly due to being positioned behind the <dialog>

The Challenge of Toast Notifications Visibility with <dialog> Element tl;dr When utilizing the native dialog.showModal() function, the <dialog> element appears to consistently remain on top, which causes toast notifications to be obscured by ...

Transferring data from JavaScript variables to PHP function through AJAX requests

Within my dashboard.php, there is a JavaScript function triggered by a button click event named getTeamMembers. This function receives values from the user's interaction and passes them to a PHP function also present in dashboard.php. Despite my effo ...

Detecting When a User Reaches the Bottom of the Screen in REACTjs

My goal is to monitor when the user reaches the bottom of the screen and then trigger a function to load more data. Within my app.js file, I have a Products component that handles the data fetching. The Products component is enclosed in a div with a class ...

Angular JS may encounter a cross domain problem when attempting to post on a third-party website

HTML Code: <div ng-app="myApp" ng-controller="myController"> <div> <input type="button" data-ng-click="submit()" ...