modify the b-form-checkbox component in a Vue application

I have inserted the <b-form-checkbox> element into a <b-table>:

https://jsfiddle.net/fmattioni/L269rd7s/

This is my current objective:

1 - Initially, all checkboxes in the table are checked using checked="true".

2 - Try unchecking any checkbox as an example.

3 - Change the date on the calendar now.

4 - The issue arises here: after changing the date, I want all checkboxes to revert to their initial states, with all of them being checked.


I am looking to accomplish this through one of the following methods:

  • Option #1 - Is it possible to define within my data items whether a column should be checked or not? I attempted the following:

items: [
        {id: 1, check: true,},
        {id: 2, check: true,},
        {id: 3, check: true,},
      ]

However, I haven't figured out how to instruct <b-form-checkbox> to do so. For instance, I would like to set id: 2 initially as check: false, and the others as check: true. Is this achievable?

  • Option #2 - It would suffice if I could simply reset all values to true once the date changes.

Any suggestions or solutions for this scenario?

Answer №1

Modify the #cell(check)="row" to

#cell(check)="{ item }"

<div id="app">
  <div>
    <b-form-datepicker
    value='2021-04-25'
    >
    </b-form-datepicker>
    <b-table
    :items='items'
    >
      <template #cell(check)="{ item }">
        <b-form-checkbox
        v-model="item.check"                 
        >
        </b-form-checkbox>
      </template>
    </b-table>
  </div>
</div>

This adjustment allows the component to receive the item from the table component. Your #cell slot contains additional data like index, which may be necessary for other uses.

https://jsfiddle.net/eligiv/eumvgp4s/15/

Answer №2

I believe that using watch may be unnecessary as it can add overhead to your code. Instead, you can utilize @input to detect changes and then call the reset method. I have included a code snippet below for better clarity.

new Vue({
  el: "#app",
  data() {
    return {
      date: new Date(),
      items: [{
          id: 1,
          check: true,
        },
        {
          id: 2,
          check: false,
        },
        {
          id: 3,
          check: true,
        },
      ]
    }
  },
  methods: {
    reset_checkboxes() {
      this.items.forEach((el) => {
        el.check = true
      })
    }
  }
});
<link type="text/css" rel="stylesheet" href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="50323f3f24232422312010647e657e63">[email protected]</a>/dist/css/bootstrap.min.css" />

<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6711120227554951495655">[email protected]</a>/dist/vue.min.js"></script>
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8fede0e0fbfcfbfdeeffa2f9faeacfbda1bdbea1bd">[email protected]</a>/dist/bootstrap-vue.min.js"></script>


<div id="app">
  <b-form-datepicker @input="reset_checkboxes" v-model='date'>
  </b-form-datepicker>
  <b-table :items='items'>
    <template #cell(check)="{ item }">
        <b-form-checkbox v-model="item.check" />
    </template>
  </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 we efficiently validate specific fields within arrays and objects in express-validator by utilizing the body() method?

I have organized my field names in an array as follows: const baseFields = [ 'employeeNumber', 'firstName', 'lastName', 'trEmail', 'position' ]; These are the only input fields I need to focus on for valid ...

Attempting to Create a New User and Display the Resulting Page Using JavaScript and Handlebars Templates

I've implemented a form that allows users to input data. https://i.sstatic.net/prh11.png Once the user hits "Add User", they are successfully added to the database. However, instead of transitioning to the next page as expected, the form remains popu ...

Unable to implement str.replace function within HTML code

Within my Angular app, I'm looking to replace all instances of _ within a string. In my controller, the following code achieves the desired outcome: alert("this_is_string_".replace(/_/g, " ")); However, when attempting to implement the same code wit ...

Discover the magic of Material UI's Multiple Select feature for working with arrays

Utilizing the material-ui multiple select feature, I have set up a demo following the guidelines provided in the Multiple Select documentation. You can check out my example here: codesandbox In my demonstration, I am aiming to use 2 arrays for two separa ...

Managing various iterations of identical repositories

Currently in the process of constructing a library of unique react components that are being utilized in various downstream applications. To incorporate these components into my downstream applications, I rely on the package.json. Recently, I began ponde ...

Using a nested loop in Javascript to fetch JSON data

