Vue looping through nested checkbox groups

Here is an object I am working with:

      rightGroups: [
        {
          name: "Admin Rights",
          id: 1,
          rights: [
            {
              caption: 'Manage Rights',
              name: 'reports',
              right: false,
              id: 1
            },
            {
              caption: 'Right X',
              name: 'admin.rightX',
              right: false,
              id: 2
            },
            {
              caption: 'Right Y',
              name: 'admin.rightY',
              right: false,
              id: 3
            },
            {
              caption: 'Right Z',
              name: 'admin.rightZ',
              right: false,
              id: 4
            }
          ]
        },
        {
          name: "User Management Rights",
          id: 2,
          rights: [
            {
              caption: 'Manage Users',
              name: 'user.recht',
              right: false,
              id: 1
            },
            {
              caption: 'Right X',
              name: 'user.rightX',
              right: false,
              id: 2
            },
            {
              caption: 'Right Y',
              name: 'user.rightY',
              right: false,
              id: 3
            },
            {
              caption: 'Right Z',
              name: 'user.rightZ',
              right: false,
              id: 4
            }
          ]
        }
      ],

I want to display dynamic loaded checkbox groups like in this example: checkbox groups

Each checkbox group should have their own checkboxes loaded only. I am trying to achieve this using v-for in bootstrap-vue as shown below:

    <b-row class="mt-5">
      <b-col
        v-for="group in rightGroups"
        :key="group.id"
        md="6"
      >
        <b-card
          border-variant="dark"
        >
          <template v-slot:header>
            <b-form-checkbox>
              <strong>{{ group.name }}</strong>
            </b-form-checkbox>
          </template>

          <b-form-checkbox
            v-for="right in group"
            :key="right.id"
          >

          </b-form-checkbox>
        </b-card>
      </b-col>

    </b-row>

I only want the rights to be loaded within their respective groups. How can I accomplish this?

I have created a codesandbox for reference: https://codesandbox.io/s/thirsty-snowflake-2q4l0?file=/src/components/Rights.vue

Answer №1

Are you seeking this specific solution? To implement the desired changes, simply insert the following code snippet into your CSB Rights.vue file.

<template>
  <div id="authorization">
    <b-row class="m-5">
      <b-col v-for="group in rightGroups" :key="group.id" md="6" align="start">
        <b-card border-variant="dark">
          <template v-slot:header>
            <b-form-checkbox>
              <strong>{{ group.name }}</strong>
            </b-form-checkbox>
          </template>

          <b-form-checkbox v-for="rights in group.rights" 
          :key="rights.id" 
          v-model="rights.right">{{rights.caption}}</b-form-checkbox>
        </b-card>
      </b-col>
    </b-row>
  </div>
</template>
<script>
export default {
  data() {
    return {
      rightGroups: [
        {
          name: "Rights for Administration",
          id: 1,
          rights: [
            {
              caption: "Manage Rights",
              name: "reports",
              right: true,
              id: 1
            },
            {
              caption: "Right X",
              name: "admin.rightX",
              right: false,
              id: 2
            },
            {
              caption: "Right Y",
              name: "admin.rightY",
              right: false,
              id: 3
            },
            {
              caption: "Right Z",
              name: "admin.rightZ",
              right: false,
              id: 4
            }
          ]
        },
        {
          name: "Rights for User Management",
          id: 2,
          rights: [
            {
              caption: "Manage Users",
              name: "user.right",
              right: false,
              id: 1
            },
            {
              caption: "Right X",
              name: "user.rightX",
              right: false,
              id: 2
            },
            {
              caption: "Right Y",
              name: "user.rightY",
              right: false,
              id: 3
            },
            {
              caption: "Right Z",
              name: "user.rightZ",
              right: false,
              id: 4
            }
          ]
        }
      ]
    };
  }
};
</script>

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

Answer №2

One simple modification to improve the code is by adjusting the second loop as shown below:

<b-form-checkbox v-for="right in group.rights" :key="right.id">{{right.name}}</b-form-checkbox>

This tweak should enhance the functionality of the code.

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

Understanding the response format in jQuery and AJAX

Can anyone advise on the optimal data format for communication with JavaScript? Should I stick to JSON or use plain HTML instead? Are there any alternatives worth considering? ...

JavaScript: The functionality of calling functions through buttons ceases to function once the page is updated without reloading

I am trying to create a program that consists of one HTML page, where I can dynamically update and populate it with different elements using JavaScript. The main feature of the program is a button that remains constant in every version and displays a mod ...

Click Event for Basic CSS Dropdown Menu

