Displaying Hierarchical Data with AngularJS and ng-repeat

Recently, I've been working on finding an elegant solution to represent hierarchical data in my application. Specifically, I have implemented a slide effect for bootstrap badges that showcase sub-categories using AngularJS.

With some guidance from the community on stackoverflow, I managed to create an example that functions well for parent and child data structures. However, I now require support for a new category type that allows elements to be both parents and children simultaneously - a necessity for browsing a directory structure.

  <ul>
    <li ng-repeat="category in categories"
        ng-init="isChild = category.category_type == 'child'"
        ng-show="category.category_show"
        class="badge-slider">

      <span class="badge {{ isChild ? 'badge-c' : 'badge-p' }}"
            ng-click="isChild || updateResults(category)"
            ng-bind="category.category_name">
      </span>

    </li>
  </ul>

I am currently grappling with how to modify the code to accommodate the new 'parent and child' category type effectively. The existing implementation heavily relies on the boolean value 'isChild' and the ternary operator, which will not suffice for this additional category.

If anyone has any suggestions or ideas, please share them!

For reference, here is a PLNKR link demonstrating the sliding feature working for simple parent-child relationships: http://plnkr.co/edit/9CiXW1YAoPj80x6PtBW3

EDIT 1:

I also put together another PLNKR showcasing the functionality for a three-tier hierarchical relationship. While it works smoothly, it struggles to display 'parent and child' elements with the corresponding badge.... http://plnkr.co/edit/YoRI578GHE91t6torCUt

Answer №1

After some tinkering, I managed to crack this issue:

  1. Adjusted the categories data structure
  2. Implemented an inline angular template in the HTML file
  3. Developed a new CSS class for "parent and child" elements

If you're interested, you can check out the working solution on Plunker

Here's a glimpse of the final outcome: https://i.sstatic.net/ibV8w.jpg

Answer №2

Your current data structure lacks hierarchy. I recommend creating a new structure for your list of categories, where each element in an array includes a "children" property with nested arrays.

$scope.categories = [
  {category_name:"apples", children: [
    {category_name:"bramble", children: [
      {category_name:"red"},
      {category_name:"green"}
    ]},
    {category_name:"granny smiths"},
    {category_name:"pink lady"}
  ]},
  {category_name:"oranges", children: [
    {category_name:"satsuma"},
    {category_name:"nectarine"},
    {category_name:"mandarin"}
  ]}
];

Check out this plunkr example that demonstrates an inefficient way to represent the data (using templates and directives would be more effective for recursive structures).

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

Elements not being styled by CSS properties

I am facing an issue with a CSS file designed to theme a page in white on black, as opposed to the usual black on white. body { background-color: black; } h1 h2 h3 h4 h5 h6 p { color: white; } However, the color: white; property is not being applied ...

Step-by-step guide on achieving a radiant glow effect using React Native

I am looking to add a glowing animation effect to both my button and image elements in React Native. Is there a specific animation technique or library that can help achieve this effect? While I have this CSS style for the glow effect, I am uncertain if ...

How come my Calendar is not showing up on the browser?

I came across a helpful guide on setting up a Calendar in HTML using JavaScript You can find it here: Unfortunately, the code I tried to use based on that guide isn't functioning as expected. <div class="row"> <div class="col-lg-4 text ...

Creating a Jira-inspired table layout in Angular by embedding components in a structured format

I'm attempting to recreate the functionality of Jira where I can create a "gadget" that can be added to a board, resized, moved, and deleted. (For those unfamiliar with why this might be useful, think of gadgets like the "Gantt" gadget. You can add a ...

Send back alternate HTML content if the request is not made via AJAX

