"Exploring the relationship between Vue checkbox components: parent and

Currently, I am working on a checkbox group where the behavior is such that if the parent checkbox is checked, all the child checkboxes will also be checked. Similarly, if a child checkbox is checked, the parent checkbox should also get checked, and so on.

Below is a Vue template code snippet demonstrating this functionality:


<div class="row">
    <div class="col-12">
        <b-tabs content-class="mt-2" @click="myId(value.parentId)">
            <div v-for="title in testpTitle" :key="title.id">
                <b-tab :title="title.name">
                    <div class="md-tab-content">
                        <div v-for="value in testp" :key="value.id">
                            <div class="form-check" v-if="value.parentId !== 0 && 
                            //value.parentId == title.id &&
                             value.parentId != 62">
                                <input class="form-check-input" @click="checkIfItHasChildren(value.id)"
                                    :id="value.id"
                                    type="checkbox"
                                    :value="value.id"
                                    :disabled="value.isDisabled"
                                    v-model="value.isChecked"

                                />{{value.name + "  " + value.id + "   " + value.parentId}} 

                            </div> 
                        </div>
                    </div>
                </b-tab>
            </div>

        </b-tabs>
        <div class="float-right">
            <button type="button" class="btn btn-success" @click="saveData()">SAVE</button>
            <button type="button" class="btn btn-danger" @click="returnToRoles()">CANCEL</button>
        </div>
    </div>

Script file written in TypeScript:

<script lang="ts">
@Component({
    components: {
    }
})

export default class RolePermission extends Vue { 
        
    testp: PermissionModel[] = [];
    testpTitle: PermissionModel[] = [];

    $refs!: {
        rolePermissionList: DataTable
        permissionList: DataTable
    };

    async mounted(){
        this.dashboard = await permissionService.getDashboard();
    }

    checkIfItHasChildren(id){
        let i = 0;
        var array = this.testp;

        array.forEach(element => {
            if(element.parentId == id)
            {
                array[i].isChecked = element[id].isChecked;
            }
            i++;
        });
    }
    
    async clicked(id) {
        this.openedDatatable=1;

        this.testp = await permissionService.getPermissions(id););
        this.testpTitle = await permissionService.getPermissionsTitle();
    }

Embedded below is a picture link showcasing a checkbox list with its respective name, id, and parentId: [Image Description][1] [1]: https://i.sstatic.net/weMNu.png

I'm encountering the following errors: 1. "vue.esm.js?a026:628 [Vue warn]: Error in v-on handler: "TypeError: Cannot read properties of undefined (reading 'isChecked')"" 2. "TypeError: Cannot read properties of undefined (reading 'isChecked')

Answer №1

Try using element.isChecked; in place of element[id].isChecked; to resolve the issue

An error occurred: TypeError - Unable to access properties of undefined (specifically 'isChecked')

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

Having trouble with the installation of go get wails?

Attempting to implement golang and wails, but encountering issues after executing the following command: go get github.com/wailsapp/wails/cmd/wails Resulting in errors such as: ../../github.com/wailsapp/wails/cmd/semver.go:21:3: cannot use semverVers ...

Access exclusive content by subscribing now!

How can I return a reference to a subject from a service without allowing the receiver to call .next() on the subject? Let's say there is a service with a subject that triggers new events. class ExampleService { private exampleSubject = new Subjec ...

Issue with ResponseCache functionality in .NET Core 3.1 is causing unexpected behavior

I attempted to delve into Response Caching within the realm of .NET Core 3.1, only to find that my efforts did not yield the desired outcome. Upon inspecting the network via Chrome devtools, I observed the response header specifying cache-control: no-cache ...

What is the best way to access form data in React using a React.FormEvent<HTMLFormElement>?

I am looking for a way to retrieve the values entered in a <form> element when a user submits the form. I want to avoid using an onChange listener and storing the values in the state. Is there a different approach I can take? login.tsx ... interfa ...

In the template, I utilize both vue-router and jQuery. However, there seems to be an issue with $(document).ready() not functioning

Looking for guidance on using jQuery in Vue. I've noticed that when I refresh the page, $(document).ready() function is triggered. However, when I use vue-router to switch pages, $(document).ready() does not seem to work. <template> <div ...

