By default, the first table row is displayed when using table grouping in AngularJS

I am attempting to display only the first tr in the table and hide all other tr elements by default. I have tried using ng-show and ng-hide but it does not seem to be working as expected.

This is not originally my plunker; I used it for reference on grouping and I am trying to implement the stated functionality within the same. The default behavior should show area1 while collapsing area2. Only upon clicking area 2, should area1 collapse.

var app = angular.module('app', ['ngTable']);

app.controller('myCtl', function($scope, $timeout, NgTableParams) {

  $scope.data = [{
      uid: 'User 11',
      name: 'Name 11',
      area: 'Area 1'
    },
    {
      uid: 'User 12',
      name: 'Name 12',
      area: 'Area 1'
    },
    {
      uid: 'User 21',
      name: 'Name 21',
      area: 'Area 2'
    },
    {
      uid: 'User 22',
      name: 'Name 22',
      area: 'Area 2'
    }
  ];


  $scope.tableParams = new NgTableParams({
    group: "area"
  }, {
    dataset: $scope.data
  });


});
<!DOCTYPE html>
<html>

<head>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.css" rel="stylesheet" data-semver="3.3.6" data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="43212c2c3730373122336e20303003706d706d75">[email protected]</a>" />
  <script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e1808f86948d8093cf8b92a1d0cfd5cfd9">[email protected]</a>" data-semver="1.4.8" src="https://code.angularjs.org/1.4.8/angular.js"></script>
  <link rel="stylesheet" href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fc929bd1889d9e9099bcced2ccd2ce">[email protected]</a>/bundles/ng-table.css" />
  <script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ee8089c39a8f8c828baedcc0dec0dc">[email protected]</a>/bundles/ng-table.min.js"></script>
  <script src="script.js"></script>
</head>

<body>
  <div ng-controller="myCtl" ng-app="app">

    <table ng-table="tableParams" class="table table-bordered table-hover" show-group="false">
      <colgroup>
        <col width="50%" />
        <col width="30%" />
      </colgroup>
      <tr class="ng-table-group" ng-repeat-start="group in $groups">
        <td colspan="2">
          <a href="">
                  {{ group.value }}
                </a>
        </td>
      </tr>
      <tr ng-repeat="u in group.data" ng-repeat-end>
        <td title="'User ID'">{{ u.uid }}</td>
        <td title="'Name'">{{ u.name }}</td>
        <td title="'Area'">{{ u.area }}</td>
      </tr>
    </table>

  </div>
</body>

</html>

http://plnkr.co/edit/M8BcStInfSaSEaq6UVN7?p=previewplunker example

Answer №1

To achieve this, you can utilize a variable that holds the active row. Start by setting it to the default active row:

$scope.activeRow = 'Area 1';

Then, update the variable when an ngClick event occurs (since it is inside an ng-repeat, remember to refer to the parent scope):

ng-click="$parent.activeRow = group.value"

Finally, hide the row if it does not match the activeRow:

ng-hide="$parent.activeRow != u.area"

You can see a demonstration of this in action on this working plunker.

Answer №2

ng-repeat offers special properties such as $first (indicating if the element is first in the iteration), $index, $middle, and $last.

Utilize $first to specifically target the initial row :)

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

Error in MEAN CRUD operation cannot be determined

{ text: undefined, done: false, _id: 529e16025f5222dc36000002, __v: 0 } PUT /api/todos/529e16025f5222dc36000002 200 142ms - 68b Encountering an issue while updating my CRUD todo list. Despite receiving a successful status code of 200 after submittin ...

AngularJS Loopback SDK - querying associated models

The AngularJS "SDK creates a Category.products() method that can be used to list all products in a given category," as stated in the loopback documentation. $scope.products = Category.products({ id: $scope.category.id, }); Although this method works ...

What is an alternative way to display static images in Rails 5 without relying on the Asset Pipeline?

I developed a web-based application with the backend built on Rails 5. Utilizing AngularJS for the frontend, I opted to not use the Asset Pipeline to deliver static content. Instead, I loaded all my scripts (JS & CSS) in the index.html file located within ...

Switching from a click event to a hover event in JavaScript

I've been experimenting with animating a burger bar on hover and came across an example online that I managed to adapt for mouseenter functionality. However, I'm struggling to make it revert back to the burger bar shape once the mouse leaves on m ...

