Using AngularJS filters to narrow down content within an ng-repeat

Check out this JSON code example:

 $scope.info = [{"name":"Category1", "data":[{"name":"Item1"}, {"name":"Item2"}]},
                    {"name":"Category2", "data":[{"name":"Item3"}, {"name":"Item4"}]}];

I have utilized ng-repeat to display the list and a search box to filter, as well sorting the results by categories :

<div ng-repeat="Cat in info">
      <h3>{{Cat.name}}</h3>
      <ul>
          <li ng-repeat="Item in Cat.data | filter:search" >
               {{Item.name}}
          </li>
      </ul>
</div>

An issue arises when searching for an item like "Item3", it displays under Category2 but Category1 remains visible even if empty because only the content is being filtered, not the categories themselves.

To address this, how do I instruct AngularJS to hide a category if its filtered content is empty?

Answer №1

To make the header dynamically hide based on the filtered data length, save the filter output to a variable.

<div ng-repeat="Cat in info" ng-hide="filtered.length == 0">
  <h3>{{Cat.name}}</h3>
  <ul>
      <li ng-repeat="Item in filtered = (Cat.data | filter:search)" >
           {{Item.name}}
      </li>
  </ul>
</div>

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

Exploring the concept of jQuery handler functions

Within my code, I have the following: <script type="text/javascript"> $(document).ready(function (e) { $('#EventCreate').click(function (e) { location.href = '@Url.Action("Create", "AEvents")'; }); ...

VueJS: Incorporating a Computed Property within a v-for Loop

Is there a way to utilize computed properties in lists while working with VueJS v2.0.2? Check out the HTML snippet below: <div id="el"> <p v-for="item in items"> <span>{{fullName}}</span> </p> </div> A ...

Calculating the total value in a Laravel database

Is there a way for me to calculate the number of apartments in a project based on the floor they are located on? This is db $table->id(); $table->unsignedBigInteger('project_building_id')->nullable(); $table->unsignedBigInteger(&apos ...

Rename multiple files in bulk

With 10000 files consisting of php, html, css, and png formats that are interconnected to operate the website's engine, I am looking for a way to perform bulk renaming in order to ensure proper functionality. Not only do I need to rename the actual fi ...

Is compiling inline sass possible with npm?

Looking for a simple method to achieve this task? I've experimented with sass, node-sass, and tinysass without success. My goal is to compile inline sass in JavaScript, much like the code snippet below: import sassPkg from 'sass-pkg' const ...

Encountering problems with createMediaElementSource in TypeScript/React when using the Web Audio API

Currently, I am following a Web Audio API tutorial from MDN, but with a twist - I am using TypeScript and React instead of vanilla JavaScript. In my React project created with create-react-app, I am utilizing the useRef hook to reference the audio element ...

Customize Bootstrap Vue dropdown without any predefined styling options

After reviewing the documentation, I created a sample example utilizing the b-dropdown component: You can view the example here: https://codesandbox.io/s/6lhk6?file=/src/components/GenericItem.vue However, when I implemented the component in the code: &l ...

How come the light position is not updating?

I am currently using the three.js library to create an animated cylinder: let renderer, camera, scene, light, cylinder; initialize(); animate(); function initialize() { renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true }); renderer ...

"Error encountered: Module not found - Issue resolving module" while using signrequest-client in Angular

I've been attempting to integrate the signRequest API into my angular application. To do so, I first installed the signrequest-client using the following command: npm install signrequest-client --save Following this installation, I included the Ja ...

Creating custom shaders for YouTube videos within a Three.js userscript

I want to add a shader effect to YouTube videos. My current approach involves using Three.js to implement a shader on a video. Specifically, I am trying to adapt this example of applying a shader to a video (code available here) into a Tampermonkey usersc ...

Tips for avoiding duplicate elements in ASP.NET during postback

My issue is that I have a div with the ID "mydiv" and runat=server. <div ID="mydiv" runat="server"></div> I move mydiv to a Container using jQuery. $("#mydiv").appendTo('#Container'); After a PostBack, my div gets duplicated and I ...

What is the process for displaying images fetched from the API?

Currently, my front-end is built using React/Redux and the API side with AdonisJS. All of my images are stored within the API, and I need to find a way to display them on the front-end. Can anyone provide guidance on accomplishing this task? ...

What is the best method for importing a single module within a monorepo project using JavaScript and NPM?

I've organized my codebase into a monorepo with the following structure: ➜ yw git:(master) tree . ├── package.json ├── packages │ ├── common │ │ ├── package.json │ │ ├── src │ │ │ ├─ ...

Ways to conceal elements when a router link is clicked

Within my app.js, I have the App.js where I aim to display certain components upon clicking on the router. Below is the code snippet from my App.js: import React from 'react'; import {BrowserRouter, Route} from 'react-router-dom'; impo ...

What is the best way to send parameters in AngularJS?

I am currently retrieving the geolocation of the user using this method, and it is functioning successfully as shown in the view with {{lat}} and {{lng}}: (function () { var showController = function($scope, $http, $routeParams) { ...

Firestore Query sends data object to browser

When making a Firestore query, the browser is displaying [object Object],[object Object] instead of the expected output: router.get('/jobopportunities', async(req, res) => { var jobArray = []; const snapshot = await fireba ...

React JS functionality does not support Bootstrap tooltips

I'm attempting to implement a tooltip in my React app, but I'm experiencing issues with it not displaying properly. I am utilizing vanilla Bootstrap for this purpose. I've included the following script tag in my index.html file to import the ...

Using AJAX to retrieve a specific JSON object from an array of JSON data

Data retrieved in JSON array from the API: [{"id":"001", "name":"john", "age":"40"}, {"id":"002", "name":"jane", "age":"30"}] Using Ajax: $.ajax({ url: 'interface_API.php', ...

Discontinuing the mobx autorun feature upon componentWillUnmount

In my componentDidMount, I have the following autorun function: componentDidMount() { this.autoUpdate = autorun(() => { this.setState({ rows: generateRows(this.props.data) }) }) } Unfortunate ...