Using AngularJS to filter data on ng-repeat while also concealing the column header

I received an extensive JSON response from a REST service with information on numerous cities worldwide. The data structure is as follows (this is just a sample - typically it includes 100-200 places retrieved from the service and stored in the $scope item for easier comprehension):

$scope.cityData = [
            {
                "country": "Germany",
                "cities": [
                    {
                        "id": 1,
                        "name": "Berlin",
                        "country": "Germany",
                        "browserUrl": "berlin",
                        "clusterId": null
                    },
                    {
                        "id": 2,
                        "name": "Hamburg",
                        "country": "Germany",
                        "browserUrl": "hamburg",
                        "clusterId": null
                    },
                    {
                        "id": 3,
                        "name": "Munich",
                        "country": "Germany",
                        "browserUrl": "munich",
                        "clusterId": null
                    },
                    {
                        "id": 4,
                        "name": "Cologne",
                        "country": "Germany",
                        "browserUrl": "cologne",
                        "clusterId": null
                    }
                ]
            },
            {
                "country": "Switzerland",
                "cities": [
                    {
                        "id": 15,
                        "name": "Zurich",
                        "country": "Switzerland",
                        "browserUrl": "zurich",
                        "clusterId": null
                    },
                    {
                        "id": 16,
                        "name": "Geneva",
                        "country": "Switzerland",
                        "browserUrl": "geneva",
                        "clusterId": null
                    }
                ]
            },
            {
                "country": "Austria",
                "cities": [
                    {
                        "id": 19,
                        "name": "Vienna",
                        "country": "Austria",
                        "browserUrl": "vienna",
                        "clusterId": null
                    },
                    {
                        "id": 20,
                        "name": "Graz",
                        "country": "Austria",
                        "browserUrl": "graz",
                        "clusterId": null
                    },
                    {
                        "id": 20,
                        "name": "Linz",
                        "country": "Austria",
                        "browserUrl": "linz",
                        "clusterId": null
                    }

                ]
            }
        ];

Now, I aim to group this data on my interface in the following manner:

- Country
  ---- City
  ---- City
  ---- City
- Country
  ---- City
  ---- City
  ---- City

Additionally, I want to filter the display using an input field, so I have included the input and ng-repeat directives in my interface:

<input type="text" ng-model="cityFilter">
  <div class="row">
    <div class="col-xs-12" ng-repeat="c in cityData | filter: c.country">
      {{ c.country }}

       <div class="col-xs-12" ng-repeat="d in c.cities | filter: cityFilter">
        {{ d.name }}
       </div>
    </div>
  </div>

This setup works well, but the c.country title always appears regardless of the applied filter. How can I modify my HTML/directives so that the c.country disappears when the cityFilter does not match any city names within that country?

Appreciate your help.

Answer №1

Wrap the country in a span element and utilize ng-show.

<input type="text" ng-model="cityFilter">
<div class="row">
    <div class="col-xs-12" ng-repeat="c in cityData | filter: c.country">
        <span ng-show="(c.cities | filter: cityFilter).length > 0">{{ c.country }}</span>

       <div class="col-xs-12" ng-repeat="d in c.cities | filter: cityFilter">
          {{ d.name }}
       </div>
    </div>
</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

What is the proper method for adding a file to formData prior to sending it to the server using a

I came across this tutorial on FormData, but I'm still trying to grasp how the formData object functions. Input Form Example: https://i.stack.imgur.com/h5Ubz.png <input type="file" id="file-id" class="w300px rounded4px" name="file" placeholder=" ...

Designing geometric forms using the vertices of a Sphere in ThreeJs

My current project involves creating shapes based on vertices that have already been generated using a formula. I have successfully connected lines between these vertices using a threejs method, but now I want to take it a step further and create map tiles ...

Can you explain the meanings of <div class="masthead pdng-stn1"> and <div class="phone-box wrap push" id="home"> in more detail?

While working on styling my web pages with CSS and Bootstrap, I came across a few classes like "masthead pdng-stn1" and "phone-box" in the code. Despite searching through the bootstrap.css file and all other CSS files in my folders, I couldn't find a ...

Is Cookie-session the most ideal choice for React applications?

My NodeJS application currently authenticates users through a third-party app. Once the app retrieves user data, a cookie is generated and sent to the client for React to access the user data from that Cookie. Should I stick with using cookies or consi ...

Unable to successfully call a directive function from a nested child directive

