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

Start the service without the need to include it as an argument

Looking at some older AngularJS code I've inherited, it appears like this: angular .module('MyModule') .directive('MyDirective', []) .controller('MyController', [ 'MyDependency', func ...

Quickly redesigning the appearance of file input using javascript and jquery. Seeking assistance to correct css issues on jsfiddle

Hey there, I've been working on styling the input[type="file"] element and could use some assistance. Like the saying goes, "One JSfiddle is worth 1000 words," so here's the link to my example: --- For more details, click here: http://jsfiddle.n ...

When utilizing a Slick carousel with three slides and setting autoPlay to true, the slider-nav behaves as if there are five slides

I am currently using the Slick carousel plugin and I want to feature a display of 3 photos. The issue is that when I set slidesToShow=2, everything works perfectly fine, but when I try to set slidesToShow=3, which equals the total number of slides, the bot ...

Tips on keeping the size of a react-bootstrap modal constant and enabling scrolling instead of expanding

<Modal size="lg" scrollable show={showInvModal} onHide={handleCloseInvModal}> <Modal.Header closeButton> <Modal.Title>Check out our latest items!</Modal.Title> </Modal ...

Issue with clearTimeout function not functioning properly on keyup event in iFrame

While there may be many similar questions out there, I have yet to find a solution that works for me. Currently, I am developing a WYSIWYG editor and I want it to save when the user performs a keyup action. However, I do not want it to update after every ...

AngularJS Date Formatting

I am facing an issue with binding a simple date object created in JavaScript to an input control using AngularJS. It seems like the problem might be related to the format of the date. Can you please help me understand why this happens? This problem specifi ...

Modifying the color of the tooltip arrow in Bootstrap 4: A step-by-step guide

How can I change the arrow color of the tooltip using Bootstrap 4? Here is my current code: HTML: <div class="tooltip-main" data-toggle="tooltip" data-placement="top" data-original-title="Service fee helps Driveoo run the platform and provide dedicate ...

Issue with the execution of Javascript code. Edit required

After creating a registration form that allows users to input their information for registration, I encountered an issue where if certain fields were left empty or if the user name was unavailable or the email address was already registered, a warning mess ...

An unexpected problem with text redirection that should not be happening

I am facing an issue with my HTML text field and post button on my website. Whenever I click the post button to make a post, it redirects to a separate PHP file called post.php which handles PHP and MySQL code for posting. However, I want to avoid this red ...

Assign values to several variables when ng-click event is triggered

Is there a smart way to assign values to multiple variables within an ng-click statement in a view without using a controller function? For instance, something like <li ng-click="showLeftDiv = true, showRightDiv = false, showBottomDiv = false"> I ...

What could be causing the value of response.name to be undefined in this situation?

Despite extensively searching through various StackOverflow threads, none of the suggested solutions seemed to work for my specific scenario. Upon analyzing the response using console.log(response), I received an "Object Object" message. I am puzzled as to ...

How can I use querySelector in JavaScript to target all div elements that have the same class?

I'm having an issue with the document.querySelector in my JavaScript code. It currently only selects the first div that contains the class "test", but I need it to select all such divs. Is there a way to achieve this using my existing JavaScript? ...

Looking to retrieve HTML elements based on their inner text with queryselectors?

I am looking to extract all the HTML divs that contain specific HTML elements with innerText = ' * ' and save them in an array using Typescript. If I come across a span element with the innerText= ' * ', I want to add the parent div to ...

Switch the display of a div within a ng-template loop using PrimeNg

Working with the PrimeNg picklist, I have encountered a challenge. Here's what's going on: The main focus is on the first row, while the other rows do not have radio buttons (as they are part of incomplete test data). The goal is to show a drop ...

Transforming an HTML file into a Vue.js component

I'm working on a login.vue file and I need to incorporate styling along with the necessary js and css files. Unfortunately, I'm clueless about how to achieve this within the login.vue file. Below is a snippet from the html file: <!DOCTYPE ht ...

Enhance your message inbox experience with jQuery/Javascript features inspired by Gmail, including the ability to select all messages with a checkbox and

It is truly satisfying to be here working on developing a private message inbox for my website, especially after successfully implementing a complete user signup/login and activation system. A few months ago, I never believed I had enough patience to grasp ...

Obtaining numerous distinct array elements

I have created a form with checkboxes for users to select options. <input type="checkbox" name="rights[]" value="1" class="a"> A <input type="checkbox" name="rights[]" value="2" class="b"> B <input type="checkbox" name="rights[]" value="3" ...

What situations call for utilizing $scope directly?

As a beginner in Angular, I have been diving into various tutorials to enhance my knowledge. Interestingly, the free tutorial at CodeSchool, which served as my introduction to Angular, does not mention the use of $scope. It appears that the controllerAs s ...

What is the best way to split text copied from a textarea into <p> paragraphs with an equal number of characters in each?

Check out this JSFiddle version I've found a JSFiddle example that seems perfect for my current project needs. However, I'm wondering how to modify the code to ensure that each paragraph is evenly divided with the same number of characters and a ...

Trouble getting AngularJS $scope arrays to populate with multiple PHP to MySQL queries

In my Angular controller, I used to fetch data from a PHP file that pulled one query from the database, stored it in a scope array, and displayed it on the webpage successfully. However, now I am trying to execute two queries in the same file. Each query ...