Vue.js v-model causing array to be overridden

Currently, I am working with select boxes for a permissions module in Vuejs 2 with Laravel. The structure of the select boxes is quite complex. However, I am facing an issue where binding my nested array item in v-model is treating it as a string. Every time I check a box, it ends up overwriting the variable.

The model that is being overwritten is workspace.selectedOperations.

Below is the HTML code:

<b-tab v-for="workspace in addedWorkspaces" :key="workspace.id" :title="workspace.title" active>
    <div class="roles-permissions">
        <label>Permissions</label>
        <div class="permissions-path">
            <ul class="pl-0">
                <li v-for="entity in workspace.entities">
                    <b-form-checkbox>{{entity.title}}</b-form-checkbox>
                    <ul>
                        <li v-for="operation in entity.operations">{{workspace.selectedOperations}}
                            <b-form-checkbox v-model="workspace.selectedOperations" :value="operation.id">{{operation.title}}</b-form-checkbox>
                        </li>
                    </ul>
                </li>
            </ul>
        </div>
    </div>
</b-tab>

And here is the script section:

<script>
import Multiselect from 'vue-multiselect'
export default {
    props: ['showModalProp'],
    data() {
        return {
            title: '',
            value: [],
            entities: [],
            addedWorkspaces: [],
            selectedWorkspaces: ''
        }
    },
    methods: {
        getOperationsList(){
            this.$http.get('entity').then(response=>{
                response = response.body.response;
                this.entities = response.data;
            });
        },
        showModal() {
            this.$refs.myModalRef.show()
        },
        hideModal() {
            this.$refs.myModalRef.hide()
        },
        onHidden(){
            this.$emit('HideModalValue');
        },
        addWorkspaces(){
            this.addedWorkspaces = this.selectedWorkspaces;
            this.selectedWorkspaces = [];
            for (var i = this.addedWorkspaces.length - 1; i >= 0; i--) {
                this.addedWorkspaces[i].entities = 
                JSON.parse(JSON.stringify(this.entities));
                this.addedWorkspaces[i].selectedOperations = [];
            }
        }        
    },
    mounted(){
        this.getOperationsList();
    },
    components: {
        Multiselect
    },    
    watch:{
        showModalProp(value){

            if(value){
                this.showModal();
            }
            if(!value){
                this.hideModal();
            }

        }
    },
    computed:{
        options(){
            return this.$store.getters.getWorkspaces;
        }
    }
}  
</script>

Answer №1

To utilize an array as the v-model for b-form-checkbox, it is recommended to enclose your b-form-checkbox within a b-form-checkbox-group and set the v-model on it https://i.sstatic.net/3fLvs.png

For more information, visit

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

Accessing content from a text file and showcasing a designated line

Apologies if my wording was unclear... In this scenario, we will refer to the text file as example.txt. The value in question will be labeled as "Apple" My goal is to retrieve example.txt, search for Apple within it, and display the entire line where thi ...

Make sure the "Treat labels as text" option is set to true when creating a chart in a Google spreadsheet using a script

I am currently working on a script using Google Spreadsheet Apps Script interface and I need to set the marker for 'Treat labels as text' to true. Despite searching through App Script documentation, I couldn't find any specific mention of t ...

Invoking the callback function within the containing scope in Typescript

I am facing an issue with my Angular component where I have a class that includes common services and functions. While passing some functions as callbacks, the scope is getting lost during execution. Let me demonstrate the problem through the code below: @ ...

"Troubangular: Troubleshooting unexpected behavior when trying to update ngFor after setting a property

Dealing with what seems like a straightforward component here. I'm fetching an array of objects from an API, storing them in a property, and then displaying them in a select list for the user to choose from. When the value changes, I filter the result ...

Learn how to define matching properties in an enum and object in React using Typescript

