Illuminate the principles of AngularJS

My goal is to achieve results in the following manner:

Expected workflow chart:

https://i.stack.imgur.com/G2OAO.png

Here is the default view:

https://i.stack.imgur.com/H6xkZ.png

Scenario 1: When I click on the number "1", all elements from left to right and their children should be highlighted as shown below:

https://i.stack.imgur.com/3L8h2.png

Scenario 2: Building on Scenario 1, if I click on the number "3", all elements from left to right and their children should have their highlight removed since "3" is considered the parent/root element:

https://i.stack.imgur.com/qQoQP.png

Scenario 3: Starting from the default view with no selection, clicking on the number "18" should highlight all parent values as shown below:

https://i.stack.imgur.com/4H3hs.png

Scenario 4: Following Scenario 3, clicking on the number "18" should only remove the highlight for "18" specifically, without deselecting parent levels on the right side:

https://i.stack.imgur.com/3y4OT.png

Note: When clicking on any value, deselection of parent levels on the right is not required. Only the clicked value should lose its highlight.

Below is the code: JSFiddle

 $scope.setActiveSelectedItem = function() {
 return $scope.groupOfCheckboxes[i].RowValues[j].isActive = !$scope.groupOfCheckboxes[i].RowValues[j].isActive;
    }

    $scope.setActivePrevNext = function(arr, id) {
     let keys = Object.keys(arr);
     let nextIndex = keys.indexOf(id) +1;
     let nextItem = keys[nextIndex];
     return $scope.groupOfCheckboxes[i].RowValues[nextIndex].isActive = !$scope.groupOfCheckboxes[i].RowValues[nextIndex].isActive;
    }

    $scope.setBinary = function (id) {

        for(i=0; i<$scope.groupOfCheckboxes.length; i++) {
          for(j=0; j<$scope.groupOfCheckboxes[i].RowValues.length; j++) {

             if($scope.groupOfCheckboxes[i].RowValues[j].td == id) { 
             $scope.setActiveSelectedItem();
             $scope.setActivePrevNext($scope.groupOfCheckboxes, id);

             } 

             }

        }


    }

});

Answer №1

Learn HTML and AngularJS Integration

<table>
    <tr ng-repeat="checkbox in groupOfCheckboxes track by $index" ng-init="rowId = $index">
        <td ng-repeat="data in checkbox.RowValues track by $index" ng-init="vId = $index">
            <span ng-if="data.show" ng-click="setBinary(rowId,vId,data)"
                ng-class="data.isActive ? 'active' : ''">{{data.td}}</span>
        </td>
    </tr>
</table>

Implementing Controller Logic

    $scope.groupOfCheckboxes = [
    {
        Row: "1",
        RowValues: [
            { td: "1", show: true },
            { td: "2", show: true },
            { td: "3", show: true },
            { td: "4", show: true },
            { td: "5", show: true }
        ]
    },
    {
        Row: "2",
        RowValues: [
            { td: "6", show: false },
            { td: "7", show: false },
            { td: "8", show: false },
            { td: "9", show: true },
            { td: "10", show: false }
        ]
    },
    {
        Row: "3",
        RowValues: [
            { td: "11", show: false },
            { td: "12", show: false },
            { td: "13", show: true },
            { td: "14", show: true }
        ]
    },
    {
        Row: "4",
        RowValues: [
            { td: "15", show: false },
            { td: "16", show: false },
            { td: "17", show: false },
            { td: "18", show: true }
        ]
    }
];

// Implementing Parent-Child Relationship
for (let rowIndex = $scope.groupOfCheckboxes.length - 1; rowIndex >= 0; rowIndex--) {
    for (let valIndex = $scope.groupOfCheckboxes[rowIndex].RowValues.length - 1; valIndex >= 0; valIndex--) {
        $scope.groupOfCheckboxes[rowIndex].RowValues[valIndex].isActive = false;
        if ($scope.groupOfCheckboxes[rowIndex].RowValues[valIndex].show == true) {
            loop4:
            for (let rx = rowIndex; rx >= 0; rx--) {
                for (let vx = valIndex - 1; vx >= 0; vx--) {
                    if ($scope.groupOfCheckboxes[rx].RowValues[vx].show == true) {
                        $scope.groupOfCheckboxes[rowIndex].RowValues[valIndex].pri = rx;
                        $scope.groupOfCheckboxes[rowIndex].RowValues[valIndex].pvi = vx;
                        break loop4;
                    };
                }
            }
        };
    }
}
$scope.setBinary = function (rowId, vId, data) {
    data.isActive = !data.isActive
    bool = data.isActive;
    loopx:
    for (let row = rowId; row < $scope.groupOfCheckboxes.length; row++) {
        loop1:
        for (let v = vId; v < $scope.groupOfCheckboxes[row].RowValues.length; v++) {
            if (row == rowId) {
                if ($scope.groupOfCheckboxes[row].RowValues[v].show == true) {
                    $scope.groupOfCheckboxes[row].RowValues[v].isActive = bool;
                } else {
                    break;
                };
            } else {
                if (v == vId) {
                    if ($scope.groupOfCheckboxes[row].RowValues[v].show == true) {
                        break loopx;
                    }
                } else {
                    if ($scope.groupOfCheckboxes[row].RowValues[v].show == true) {
                        $scope.groupOfCheckboxes[row].RowValues[v].isActive = bool;
                    }
                }
            }
        }
    }
    function rec(pri, pvi) {
        if ($scope.groupOfCheckboxes[pri]) {
            $scope.groupOfCheckboxes[pri].RowValues[pvi].isActive = true;
            x = $scope.groupOfCheckboxes[pri].RowValues[pvi]
            rec(x.pri, x.pvi)
        }
    }
    rec(data.pri, data.pvi)
}

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