Last time I asked this question, I received several negative responses. This time, I will try to be more clear. Here is the structure of a website: Mainpage (Containing all resources and scripts) All Other pages (HTML only consists of elements of that ...

We apologize, but the module you are looking for cannot be found: Unable to locate 'fs'

Trying to create a new MDX blog website using Next.js 13 with the latest app router, but encountering an error message saying "Module not found: Can't resolve 'fs'". It was working fine in Next.js 12 and pages directory, but not in the lates ...

Transferring PHP data to JQuery through HTML elements

When adding a new comment to the list using PHP variables containing HTML code, the method of choice is typically through JQuery. The question arises - what is the most effective way to achieve this? Would one generally opt for an ajax call? Here's t ...

Using gradient colors for the background on the ::selection pseudo-element

Can you apply a gradient color to CSS ::selection? I tried using the following code: ::selection { background-image: -webkit-linear-gradient(left, #ee4037 0%, #eb297b 100%); } However, it did not have the desired effect. ...

Error: Scheme is not a valid function call

Currently, I am attempting to implement user registration functionality in a Node.js application using MongoDB. However, I encountered this error: var UtenteSchema = Scheme({ TypeError: Scheme is not a function Below is my model utente.js: cons ...

Tips for setting default values for named parameters in JavaScript

In my TypeScript method, I am using named parameters like this public foo({x, y, z , m , n} : {x:string, y: number, z: number, m?:string, n?:number}) { } The parameters m and n will be provided from another object like const defaults = { m : 'M&apo ...

The <h:outputStylesheet> tag fails to display any content on the HTML page

I am having trouble getting my CSS sheet to load while using JSF2 and PrimeFaces. The CSS file is located at WebContent/resources/css/style.css Here is the xhtml code: <h:head></h:head> <h:body> <h:outputStylesheet name="css/styles. ...

Does the AngularJS inject function operate synchronously?

Does the AngularJS inject method work synchronously? Here is an example: inject(function(_$compile_, _$rootScope_) { $compile = _$compile_; rootScope = _$rootScope_.$new(); }); ...

Redirect to a new URL using $routeProvider's resolve feature

Currently, I am in the process of developing an application that includes the following endpoint: .when('/service/:id?', { templateUrl: 'views/service.html', controller: 'ServiceCtrl', resolve: { service: fu ...

When attempting to dispatch in getServerSideProps, the State refuses to change. Could it be due to the Redux-Next-Wrapper?

I'm facing an issue where the Redux Store does not change when I dispatch in getServerSideProps. Even though I can see the changes in console log after dispatch, the store appears as an empty array when the page loads. Why are these changes not taking ...

Ways to replace CSS classes created using makeStyles

To clarify, my development environment is using MUI version 4.12.3. Inside file A, I have a simplified code snippet for a functional component, along with the usage of makeStyles to style a JSX element within the return statement (not displayed here). Ever ...

Integrating fresh components into a JSON structure

I've been attempting to insert a new element into my JSON, but I'm struggling to do it correctly. I've tried numerous approaches and am unsure of what might be causing the issue. INITIAL JSON INPUT { "UnitID":"1148", "UNIT":"202B", "Sp ...

When utilizing JavaScript to input text, I have observed that if I enter text in one text box, any previously entered value is automatically deleted

Currently, I am facing an issue with 3 text boxes in a row that I am populating using JavaScript. The problem arises when I enter text into one field and then move to the second box to input text - the value from the first text box gets removed. Below is ...

What is the best way to ensure the width of the div is adjusted to fit the table it contains?

What is the best way to adjust the width of a div containing a potentially wider-than-screen table? Despite setting padding: 10px for the div, the right padding disappears when the table exceeds the screen width. Below is an example code snippet: <div ...

The :first selector examines the parent's parent as a reference point, rather than the immediate

I am facing a challenge with shuffling large elements within my layout because of floating them and attempting to display them. Specifically, the elements with the class .gallery-large always need to be the first child inside the .item container. There are ...

Understanding how to deduce parameter types in TypeScript

How can I infer the parameter type? I am working on creating a state management library that is similar to Redux, but I am having trouble defining types for it. Here is the prototype: interface IModel<S, A> { state: S action: IActions<S, A&g ...