My goal is to develop a straightforward feature where a dropdown menu appears below a specific text field once it is clicked. $(".val").children("div").on("click", function() { alert("CLICK"); }); .container { display: inline-block; } .val { d ...

SignOut operation in Express.js Firebase instantly responds with a status of 200 to the client-side Fetch POST request

I'm currently facing an issue with my POST request setup using the Fetch API in my client-side JavaScript. The request is being sent to my server-side JavaScript code which utilizes Express Js and Firebase Auth. The problem arises when the auth().sign ...

Utilizing JavaScript to update the content of a React page

Recently, I came across an API using Swagger for documentation. Swagger generates a ReactJs webpage to document and test the API endpoints. However, my lack of knowledge in React architecture has led to a problem: Prior to accessing any endpoint, an authe ...

To reveal the secret map location, just tap on the button - but remember, this only applies to

I am encountering an issue with duplicating a card and toggling the hidden location map. Currently, the location button only works for the first card and not for the rest. I need each card to independently open and hide the location map when clicked on the ...

Add an element to the jQuery collection before the last element, not at the end

My challenge lies in utilizing AJAX to post a comment. However, the last comment element features a submit button within it. Consequently, whenever a new item is appended, it appears after the submit button. <div class="commentContainer" > < ...

Express Angular Node Template Render throwing an error: module 'html' not found

I am currently in the process of creating a web application using AngularJS with ui-router for routing via $stateProvider, ensuring that only the specified states are displayed in the ui-view. In my server.js file, I have set up an initial framework such ...

Creating objects and their relationships from a JSON request in Spring: A guide

My issue involves POSTing a JSON request to a Spring 3.0 controller with the following method signature... @RequestMapping(value="/add", method=RequestMethod.POST) public @ResponseBody Map<String, ? extends Object> add(@RequestBody Entry) The JSON ...

org.codehaus.jackson.JsonParseException: An unexpected character ("/" (code 47)) appeared

I have a file with a customer list stored as a HashMap in json format. Here is an example: {"John":{"name":"John","id":"12345", "email":"john@test.com","phone":"555-555-5555"}} This represents just one customer from the list. When the controller is ...

How can I retrieve information on a logged in Auth0 user from an API?

I'm currently working on a React application that utilizes auth0 in conjunction with an express API server. One issue I'm facing is how to access user information within the API when a secure endpoint is called. While I can retrieve user data on ...

Transmit information to the controller using jQuery in a C# MVC environment

Here is my jQuery script code snippet. The script works perfectly and stores the data array/object in a variable called dataBLL. var dataBLL = []; $('#mytable tr').each(function (i) { dataBLL.push({ id: $(this).find('td:eq(0)').text( ...

HTML5 canvas processing causing web worker to run out of memory

Within the Main thread: The source image array is obtained using the getImageData method. It is represented as a uint8ClampedArray to store the image data. Below is the code executed in a web worker: (This operation generates a high-resolution image, but ...

Clicking on the React Bootstrap Checkbox within the Nav component does not trigger a rerender of the NavItem component

Encountering an unusual issue while using a Nav and NavItem with a Checkbox from React Bootstrap. What I've noticed is that when clicking directly on the checkbox instead of the NavItem button, the checkbox does not re-render correctly even though my ...

Issue with receiving output from tessearct.js in PDF format

I am currently working on a basic javaScript OCR (Optical Character Recognition) application using tesseract.js. I have included {tess_create_pdf: "1"} in the .recognize() method to receive the result in PDF format, but it seems to be malfunctioning. Can ...

Guide on transferring information obtained from a service to an Angular datatable

Recently, I started working on Angular 4 and encountered an issue while trying to display data from an Angular service in JSON format using angular-datatable. Despite trying various options, I am unable to get the data to display within the columns of the ...

What techniques can be used to avoid blinking while forcefully scrolling to the left?

In my previous inquiry about dynamically adding and removing divs on scroll, I was unable to find a satisfactory solution among the responses provided. However, I decided to take matters into my own hands and attempted to implement it myself. My approach ...

Tips for Implementing a "Please Hold On" Progress Bar in ASP.NET

I have a master page named siteMaster.master, an aspx page called submission.aspx, and a user control named attachment.ascx. The script manager is included in my master page. The submission page inherits the master page and registers the user control attac ...

Transform the javascript ES6 class into a functional programming approach

I am interested in converting my reactjs class to a functional programming approach rather than using OOP. Can anyone provide guidance on how to achieve this? Please refer to my code snippet below. import * as h from './hydraulic'; const vertic ...

"Working with MongoDB JSON Objects in C# using Servicestack: Escaping the Un

I am facing two issues. Currently, I have a local MongoDB setup with multiple collections. The structure of my DataBase Object can be viewed at this link: https://i.stack.imgur.com/sVKxI.png My Configuration is as follows: ​using Funq; using ServiceS ...