Retrieve JSON Data Using Angular in a Wordpress Environment

I need assistance with displaying a JSON Array in <li>'s within a Wordpress Template. Here is the specific JSON file: I am completely unfamiliar with how to achieve this. This is the HTML code I have: <div ng-app="appExtern" ng- ...

The $eval function encounters issues within an angular.js directive

In my quest to create an angular.js directive, I encountered a situation where clicking on a <span> element should turn it into an editable input. The code below accomplishes this task flawlessly, except in the scenario where the model is empty or ha ...

Create a unique key dynamically based on a specified scope value using AngularJs

I am working with an angular directive that utilizes a title with a key from a messages.properties file. To dynamically generate the key, I want to concatenate 'root.' + scope.value + '.title' like so: titre="{{ 'flux.' + &ap ...

Utilizing the controller specified in the template that has been included

Within this snippet of code, I am attempting to utilize a controller named FooCtrl that is defined in the included template app/foo.html, using the directive common.script. angular.module('common.script', []).directive('script', func ...

Discovering the version of the Javascript library utilized on a website - a step-by-step guide

My quest is to uncover the versions of JavaScript libraries being utilized by popular websites. By using phantomjs.exe, I successfully identified the version information for jquery. However, I am currently at a loss on how to expand this capability to inc ...

Tips for dynamically binding the image source in Angular based on certain conditions

Hi, I'm receiving a response from an API that only includes a name but no image source. However, I have a collection of images in my local images folder. <div class="left"> <img src="{{ record.imgSrc }}" alt="{ ...

In need of a method to create PDFs using client-side technology (specifically AngularJS)?

I need a method to create PDFs using AngularJs that includes HTML, CSS, and JavaScript elements. I have tried two options: jsPDF (which does not support CSS) Shrimp (built on Ruby) Neither of these solutions fit my needs. Is there another way to accom ...

What is the functionality of Angular router and dynamic links in relation to Google services?

Currently, I am working on a project that involves an admin/frontend AngularJS framework. This project consists of a blog section and an area for project posts. I have some queries related to the dynamically created links in Angular: Do these links get ...

Storing information in local storage based on the currently logged-in user

Is there a way to store the information of the currently logged in user using AngularJS? For example, if user1 is logged in, can I display only that user's details on the page? How can I assign unique keys for each logged in user? ...

Directive not displaying CSS transition

I've been experimenting with transitions and directives. I have developed a Card directive that is supposed to display a fullscreen clone of itself when clicked. However, the transition does not occur unless I apply the altering CSS class in a timeout ...

Is it necessary for AngularJS directive controllers to depend on their parent directive controllers?

My thought process might be a bit unconventional, but I'm currently working on creating three nested directives: screen, component, and widget. The goal is for the widget to trigger some action in the component, which then triggers an action in the sc ...

Encountering unforeseen challenges when implementing Angular routing alongside NGINX routing

I have developed an Angular single page application that utilizes HTML routing and ng-route. This means that all the pages can be accessed through links such as: example.com/products example.com/home However, I also have a blog section on my website whic ...

Implementing html5mode in Express.js and Angular.js for cleaner URLs

I've been working on resolving the issue of avoiding # in my Angular app with an ExpressJS server-side setup. I found a solution to enable html5mode and it worked well. However, whenever there is another 'get' request to fetch data from a di ...

Is there a way for me to determine if a route has been defined at the start of my AngularJS ui-router application?

My AngularJS SPA application utilizes angular-ui-router. Normally, the application starts when the user visits: (a) www.example.com and then automatically loads the index.html page. At this point, I want the application to directly transition to the home ...

What is the process for altering the "this" keyword within the Controller during construction?

Exploring the Controller as structure feature in AngularJS has been a challenge for me. I am using the 'this' keyword of the controller to store values in a form. While working with the controller, the values are being set by a promise. However, ...

Utilizing a combination of nested modules and ui-views with ui-router for a complex

I'm currently tackling a challenging task of structuring a UI architecture using Angular and Angular UI Router. My objective is to establish routes with nested ui-views across multiple modules. Check out what I'm trying to achieve: http://plnkr. ...

Unraveling Angular: Generating a Random Sequence of Symbols from an Array Multiple Times

A collection of symbols from A to E is at my disposal. My task is to display each symbol using ng-repeat, but the order in which they appear should be random and each symbol needs to be repeated n times (let's say 4). ...

customizing angular filters and implementing them in JavaScript

I'm currently working with an array in my controller. $scope.arr = [......], a Now, in the HTML file, I want to implement the following: ng-repeat = "item in arr | color:'blue'" //this line works, filter done in the app.filter way. The & ...

Successive vows

I'm trying to retrieve responses from four promises, but I currently have to call each function in sequence one after the other. In my code, you can see that I trigger the next function within the promise callback of the previously called function. H ...

Start Your Sentences with an Exclamation Point using an AngularJS Filter

When working with AngularJS, I encountered an issue while filtering a list of objects using ng-repeat: <tr ng-repeat="application in page.applications | filter: {DisplayName:page.ApplicationSearchValue}"> {{application.DisplayName}} </tr> ...