Building a straightforward expandable/collapsible tree view with AngularJS by utilizing a particular data format

Seeking a tree view control that displays as follows:

http://plnkr.co/edit/JAIyolmqPqO9KsynSiZp?p=preview

+Parent
     child
+Parent
     child
     child
     child
     child
+Parent
     child
     child
     child     
     child
     child
     child
     child

In need of a simple tree structure with collapse-expand features and single level depth.

Requirements: Angular 1.6 and Bootstrap for implementation.

Data includes "List of Tasks" with each task containing "List Of Items." How can I bind this data to achieve the desired tree layout?

Explored other options but found them to be too complex with additional features.

Your suggestions are highly appreciated.

Answer №1

To display a single level, consider using an HTML table with two ng-repeats - one for tasks and another for child tasks. Additionally, you can include buttons to toggle the visibility of child tasks.

<table class="table table-hover">
    <thead>
        <tr>
            <td><b>ID</b></td>
            <td><b>Task</b></td>
            <td></td>
        </tr>
    </thead>
    <tbody ng-repeat="task in ListOfTasks" ng-init="task.showChildren = false">
        <tr>
            <td>
                <span class="label label-success">{{$index+1}}</span>
                <span>
                    <a class="label label-danger" ng-show="!post.showChildren" ng-click="task.showChildren = true">+</a>
                    <a class="label label-primary" ng-show="post.showChildren" ng-click="task.showChildren = false">-</a>
                </span>
            </td>
            <td>
                {{task.TASK_NAME}}
            </td>
            <td class="table-actions"></td>
        </tr>
        <tr ng-show="task.showChildren">
            <td>
                <div class="row">
                    <div class="col-sm-offset-1">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <td>ID</td>
                                    <td><b>Child Task</b></td>
                                    <td></td>
                                </tr>
                            </thead>
                            <tbody>
                                <tr ng-repeat="childTasks in task.CHILD_TASKS">
                                    <td class="col-sm-1">
                                        <span class="label label-success">{{ $index + 1 }}</span>
                                    </td>
                                    <td>
                                       {{childTasks.CHILD_TASK_NAME}}
                                    </td>
                                    <td class="table-actions"></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </td>
        </tr>
    </tbody>
</table>

Check out how the table would appear https://i.sstatic.net/uHI4b.jpg

If you prefer not to load all data initially, fetch children on expand click.

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

What steps can I take to troubleshoot and fix the height of a div/span element?

Here is my HTML code: <div> <span style="display: inline-block;height:20px">20</span> <span style="display: inline-block;"><img src="img/ex.png"/></span> </div> The image I have in the second span is sized at ...

How to manually close the modal in Next.js using bootstrap 5

Incorporating Bootstrap 5.2 modals into my Next.js application has been smooth sailing so far. However, I've encountered an issue with closing the modal window after a successful backend request. To trigger the modal, I use the data-bs-toggle="modal" ...

How can I most effectively establish defaultValues for react-hook-form in this scenario?

I am working on a static Next.js website with only frontend functionality. In the pages/feedback/[id]/edit.tsx page, I need to dynamically retrieve an id and set default values in a nested FeedbackForm component. export const FeedbackForm = ({ editing }: { ...

Angular does not seem to be identifying the project name as a valid property

After installing Angular materials using the CLI, I decided to check my angular.json file and encountered an error in the console stating that "Property MEAN-APP is not allowed". [The name of my Angular project is MEAN-APP] Here's a screenshot of the ...

What is the best way to show HTML content within a <div> using Angular 1.2.0 - rc2?

Trying to incorporate HTML into a webpage can be tricky, but it's essential for displaying content effectively. In my attempt to do so, I used the following code: xx {{ pageHtml }} yy <div data-ng-bind-html-unsafe="$scope.pageHtm ...

What is the best way to apply various styles using the ng-style directive in different scenarios?

When working in Angular, I am attempting to apply different style attributes based on varying conditions. However, the typical conditional expression method is limited to just two conditions and is not providing the desired results. <div ng-style=" ...

Transforming Observable into a Promise

Is it considered a best practice to convert an observable object to a promise, given that observables can be used in most scenarios instead of promises? I recently started learning Angular and came across the following code snippet in a new project using ...

JavaScript - Delayed Text Appearance with Smooth Start

I want to create a landing page where the text appears with a slight delay - first, the first line should be displayed followed by the second line. Both lines should have an easing effect as they appear. Below is a screenshot of the section: https://i.sst ...

Rotation Causes Vertices to Deviate from Expected Axis Movement

While moving the vertices of a shape on mouse move works well, applying a rotation to the shape causes the vertices to move along the wrong axis. If you'd like to see the issue in action, check out this codesandbox.io link: https://codesandbox.io/emb ...

What are the reasons behind the failure of this regex matching in Angular specifically on Safari browser?

In my angular project, I have the following code. It works perfectly fine in Chrome and Firefox, but in Safari, it throws an exception. var shour = "9:00:00 PM CDT"; var ehour = "12:00:00 AM CDT"; var conver_shour = shour.match(/^(\d+):(\d+)/)[ ...

Unusual situation observed in ExpressJS: Callback function fails to execute

Currently, I am facing an issue with my web app built using expressjs and node. It seems that the functionality is not working correctly. An unusual situation has occurred where accessing the first link in the browser yields the expected results, while th ...

Using the Angular Directive Method Binding for Selecting Items

Is it normal for all method bindings to fire when changing a select element with the same directive in Angular? For example, selecting a state also triggers the selection of a dog. Do I have a misunderstanding of Angular Directives? This behavior does not ...

The user interface does not reflect the updated data after a successful update

I am currently getting acquainted with Breeze and creating a sample to enhance my understanding. However, I have encountered an issue. In my datacontext.js file, I have included Breeze code to fetch data from a service as shown below: sample.factory(&apos ...

Why isn't my AJAX POST request to PHP functioning correctly?

It was all working perfectly fine earlier, but now something seems off and I must have made a mistake somewhere. I'm in the process of setting up a form where the information entered is sent via AJAX to my PHP file, which then outputs the username an ...

Tips for converting JObject to T using JSON.Net

When working with JSON data, I am aware of the standard method of using JsonConvert.DeserializeObject<T>(string). However, my specific requirement is to access the object's _type property in order to determine the exact class for casting. Essent ...

Fetching data from the database and seamlessly displaying it in a single view with the help of jQuery within the ASP.NET MVC framework

Currently, I am working with asp.net MVC to develop an application. In this project, I have a view that requires a treeview in the left pane and the details of the selected node item to be displayed in the right pane. The left pane's table represents ...

Create a collection of functions within an array that each return promises

I have created 4 different functions that return promises. By running the "hello" function and passing each subsequent function into the next .then, you can generate a single long string output. var hello = function(str){ return Promise.resolve(str + "h ...

Guide: Exchanging choices using jQuery and Address plugin

I am trying to find a way to exchange the values of two options with each other. I created a simple fiddle that successfully swaps input values, but when I tried it with select options, it didn't work as expected. The approach I'm using is based ...

Troubleshooting issue with jQuery animate not correctly scrolling

I am experiencing an issue with my jQuery code that is supposed to scroll a smaller container element within a larger container element when a button is clicked. Despite testing with an alert and verifying that the code is working, the scrolling functional ...

Tips for transferring an MVC model to a UI-bootstrap modal

Trying to implement an Angular/bootstrap modal for editing MVC ApplicationUser scaffolded views. Came across jquery examples but want to stick with angular-ui or plain bootstrap for consistency in modals. Unclear on how the MVC controller is being called f ...