What steps can I take to stop nested tables in ng-repeat from displaying in each individual row?

Creating a dynamic nested hierarchy of tables is my current goal. I've been able to retrieve the data without any issues, but when using ng-repeat at each level, the parent table ends up inserting child data for a specific row into every row of the parent table. My aim is to prevent this from happening, and so far, I've experimented with using ng-repeat-start and ng-repeat-end. However, due to the nesting of tables, I'm unable to place the end tag in a suitable location to halt the repetition.

A recent update: To provide more clarity on the situation, I have 3 nested tables. The highest-level table contains a list of groups, with the first child table displaying all items belonging to a specific group in the parent table. Upon clicking the expand button, the child table gets populated based on which row was clicked in the parent table. This functionality works fine, yet the issue arises when the child table appears in every row of the parent table instead of just the selected row.

Visit this Plunker link for reference: http://plnkr.co/edit/RVOnf9wBF3TzXauzvMfF

HTML code snippet:

<table id="sicGroupTable" class="RadGrid_Metro">
   <thead>
       <tr>
          <td>Sic Code Group</td>
       </tr>
   </thead>
       <tbody ng-repeat="group in sicCodeGroup">
           <tr class="rgRow">
               <td class="rgExpandCol"><img class="rgExpand" ng-click="expandRow(group.GroupId)" ng-model="hideTwoDigitSub" /></td>
               <td><input type="checkbox" ng-model="SelectGroup" /></td>
               <td>{{group.GroupName}}</td>
           </tr>
           <tr ng-hide="hideTwoDigitSub">
               <td></td>
               <td>
                   <table id="sic2DigitTable" class="RadGrid_Metro">
                       <thead>
                           <tr>
                               <th>2 digit sic Code</th>
                           </tr>
                       </thead>
                           <tbody>
                               <tr ng-repeat="twoDigitSic in twoDigitSicCodes" class="rgRow">
                                   <td class="rgExpandCol"><img class="rgExpand" ng-click="expandRow(twoDigitSic.SicCode)" /></td>
                                   <td><input type="checkbox" ng-model="Select2DigitSicCode" /></td>
                                   <td>{{twoDigitSic.SICCode2}} - {{twoDigitSic.Title}}</td>
                                </tr>
                                <tr>
                                <td></td>
                                 <td>
                                     <table id="sic4DigitTable" class="RadGrid_Metro">
                                         <thead>
                                             <tr>
                                                 <th>4 digit sic code</th>
                                             </tr>
                                         </thead>
                                             <tbody>
                                                 <tr class="rgRow">
                                                     <td class="rgExpandCol"><img class="rgExpand" ng-click="expandRow(sicCode.SicCode)" /></td>
                                                     <td><input type="checkbox" ng-model="Select2DigitSicCode" /></td>
                                                     <td>{{sicCode.SicCode}} - {{sicCode.Title}}</td>
                                                 </tr>
                                                 <tr>
                                                     <td></td>
                                                 </tr>
                                              </tbody>
                                          </table>
                                      </td>
                                  </tr>
                              </tbody>
                          </table>
                      </td>
                  </tr>
              </tbody>
          </table>

JavaScript snippet:

var app = angular.module("apptSetting", ['ngResource'])

app.factory('dataFactory',
    function ($resource) {
        return {
            getSicGroups: $resource('../Packages/GetJsonSicCodeGroups'),
            expandRow: $resource('../Packages/GetExpandedRowData')
        }
    });

app.controller('aPackageController', ['$scope', 'dataFactory', function ($scope,  dataFactory) {
function init() {
    $scope.hideTwoDigitSub = true;
    $scope.hideFourdigitsub = true;
}
$scope.sicCodeGroup = dataFactory.getSicGroups.query({}, isArray = true);
$scope.twoDigitSicCodes = null;
$scope.expandRow = function (groupId, sicCode) {
    if (groupId != undefined)
    {
        $scope.twoDigitSicCodes = dataFactory.expandRow.query({ GroupId: groupId }, isArray = true);
        $scope.hideTwoDigitSub = false;
        if (sicCode != null && sicCode != undefined && sicCode != "") {
            if (sicCode.length == 2) {
                $scope.hideTwoDigitSub = false;
                $scope.twoDigitSicCodes = dataFactory.Get2DigitSicCodes.query({ GroupId: groupId }, isArray = true);
            }
        }
    }     
}
    init();
}])

Answer №1

The main issue here is that a single boolean variable hideTwoDigitSub is being used to control all the generated tr elements in your ngRepeat:

<tr ng-hide="hideTwoDigitSub">

This means that when you set $scope.hideTwoDigitSub = false;, every element with ngHide within your ngRepeat will receive this value and therefore show all the tr elements.

Improvement for Radio Button Functionality

Instead of using a boolean, it would be better to assign the groupId to the hideTwoDigitSub variable for the row you want to display (consider renaming hideTwoDigitSub to showTwoDigitSub since it now indicates which row to display).

In your expandRow() function, modify the line:

$scope.hideTwoDigitSub = false;

to:

$scope.hideTwoDigitSub = groupId;

Also, update the tr tag to:

<tr ng-hide="hideTwoDigitSub != group.GroupId">

This way, the row will remain hidden unless the control variable hideTwoDigitSub is not equal to the current group's GroupId.

Alternative Approach Using ngShow

If you prefer using ngShow, you can modify the code as follows:

<tr ng-show="showTwoDigitSub == group.GroupId">

View radio button plunker example

Solution Using Checkboxes

To simplify things, switch from hideTwoDigitSub to showTwoDigitSub. The steps below assume this change has been made.