Here is an enum I have defined: enum INFO_CODES { goToInit = 'goToInit', lockIp = 'lockIp', } I am looking to create an object (or another enum) with the same properties, which are only goToInit and lockIp: const SOME_OBJ = { goToInit ...

What is the best way to utilize regex for replacing a string within Node.js environment?

I'm struggling with updating the string in my code. Essentially, I have a .php file and I want to modify some constants using the replace package. The output of my current code is: // Current Output define('DB_NAME', 'foo'); // ...

Struggling to make vue-i18n function properly with nuxt generate?

In an effort to enhance my skills, I am creating a small Nuxt project from scratch. While the project runs smoothly on the local server, I have encountered an issue with the language switcher not updating the translation fields when using nuxt generate. U ...

What could be the reason behind the validation error occurring in this Laravel 8 and Vue 3 application?

Currently, I am developing a registration form with a Laravel 8 API and integrating it with a Vue 3 front-end. In the AuthController, I have implemented the following code snippet for user registration: ... On the front-end side, my setup looks like this ...

Replace default global variables in contemporary web browsers

After reading the John Resig article on overriding globals in the browser, I attempted to replicate the code in my own browser. However, to my surprise, nothing happened when I ran the example. Here is the code snippet: var sec = {}; function Array() { ...

NServicebus JavaScript message handler: Enhancing Communication

I am in the process of developing a JavaScript event subscriber for NServicebus, and I am seeking feedback on my approach as well as any potential pitfalls to watch out for in this design. My proposed components are as follows: ASP.NET MVC BusControll ...

How does the $watch method detect a change in value from the backend when the digest loop is invoked?

Can you provide an explanation of how the digest cycle functions in the backend of AngularJS, specifically in relation to $watch? How does $watch manage a new value versus an old one? During the execution of the digest loop, how is $watch able to detect ...

Error: Connection to server at [undefined:27017] unsuccessful due to glitch in system

Currently, I am following the curriculum of FreeCodeCamp, focusing on backend development. Working on this project requires forking it in either Glitch or GitHub. To start this project on Glitch, use this link or clone the repository on GitHub. Don&apo ...

What is the best way to pause function execution until a user action is completed within a separate Modal?

I'm currently working on a drink tracking application. Users have the ability to add drinks, but there is also a drink limit feature in place to alert them when they reach their set limit. A modal will pop up with options to cancel or continue adding ...

Bootstrap modal assistance does not function properly when used together with ajax requests

I'm experiencing an issue with a small class project where the data is not being inserted into the database. I collect the data using a bootstrap modal and pass it to the php file using ajax. The problem seems to be occurring when executing the script ...

Strings that automatically adjust to fit the content from a JSON object

I have a JSON object with a long string that I want to display. The string looks like this: "String":"<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever si ...

Positioning a toggleable div in relation to its trigger

Struggling to implement Jquery functionality to show/hide a div at the same height as the trigger button. Trying to offset the position of the show/hide div is proving tricky due to varying footnotes positioning. Considering enclosing divs within another d ...

Using Jquery to show items in a nested loop

I have a requirement to showcase items owned by different customers. To achieve this, I utilize an ajax call to fetch the data and then group it based on individual customer identities. Subsequently, I append the grouped data to my HTML structure. The for ...

Utilizing JSON information acquired through AJAX within a distinct function

Sorry if this question has been asked before, I tried following suggestions from another post but they didn't work for me. What I'm trying to do is fetch some JSON data, save a part of it in a variable, and then use that variable in a different f ...

Guide: Initiating an action in NuxtJs

I am attempting to trigger an action in my Vue component from my Vuex store. Below is the content of my aliments.js file in the store: import Vue from 'vue'; import Vuex from 'vuex'; import axios from 'axios'; Vue.use(Vuex, ...

Retrieving a database value in Node.js Firestore containing a space

Hello, I'm just getting started with node Js and I anticipate that it will be a smooth ride! Currently working on an application using node JS with Firestore where I need to retrieve data like "display name": James and "Age": 22 Since Age does not h ...