Creating categories in AngularJS using an array

<table class="table-striped table-bordered table-condensed">
    <tbody>
        <tr ng-if="$index%3==0" ng-repeat="permission in vm.parent.getAllPermissions()">
            <td ng-repeat="i in [0,1,2]" class="col-xs-2">
                <span>
                <input type="checkbox" checklist-model="vm.data.permissions" checklist-value="vm.parent.getPermission($parent.$index+i)"> 
                {{vm.parent.getPermissionTitle($parent.$index+i) || " "}}
                </span>
            </td>
        </tr>
    </tbody>
</table>

Currently, this is the AngularJS code I am working on. The JSON data structure I am dealing with includes various users, categories, and permissions:

{
  "Admin": {
    "category": "admin",
    "permission": "admin"
  },
  "user": {
    "category": "user",
    "permission": "user"
  }
}

The vm.getAllPermissions() function retrieves all the data. My goal is to categorize the users and permissions by displaying the categories within ui-tab components, and listing the permissions under each tab in a table format.

I attempted the following approach:

var permissionArray = [];
Object.keys(vm.getAllPermissions).map(function(category, key) {

for(var key in vm.getAllPermissions) {
    if(vm.getAllPermissions.hasOwnProperty(key)) {
        permissionArray.push(category,key);
    }
}

});

While the code snippet above doesn't work as intended, it outlines my objective. The desired layout is depicted in this mockup: https://i.sstatic.net/6mkzx.png

The expected outcome includes an "admin" category tab at the top, with a list of permissions displayed as checkboxes within the tab. To achieve this, I believe passing the category as a parameter to vm.parent.getAllPermissions is necessary. However, my proficiency in Angular and JavaScript is limited, which is why I am seeking guidance on this matter.

Answer №1

To properly handle the values, you should push one value at a time by encapsulating it in an object. Modify your code as follows by adding curly braces:

permissionArray.push({category, key});

For a demonstration, refer to this example: https://jsfiddle.net/yh0ugp6q/1/

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

Retrieving information from Mongodb using Angularjs

Although my background is in a LAMP stack, my interest has recently been piqued by the Node.js/Angluarjs combo. I now want to incorporate Mongodb into the mix, but I'm struggling to set it up. Every tutorial I come across seems to use a different stac ...

File is indicating a status of 200 ok, however it is not being displayed on the screen (node.js, expressjs)

I'm trying to display a video file in the browser and access it like an API on my front end. My goal is to have my front end call the video using a simple <video> tag. <video> <source ="video/randomfile.mov" type="video/mov"> < ...

Passing variables from ExpressJS to JavaScript can be a seamless process

I am struggling with this issue; I am utilizing NodeJS to retrieve a JSON and I must transfer the variable to my page for use by JavaScript. app.get('/test', function(req, res) { res.render('testPage', { myVar: 'My Dat ...

Switch off JavaScript beyond the parent element

Struggling with implementing an event to toggle a div using an element located outside of the parent container. I am attempting to achieve the same functionality by targeting elements beyond the parent structure utilizing a span tag. Any assistance on th ...

retrieving the current value of a variable from a jQuery function

I've done my best to keep things simple. Here's the HTML code I've put together: <div id="outsideCounter"><p></p></div> <div id="clickToAdd"><p>Click me</p></div> <div id="in ...

Using JQuery to Iterate Through All Form Inputs

I am attempting to retrieve the values of all input fields from a form using JQuery and store them in an array to be sent via AJAX with a GET request. My initial approach did not yield the desired results: function gatherFormData(){ $('#formId i ...

What is causing the malfunction in this straightforward attrTween demonstration?

Seeking to grasp the concept of attrTween, I am exploring how to make a square move using this method instead of the simpler attr approach. Despite no errors being displayed in the console, the following example does not produce any visible results, leavin ...

Is this the proper formatting for JavaScript code?

Having trouble changing the CSS of elements that match b-video > p with an embed element using JQuery. Here's my code: $('div.b-video > p').has('embed').attr('style','display:block;'); Can anyone help me ...

Tips for integrating React into a jQuery-centric web application?

After diving into React, I'm eager to showcase my skills by implementing it on select pages as a proof-of-concept for my supervisor at work. Can anyone guide me on how to achieve this using traditional script tags instead of imports? So far, I'v ...

Encountering Datepicker Issue in Your Angularjs App?

I am currently working on a web application using Angular JS and I encountered an error when trying to incorporate a date picker. The error message displayed is "elem.datepicker is not a function" To implement the datepicker, I found reference code in thi ...

Express functions properly when handling the root route, but encounters issues with sub routes as it sends empty response bodies

Inside the routes.ts file: const router:Router = express.Router() // Route to get all blogs router.get('/',async (req:Request,res:Response)=>{ res.status(200).send("message sent") }) router.get('/admin',async (req:Requ ...

Experiencing difficulties when establishing a connection between MongoDB and Node.js

I'm encountering an issue when attempting to connect MongoDB and Node.js on my local server. Can anyone assist me with solving this problem? Below is my JavaScript code: const mongodb = require("mongodb"); // Provide the function to connect to the d ...

Ways to easily modify images using a single button with the help of jq

I am new to programming and eager to learn... Currently, I am facing the following problem: I would like to create a functionality where three images can be changed using one button and incorporating fadeIn and fadeOut animations. Essentially, all images ...

Tips for enhancing the clarity of elements in KineticJS

I'm new to using Kinetic JS, and while I think it's a great library, I'm having trouble with the resolution of the elements. I know that Kinetic JS doesn't use SVG, but the resolution is not up to par. I'm creating circles and othe ...

"Integrating Vuetify into a single-spa Vue application: Step-by-step

vue 2.6.14 vuetify 2.6.9 What is the process for integrating vuetify into a vue application? I am attempting to import my project into another project as a microfrontend application, but encountering issues. Here are the steps I followed: Create-single- ...

Manipulate the ng-model value within a complex array structure

Currently, I am involved in a project where I am adding dynamic form attributes to the parent element. Here is an example of what the data looks like: $scope.uploadedFiles = [{ name: sample.jpg, attr: [{ id: "caption", display: "Titl ...

Using JavaScript to adjust the width of the table header

I have a table that I need to adjust the width of using JavaScript or jQuery. <div class="dataTables_scrollHeadInner" > <table class="table table-condensed table-bordered table-striped dataTable no-footer" > <thead ...

What is the process to subscribe and obtain data from a server-to-user channel using pusher-js?

I am currently hosting my application using next.js on Vercel. I want to integrate Pusher to provide real-time messages to users in a private and secure manner. Despite successful log entries, I am facing challenges in subscribing to the channel and retrie ...

Parsing JSON data in JavaScript to extract multiple records and store them in individual variables in memory

Although I have utilized JSON stringify and JSON Parse before, I am currently struggling to split up my JSON result into multiple variables. Challenge: My JSON result contains multiple sections or pages, and my objective is to store each of these results ...

Error in TypeScript detected for an undefined value that was previously verified

I have developed a function that can add an item to an array or update an item at a specific index if provided. Utilizing TypeScript, I have encountered a peculiar behavior that is puzzling me. Here is the Playground Link. This simple TypeScript functio ...