In the init() function, initialize the control variable as an array:

$scope.showTwoDigitSub=[];

To toggle the appropriate control inside expand, use this line:

$scope.showTwoDigitSub[groupId] = !$scope.showTwoDigitSub[groupId]; 

Lastly, implement the array in your HTML:

<tr ng-show="showTwoDigitSub[group.GroupId]">

Check out the checkbox plunker demo

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

How can I customize the color of the cross in the hamburger menu?

Seeking guidance for a technical issue I'm facing. The problem lies with the X button on the menu - although everything seems to be in order, I encounter an error when attempting to change its color to black. If anyone has insight or advice on how I c ...

Retrieving additional parameters from an HTTP request

Imagine a scenario where I am sending a request to a Node Server in order to retrieve a JS file: <script src="/example.js" id="123456"> On the Node server side: app.get('/example.js', function(req, res, next) { console.log(req.params.id) ...

Incorporating Chip into a Material-UI DataGrid column

I'm having trouble displaying data of a specific column inside a chip. I attempted to use the Chip component in my code: StackBlitz Demo Web Link: Live Demo I tried to incorporate it using: import Chip from '@mui/material/Chip'; but c ...

I am having trouble toggling radio buttons in React

class VoiceSelector extends Component { constructor(props){ this.handleCheck = this.handleCheck.bind(this); } state={ voices : [ {"Voice":"Indian English Female Voice 1"}, {"Voice":&qu ...

Issue with Chart JS: Firefox is reporting that the 'Chart' is not defined

As a newcomer to Chart JS, I am currently utilizing it in Angular JS version 1.5.3 with Chart JS version 2.1.4. Below is the code snippet I am working with: var myChart = new Chart(ctx, { type: 'line', data: { labels: dates, datasets: [{ ...

Tips for effectively separating HTML and JavaScript for a cleaner code structure

Is it necessary and worth it to decouple HTML and JavaScript? In JavaScript logic, you always have to use some HTML elements... I know that in C#, you can decouple for easier maintenance and testing scenarios. ...

PHP's json_encode function does not escape the single quote character

I am currently facing an issue with parsing a JSON encoded object in JavaScript. When I use JSON.parse(word_array);, it throws an error saying Uncaught SyntaxError: Unexpected identifier. Upon investigating, I discovered that the object word_array is miss ...

Unable to activate focus() on a specific text field

It's quite peculiar. I'm working with a Sammy.js application, and my goal is to set the focus on a text field as soon as the HTML loads. Here's the CoffeeScript code snippet I've written: this.partial('templates/my-template.jqt&ap ...

Double the Trouble: jQuery AJAX Making Two Calls

I've already posted a question about this, but I have now identified the exact issue. Now, I am seeking a solution for it :) Below is my code snippet: $('input[type="text"][name="appLink"]').unbind('keyup').unbind('ajax&apos ...

What is the best way to identify a particular subtype of HTMLElement based on its tag name and then save that element within another one?

I have a function that generates a node and returns it. Here is an example implementation: addElement: function ({ parentNode, elementName, tagName, }) { // Creates and appends the new node. parentNode[elementName] = document.createEl ...

Using Ajax and Session Variables for a Worksafe Filter to selectively hide images

Creating a photography portfolio with some images containing nudity prompts the need to hide them by default until the user chooses to reveal them by clicking a "Toggle Worksafe Mode" button. To prevent "confirm form resubmission" errors when users naviga ...

Tips for saving information to a JSON file in Reactjs

Perhaps the real question should be How can I save data to a JSON file using Reactjs and Nodejs? I'm new to React and unsure about which database to use. On a side note, it's simple to read from a json file with var data = require('./d ...

Ng-show not updating when scope variable changes

Initially, I set the boolean flag of the infoWindow to false: $scope.infoWindow = false; Subsequently, I've added a google maps listener for a click event as shown below: google.maps.event.addListener(circle, 'click', function(event) { ...

The response from the Ajax call to the WCF is coming back as null

I am currently facing an issue where my ajax call to a function exposed by a WCF service is always returning 'undefined' in the success method, despite the fact that the function on the WCF side is returning the correct answer. I have debugged an ...

trouble encountered while sending data to php using ajax and json

Despite trying multiple solutions, my script is still not working properly. It functions without the $_POST value and without JSON, but when both are included, it fails to work. The error message displayed by the function is as follows: <b>Notice< ...

What is the best way to toggle the active class on a ul list?

After clicking, the active class remains on the original "li" instead of changing as it should. I've tried modifying the code but haven't been able to find a solution. Can someone please review what I might have missed? It seems like there's ...

The route handler for app.get('/') in Express is not returning the input data as expected

I have multiple routes set up, and they are all functioning properly except for the app.get('/') route. When I navigate to 'localhost:3000/', nothing is being displayed in the backend console or on the frontend. The Home component is su ...

Mastering End-to-End Testing in AngularJS with Protractor

I am struggling to extract the last row of a ng-repeat table in Protractor for testing purposes. My goal is to verify that an object created in a previous test run is displayed correctly. So far, I have managed to retrieve all the text in the last row but ...

Troubleshooting the toUpperCase error: A step-by-step guide

Within my Angular JS code, I have a variable: $scope.formData.time This variable holds the format: "12:00:00" In addition, I've created a filter called 'timeApp': .filter('timeApp', function ($filter) { return funct ...

Ways to determine if a user has been logged out of the identity server using a Javascript application

My setup includes an Identity server 4 application, several asp .net core applications, and a Javascript application. I've enabled back channel logout on the asp .net core applications to ensure that when one application logs out, they are all logged ...