My goal is to display Categories and their corresponding subcategories in a specific order. However, my current method of using loops within loops is not producing the desired outcome: Category(Mobile) Category(Laptop) Subcategory(Iphone4) Subcategory(Iph ...

Changing the color of the timePicker clock in material-ui: a step-by-step guide

I have been attempting to update the color of the time clock in my timeInput component (material-ui-time-picker) for material-ui, but unfortunately, it is not reflecting the change. Here is the code I am using: <TimeInput style ={heure} ...

Is it possible to access the chrome://webrtc-internals/ variables through an API in JavaScript?

I couldn't find any information about how to access the logged variables in chrome://webrtc-internals/ on google. There is not even a description of the graphs available there. I am specifically interested in packetsLost, googCurrentDelayMs, and goo ...

The display function in Javascript has a tendency to work sporadically

I’ve been tasked with creating my own tic tac toe game through coding. While I am relatively new to programming, I have a strong passion for it. At the moment, I've set up a basic function to hide only the "O", leaving only the "X" visible on the gr ...

The Safari keyboard navigation feature suddenly disappeared after uploading to Google App Engine

My current website is built using Vue.js (2.x) and deployed on Google App Engine. After testing the deployed application in Safari, I noticed that the accessibility feature "'skip navigation' on :focus" was no longer functioning proper ...

Incorporating VueJS with a sleek material design aesthetic

Currently, I am in the process of developing a web application using VueJs and am in need of a CSS framework to aid in designing without starting from scratch! After some research, I came across material-design-lite (www.getmdl.io) but unfortunately faced ...

What is the process for conducting linting with nodemon?

Is it possible to utilize nodemon for linting my javascript without the use of build tools like gulp or grunt, in order to fully leverage node and npm? Nodemon's output can be piped. I am interested in using this feature for linting the changed file ...

Learn how to trigger an HTTP exception after a failed command in a saga with NestJS CQRS

Currently utilizing the NestJS CQRS pattern to handle interactions between User and UserProfile entities within my system. The setup consists of an API Gateway NestJS server along with dedicated NestJS servers for each microservice (User, UserProfile, etc. ...

The concept of asynchronicity and callbacks in JavaScript

As a newcomer to the world of JavaScript and Stack Overflow, I have been learning about synchronous, asynchronous, and callbacks through various videos and blogs. However, I still have a lingering doubt. If synchronous code means following a sequential ord ...

Center both vertically and horizontally in Bootstrap UI Modal

I'm attempting to create a Bootstrap UI Modal that is centered both vertically and horizontally. While I found this solution that successfully centers the modal vertically, it loses its horizontal centering when applied to my own template with 800px ...

What is the process for appending a value to an array of JSON objects?

I have a JSON array containing objects which I need to pass the values to the DataTables. [{ _id: '58a2b5941a9dfe3537aad540', Country: 'India', State: 'Andhra Pradesh', District: 'Guntur', Division: ...

Creating smooth animations in JavaScript without relying on external libraries such as jQuery

Is there a way in JavaScript to make a <div> slide in from the right when hovered over, without relying on jQuery or other libraries? I'm looking for a modern browser-friendly solution. const div = document.querySelector('.fro ...

Tips on customizing regex to selectively identify specific strings without capturing unrelated ones

Currently, I am working with this regex: /(?<=^|\/)(?:(?!\/)(?!.*\/))(.*?)[:-]v([\d.-]+)(?=\.|$)/ The goal is to extract the captured group from the following strings: rhmtc/openshift-velero-plugin-rhel8:v1.7.9-4 oc-mirror-plug ...

What is the best way to call a function within another function only when a certain div is not clicked?

Throughout my code, I am utilizing the campusInfo(id) function. This particular function, in turn, calls another function named campusInfo_2(HouseID). One scenario where I am invoking campusInfo(id) is when a specific div is clicked. In this instance, I ...

Exploring the capabilities of jQuery by creating custom functions utilizing the .data method and HTML5 data

My goal is to dynamically add a new "app" to my "AppList" when a button is clicked. Javascript Solution: $(".appCreate" ).click(createNewApp); function createNewApp() { var facebookTemplate = $("#facebook-template").html(); var appName = $(this ...