I'm facing a challenge with utilizing a directive that was created by another developer to notify my directive when the ngRepeat inside of it has completed its creation. My parent directive has an isolate scope and includes a function within its scop ...

"Exploring the Possibilities of Expanding a Jquery UI Widget (Version 1.7

I am looking to customize the sortable widget, but I haven't been able to find accurate documentation. The best resource I came across was located here: . My initial attempt was: $.widget("ui.customsortable", $.extend($.ui.sortable, { _init: funct ...

What methods can I use to streamline integration with minimal lines of code?

Seeking help with JavaScript: Recently dived into JavaScript and managed to create the desired functionality for my navigation menu. However, I suspect that my code is not optimized as I find myself repeating lines unnecessarily. Here's a snippet of ...

Understanding the appropriate syntax to use with AngularJS is imperative

My devDependencies are as follows: "angular": "^1.5.0", "angular-ui-router": "^1.0.0-beta.1", I am looking to develop using AngularJS version that utilizes components and controllers as classes. However, I am unsure about the syntax to use for routi ...

Troubleshooting: Issue with XMLHTTPrequest functionality

I am attempting to make a request to a service using two different methods 1) Using XMLHTTPRequest -> Currently not functioning correctly Error message: JSON.parse unexpected end of data in line 1 col 1 function createCORSRequest(method, url) { ...

Troubleshooting a jQuery carousel: How can I prevent the thumbnails from automatically moving forward?

I am currently modifying a carousel where the thumbnails are moving out of frame. I need assistance in keeping them stationary, except for the blue thumbnail highlighter. To better illustrate the issue, I have created a jsFiddle here: http://jsfiddle.net ...

Sort the data by name rather than using the default JSON/C# format

On my webpage, I am displaying product data that is being called in the form of a JSON object in the JavaScript file: _loadAll: function (callback) { $.ajax({ 'type': 'GET', 'timeout': 5000, ' ...

A Guide on How to Sort and Tally Items in a JSON Data Set containing Numerous Objects using JavaScript

I have recently started learning javascript, and everything was going well until I encountered this issue. I need to reformat a JSON file in the following structure: { firstName: "Joel", lastName: "Munz", total: 154, transaction: "111111", ...

Tips for transitioning frontend JS to Angular 2 for seamless integration with a PHP MVC backend system

I currently have a robust PHP MVC web application that utilizes jQuery for click events and modal dialog rendering. To align with up-to-date frontend technologies, I am looking to revamp the JavaScript code to function within Angular 2. However, I am faced ...

Incorporate ajax into the JavaScript geocoder functionality

After retrieving the latitude and longitude coordinates from the maps, I am trying to send the address to another PHP file using Ajax in JavaScript. Unfortunately, the Ajax is not working properly for me. I am puzzled as both the Ajax code and the JavaScr ...

"Resetting count feature in AngularJS: A step-by-step guide

I have a list consisting of four items, each with its own counter. Whenever we click on an item, the count increases. I am looking to reset the counter value back to zero for all items except the one that was clicked. You can view the demonstration here. ...

Converting a database query result into a JavaScript variable: A step-by-step guide

I've been struggling with this problem for a day now and I feel like giving up. My main goal is to export the query result as a string (specifically dataString) so that I can easily import it as a string in my external .js file. module.exports.getKl ...

"Experiencing a problem with Next.js 13 where the User Context is not functioning properly outside of _app

When using Next.js 13 and the user context, I've noticed that it doesn't function properly outside of _app.tsx. Any idea why? src>context>UserPositionContext.tsx import { createContext, useContext, useState } from "react"; const ...

When working with AngularJS routing, utilize a function for the templateUrl to dynamically load the appropriate template

Can a function be used as the templateUrl in angularjs routing? The $routeProvider official documentation states: templateUrl – {string=|function()=} Check out the $routeProvider official documentation here In javascript, a function is typically def ...

Troubleshooting recursive function errors in a Javascript menu

//check out this current jsfiddle for more details: http://jsfiddle.net/0ht35rpb/45/ I'm attempting to create a loop through a JSON navigation tree in order to match the alternative language counterpart when a user navigates to a specific page. //H ...

Exploring the World of AngularJS HTTP Interceptors

Do HTTP Interceptors in AngularJS affect performance? I am interested in intercepting requests and adding the absolute URL: angular.module('app').config(['$httpProvider', function ($httpProvider) { $httpProvider.interceptors.push( ...