Is the length of a complex match in Angular JS ng-if and ng-repeat greater than a specified value?

In my code, there is an ng-repeat that generates a table based on one loop, and within each row, another cell is populated based on a different loop:

<tbody>
   <tr ng-repeat="r in roles | limitTo: 30">
     <td>{{r.name}}</td>
     <td>
        <span ng-repeat="t in users100 | limitTo: r.userLimit" ng-if="t.role == r.name"><a href="{{t.id}}">{{t.full_name}}</a></span> 
        <span ng-if="true"><a href="" ng-click="r.userLimit=500">Show all</a></span>
     </td>
   </tr>
 </tbody>

Each row represents a Role, with the first cell displaying the role's name.

The second cell displays results from a secondary dataset where the role matches the original set's role value. It repeats a SPAN element for each match up to a limit of r.userLimit, which is initially set to 20 in the JSON data.

The link within the A tag triggers an action that sets the value of r.userLimit to 500, displaying all users.

The issue arises when I only want to display this link if the number of matching items in the initial repeater exceeds the value of r.userLimit (which is 20).

I attempted to use the following syntax:

ng-if="((t.role == r.name).length > r.userLimit)"

However, the link doesn't appear. What am I doing wrong with my syntax?

Answer №1

To get the original array and the filtered one, you will need to extract 2 lengths:

Using ng-repeat, here is how you can achieve this:

<span ng-repeat="t in shownUsers = (users100 | limitTo: limit)">

If you are using Angular 1.3, there is an even simpler way:

<span ng-repeat="t in users100 | limitTo: limit as shownUsers">

You can then utilize these two with ng-if for "show all". Avoid setting a very high number for "all":

<span ng-show="shownUsers.length < users100.length" ng-click="limit = users100.length">
   Show All
</span>

As an additional feature, this setup allows for easy implementation of "show more":

<span ng-show="shownUsers.length < users100.length" ng-click="limit = limit + 10">
   show more
</span>

NOTE: If the results are already filtered, using the as alias may not be sufficient. In such cases, you can use the following approach (shortened variable names for better readability):

<span ng-repeat="t in shown = (filtered = (users | filter: {role: r.name}) | limitTo: limit)">

Then compare shown.length to filtered.length:

<span ng-show="shown.length < filtered.length" ng-click="limit = users.length">
   show all
</span>

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

Dealing with all errors across all pages in Angular using a comprehensive error handling approach

I am currently using AngularJS and attempting to capture all errors across all pages. However, I am encountering an issue where it does not catch errors in some instances. For example, when I trigger a ReferenceError in one of the controllers, it is not be ...

The URL is not being updated despite changes in document.location hash

I have created a script that toggles (show/hide) between different DIVs on the page (highlighted below in the various boxes =). However, I am facing an issue with updating the URL string when switching between different DIVs. For instance, if I navigate t ...

generate a cookie within a pop-up window

Can someone help me figure out why my Modal Window with a cookie to display only once isn't working? I followed this tutorial at . Here is the link to my website: Here is my code: <div class="modal fade" id="myModal" tabindex="-1" role="dialog" a ...

The no-unused-expressions rule is triggered when an assignment or function call is expected

I'm just starting out in full stack development and I'm experimenting with coding to improve my understanding of frontend using React JS and Material UI. While working on this component, I encountered an error in the console at line 75 (this.prop ...

Annotation for inserting code inline

I'm new to angularjs and struggling to understand the explanation in the angular documentation about the role of the $scope as an array. Can someone explain what inline injection annotation means in this code snippet? var myApp = angular.module(&apos ...

Updating React state by changing the value of a specific key

I've been diving into React lately and I'm facing an issue with updating state values for a specific key. Below is my current state: this.state = { connections : { facebook : "http://facebook.com", flickr : null, ...

Trouble with Bootstrap Modal not closing properly in Chrome and Safari

ISSUE: I'm facing a problem where clicking on the X (font awesome icon) doesn't close the modal popup as expected. LIMITED FUNCTIONALITY ON CERTAIN BROWSERS: Currently, the X button for closing the modal works only in IE and Firefox. However, i ...

Tips for retrieving a child component's content children in Angular 2

Having an issue with Angular 2. The Main component displays the menu, and it has a child component called Tabs. This Tabs component dynamically adds Tab components when menu items are clicked in the Main component. Using @ContentChildren in the Tabs comp ...

Bidirectional Data Binding Using Meteor and React

When using Meteor, the getMeteorData() method is preferred over getInitialState(). But how can we achieve binding, especially with regards to a user's surname in their profile? ... getMeteorData() { return { u = Meteor.user() } }, componentRen ...

The React.js .map function encountered an error while trying to map the results from Firebase

As a newcomer to the realm of React and Firebase, I find myself struggling with arrays and objects. It seems like the way my data is formatted or typed does not play well with the .map method. Despite scouring Stack Overflow for answers, none of the soluti ...

What is the best method to retrieve the window object in XUL?

I'm attempting to assign an onLoad event to the current webpage through a Firefox extension. My approach involves utilizing the gBrowser object, although I am uncertain if this is the most optimal method. The goal is to establish an onLoad event for t ...

Using Node.js to send a photo through a Telegram Bot

I've been trying to send a photo via node.js using this function, but it's not working. telegram-bot-api https://www.npmjs.com/package/telegram-bot-api var telegram = require('telegram-bot-api'); var api = new telegram({ token: & ...

How can you ensure that links on Ajax-loaded pages direct users to their intended destinations?

The issue at hand: When clicking on links within dynamically loaded pages, the desired dynamic loading of other pages does not occur. Within my index page, I have links structured like this: <li><a class="page_one" title="page_one" href="page_on ...

Displaying a MySQL blob image in an HTML file with Vue.js: A step-by-step guide

Here is a Vue file that I have: export default { data(){ return{ info: { name: '', image: '', }, errors: [] } }, created: function(){ thi ...

Having trouble launching the freshly developed Angular app

I'm encountering an issue with my newly created app - I can't seem to launch it. Error: The loader “C:/C#/Angular/my-app/src/app/app.component.css” is not providing a string as expected. I've attempted reinstallation of Angular and Nod ...

Setting the default selection in AngularJS based on the URL entered

I've encountered an issue with a dropdown menu in my AngularJS version 1.4 application. The dropdown menu contains links to different sections of the page. The problem arises when I directly enter the page URL - instead of selecting the correct link f ...

Looking to showcase the outcome of the Procedure invocation when I made the call?

{ "isSuccessful": true, "resultSet": [ { "name": "pradeep", "password": 123, "timestamp": "2014-04-08T12:58:45.000Z" }, { "name": "dileep", "password": 1234, "timestamp": "2014-04-08T13:00:52.000Z" } ] } I have ...

What is the proper way to utilize BrowserRouter when child routes are connected to components?

I recently started learning React and decided to create a signup form following a tutorial on React Tutorial. However, the tutorial turned out to be outdated. I attempted to implement browser redirects for the signup, login, and home page links by defining ...

What is the best way to upload my React project to GitHub without adding the node modules directory?

I'm looking to share my React Project on GitHub, but I don't want to include the node modules folder. What's the best way to go about this? ...

An advanced template system incorporating dynamic scope compilation

My project requires a unique solution that cannot be achieved with standard data-binding methods. I have integrated a leaflet map that I need to bind with a vue view-model. While I was able to display geojson features linked to my view, I am facing chall ...