Troubleshooting issue: AngularJS NVD3 line chart directive does not refresh after data update (not updating in real time)

I am currently facing an issue with the nvd3-line-chart directive in my AngularJS application. The chart does not redraw when I change the underlying model... As a newcomer to AngularJS, there may be something obvious that experienced programmers will not ...

Struggling with identifying errors in the Javascript code for my assignment

My code is giving me trouble and I could really use some assistance You can find my code in this GitHub folder: link. To see the project in action, visit this page on GitHub Pages: link. The task involves iterating over an array of names and printing eit ...

Display or conceal fields depending on custom object specifications

I am attempting to centralize my show/hide functionality for fields in one object (like vm.foo) that contains key-value pairs. For example, I could add another pair like 1502: true to hide a field with the key 1502. Is there a way to pass variables from t ...

Position your Logo to the left, align the Menu in the center, and place top links on the right using Bootstrap

I am currently struggling to create two rows with the logo aligned to the left, 'Home' and 'User' menu links at the top right corner, and a second row displaying 'Link1', 'Link2', 'Link3' menus centered. Th ...

HTML - Centered layout with a constant header and footer

Imagine a layout where there is always a header at the top, a footer at the bottom, and the space between them should be filled by the content 100% (image 2). Additionally, everything in the header, content, and footer should be horizontally centered, usin ...

"Sorting function malfunctioning when applied to a column populated with values retrieved from a service

While displaying basic personal information in a table and sorting the rows using the orderBy function, I encountered an issue with one specific column. This column's value is retrieved from a service, and when I try to resort the rows by clicking on ...

Switching JSON data when clicked in Angular 2

Looking for a way to dynamically switch between different data sets in JSON upon user interaction with UI buttons. I am utilizing Angular 2 framework for this task. The HTML structure is as follows: <button type="button" (click)="changeOdds('odds ...

A guide on verifying the creation of an Angular service through Jasmine testing

In front of me is a code snippet that serves as a branch from ng-wrap, taking inspiration from this particular article. Essentially, it establishes an angular service for global variables introduced by external libraries to align with the concept of depend ...

What could be causing my getAsFile() method to return null?

Below is the code I have been working on: document.getElementById("Image_Panel").addEventListener('paste', (event) => { console.log("Initiating image paste - Step 1"); const clipboardData = event.clipboardData; // Checking ...

Execute script when the awaited promise is fulfilled

I am looking to retrieve the URL of a cat image using the Pexels API through a script, and then set that image link as the source of an actual image element. I attempted to include some loading text to keep things interesting while waiting for the image l ...

Applying Vue Quill CSS to the initial Quill Editor component exclusively and tips for personalizing the toolbar

I have encountered an odd CSS issue while using the vue-quill package in my project. Each comment has its own quill editor for replying, and I temporarily set the isOpen prop to true for debugging purposes. This results in a quill editor being displayed un ...

Tips for updating the color of the first td element in a table using a specific class

Below is the CSS code for my table: table.show-my-request-table { border: 1px solid black; margin-left:auto; margin-right:auto; border-collapse: collapse; } tr.show-my-request-table-header{ border: 1px solid black; } tr.show-my-requ ...

Can buttons be inserted into an Input control?

I am currently working on developing a custom timepicker that is compatible with both Internet Explorer and Chrome. The desired time format is 'hh:mm'. This is what I have so far: <input ng-model="timeHours" type="number" class="hours" placeh ...

"Integrating AngularJS with Laravel: A Guide to Invoking Functions within Input Fields

I've encountered an issue while trying to integrate AngularJS with a Laravel form builder. Initially, my HTML form and AngularJS code were working seamlessly together. However, upon transitioning to the Laravel form builder syntax, an error surfaced. ...

What is the reason behind the absence of a $interval feature in AngularJS?

One of the conveniences of AngularJS is its $timeout service, which serves as a wrapper for setTimeout. Have you ever wondered why there isn't an equivalent for setInterval in AngularJS? ...

The mysterious behavior of CSS3 transforms on intricate rotations

When a div is viewed in perspective and has the transformations rotateY(-90deg) rotateX(-180deg), adding rotateZ(-90deg) to it results in a rotation of +270 degrees instead of -90 degrees. The new style of the div changes from transform: rotateY(-90deg) ...