Tips for effectively managing index positions within a dual ngFor loop in Angular

I'm working on a feedback form that includes multiple questions with the same set of multiple choice answers. Here's how I've set it up: options: string[] = ['Excellent', 'Fair', 'Good', 'Poor']; q ...

Utilizing the power of d3.js within Angular 4

Currently, I have successfully implemented code to draw a polygon using the mouse in a normal JavaScript file. Now, I am looking to replicate the same functionality in my TypeScript file. Below is an excerpt from my d3.js file: //D3.JS VERSION 3 //------ ...

How about setting variables in Vue router as an alternative to controlling component visibility?

Utilizing URL parameters to set variables in my Vue application is a goal of mine. While I initially thought vue-router would be the perfect solution, it appears to be more catered towards handling the showing and hiding of components within a router-view ...

Angular's observable data fail to show on the screen

I am facing an issue with passing the data of an Observable fetched via an API request to a component variable for display. I have been trying but unable to make it work successfully. Any assistance would be greatly appreciated. Here is my code. Thank you. ...

Why does updating state lead to a re-rendering loop while setting state does not encounter the same issue?

Essentially, I have developed a REST Api that retrieves a list of components (sections) from a CMS along with their associated data. Each section in the UI is assigned a number to indicate its position, but certain sections are excluded from counting, such ...

Avoid production build warnings in Vue.js without the need to build the code

Experimenting with vuejs, I decided to use it for a simple page even though I could have achieved the same without any framework. Now, my project is nearly production ready. The only issue is that it's just a single js and html file, but it shows thi ...

Generate random entries from an object based on specific criteria and append them to a fresh object using Typescript

Experimenting with Ionic2 and Typescript has been my recent focus. I have an object that contains various meals, calorie counts, and meal types (such as vegan). This is how the object looks: [ { "id":14093, "name":"Proteinshake mit Wasser ...

Harness the power of TypeScript in a single test file with jest's expect.extend() method

This question is similar to Can you limit the scope of a TypeScript global type? but it presents a slightly different scenario that requires clarification (although an answer to this would be greatly appreciated as well). In my Jest setup, I am attempting ...

Exploring Angular 2/4: Unpacking the Process of Accessing API Data Using Tokens

Hello there, I am trying to retrieve API data with a token using Angular 2/4. Below is the code I have written: import { Component, ViewEncapsulation } from '@angular/core'; import { Http, Response } from '@angular/http'; import &apos ...

Using Angular 5 with Typescript to generate and return an array of freshly instantiated typed objects

My backend service provides me with "moments," and I have two functions to handle this data. One is a get() method that returns a single object, and the other is a search() method that returns an array of objects. moment.service.ts The get method success ...

Error in Typescript: A computed property name must be one of the types 'string', 'number', 'symbol', or 'any'

Here is the current code I am working with: interface sizes { [key: string]: Partial<CSSStyleDeclaration>[]; } export const useStyleBlocks = ( resolution = 'large', blocks = [{}] ): Partial<CSSStyleDeclaration>[] => { cons ...

Is SSG the best approach for deploying a Nuxt.js dashboard?

We have plans to create an internal dashboard utilizing Nuxt.js. Since this dashboard will be used internally, there is no requirement for a universal mode. Typically, most dashboards are designed as SPAs (Single Page Applications). However, SPAs still ne ...

Stopping Vue from endlessly looping

There are three fields to consider: hour, minutes, and total. If the hour or minutes change, I want to calculate the total. If the total is changed, I want to calculate the corresponding minutes and hours. For example: 1h 30minutes = 1.5 Total 2.25 To ...

TypeScript versions 2.3 and 2.4 experiencing issues with generic overloads

After upgrading from typescript 2.2, I encountered an issue with the following example. interface ILayoutResult { id: string; data: any; } interface ILayout{ getResult<T extends ILayoutResult | ILayoutResult[] | void>() :T; } class te ...

Preserve final variable state - Angular

My function looks like this: flag: boolean = false; some_function(){ var foo = some_num_value; var bar = foo; // Storing value in a separate variable if(this.flag){ v ...