Tips for sorting an ng-repeat by text input and several select boxes

I possess an object named notification which includes the following properties:

EventName
EventDisplayname
SourceName
SourceDisplayname
Message

Utilizing a ng-repeat function, I am able to display notifications fetched from a webservice in a simple table.

In addition, there are 3 filters available:

  • events
  • sources
  • freetext

The HTML code is structured as follows:

<div style="float:left; width:33%;">
    <h2>Source-Filter</h2>
    <select id="SourceSelector" style="width: 250px;">
      <option value=""></option>
      <option ng-repeat="source in sources" value="{{source.SourceName}}">{{source.SourceDisplayname}}</option>
    </select>

    <h2>Event-Filter</h2>
    <select id="EventSelector" style="width: 250px;">
      <option value=""></option>
      <option ng-repeat="event in events" value="{{event.EventName}}">{{event.EventDisplayname}}</option>
    </select>
  </div>
  <div style="float:left; width:33%;">
  <h2>Freitext Filter</h2>
  <ul>
      <li><input type="text" style="width:300px" ng-model="search.text" placeholder="Enter text to filter..."/></li>
  </ul>
</div>

<tr ng-repeat="notification in notifications">
  <td>{{notification.SourceDisplayName}}</td>
  <td>{{notification.EventDisplayName}}</td>
  <td>{{notification.Message}}</td>
</tr>

I intend to apply explicit filtering to my ng-repeat using dropdowns and a freetextbox:

  • The freetext Textbox should only filter messages
  • The event dropdown should solely filter ng-repeats by the notification.eventname lists property
  • The source dropdown should exclusively filter ng-repeats by the notification.sourcename lists property

Is it feasible to have multiple explicit property filters for a ng-repeat and how can this be achieved?

Answer №1

angular
  .module('testApp', [])
  .controller('testCtrl', ['$scope',
    function($scope) {
      // Example data for notifications
      $scope.notifications = [{
        eventName: 'event2',
        sourceName: 'source1',
        message: 'This is notification 3'
      }, {
        eventName: 'event2',
        sourceName: 'source1',
        message: 'This is notification 2'
      }, {
        eventName: 'event3',
        sourceName: 'source3',
        message: 'This is notification 1'
      }, {
        eventName: 'event3',
        sourceName: 'source2',
        message: 'This is notification 3'
      }, {
        eventName: 'event2',
        sourceName: 'source3',
        message: 'This is notification 1'
      }];

      // Extract sources and events from notifications
      $scope.sources = $scope.notifications.map(function(n) {
        return n.sourceName;
      });
      $scope.events = $scope.notifications.map(function(n) {
        return n.eventName;
      });
      
      // Initialize notificationsFiltered with all notifications
      $scope.notificationsFiltered = $scope.notifications;

      // Watch for changes in filters and apply them to notificationsFiltered
      $scope.$watchGroup(['eventFilter', 'sourceFilter', 'freetextFilter'], function() {
        $scope.notificationsFiltered = $scope.notifications; // reset filtered notifications
        
        // Apply event filter
        if ($scope.eventFilter) {
          $scope.notificationsFiltered = $scope.notificationsFiltered.filter(function(n) {
            return n.eventName == $scope.eventFilter;
          })
        }
        
        // Apply source filter
        if ($scope.sourceFilter) {
          $scope.notificationsFiltered = $scope.notificationsFiltered.filter(function(n) {
            return n.sourceName == $scope.sourceFilter;
          })
        }
        
        // Apply text filter
        if ($scope.freetextFilter) {
          $scope.notificationsFiltered = $scope.notificationsFiltered.filter(function(n) {
            return n.message.indexOf($scope.freetextFilter) > -1;
          })
        }
      });
    }
  ]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>

<div ng-app="testApp" ng-controller="testCtrl">
  <!-- Source filter -->
  <select ng-model="sourceFilter">
    <option value="">No filter</option>
    <option ng-repeat="source in sources track by $index" value="{{source}}">{{source}}</option>
  </select>

  <!-- Event filter -->
  <select ng-model="eventFilter">
    <option value="">No filter</option>
    <option ng-repeat="event in events track by $index" value="{{event}}">{{event}}</option>
  </select>
  
  <!-- Text filter -->
  <input ng-model="freetextFilter" type="text">

  <!-- Display filtered results -->
  <table>
    <tr ng-repeat="notification in notificationsFiltered">
      <td>{{notification.sourceName}}</td>
      <td>{{notification.eventName}}</td>
      <td>{{notification.message}}</td>
    </tr>
  </table>
</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

"Encountering a problem with compression in Kafka while using the Node.js client with

I am currently utilizing the kafka-node library to consume data from Kafka. It appears that the data I am receiving is compressed with SNAPPY. How can I decompress this data once it has been retrieved? I attempted to use the node-snappy library for decompr ...

Resolving the Issue of Jerky Graphics During HTML5 Canvas Animation

When animating a layer in canvas, the visual quality becomes uneven if there are additional layers present. You can see an example of this by clicking "RUN" on this fiddle: http://jsfiddle.net/Q97Wn/ If I modify this line: opts.percentageIndicator.clearR ...

Is it possible for javascript to show the user's current location on a google map without using a KML layer?

I need help with loading a KML file using JavaScript and adding a marker to the map at the user's current location. The code I have been working on works fine, but it seems to be missing the geolocation check for the current position. How can I proper ...

Is there a way to incorporate the ACE code editor into a Vue project without relying on its built-in Vue

Just starting out with Vue and I'm looking to incorporate the ace code editor (this package) into my project. However, I want to avoid using the vue2-component & vue3-component versions for learning purposes. How can I achieve this? What's t ...

Tips for saving a web page using Selenium Webdriver in JavaScript

Is there a way to programmatically save an entire webpage using Selenium Webdriver JS in Firefox? I have been able to bring up the Save As dialog with the following code: driver.findElement(webdriver.By.tagName('html')).sendKeys(Key.CONTROL + &ap ...

Creating consistent web page designs: A guide

Having created multiple practice websites, I have successfully published one. However, I am now faced with the challenge of needing to make design changes. If I want to modify the text surrounding a certain element in every page, I find myself having to ...

Exploring the World of React JS by Diving into Backend Data

Let's consider an application that consists of three pages: "HomePage" "PrivatePage" "UserManagementPage" In addition, there is a file called "BackendCommunication.js" responsible for handling communication with the backend. "Homepage.js" import Re ...

`How can you generate navigation paths using the ngRoute module?`

I am currently working on creating a basic navigation system like the one illustrated below: html: <html ng-app="myapp"> <body> <ul> <li><a href="pages/sub1">sub1</a></li> <li><a href="pages/ ...

Utilize Javascript to perform operations on ndb.key()

When working with my application, I encounter keys that are created within a Python system and written to Google Datastore. To access the corresponding data, I require the IDs of these items, which are contained in the 'key' string. Unfortunate ...

Tips for verifying conditional input fields in a React component?

As a beginner in React, I attempted to create a SignIn form component that dynamically changes its layout based on a boolean prop toggled between Login and Signup options. In the signup version, there is an additional text field labeled Confirm password, w ...

The ngx-charts-line-chart is experiencing issues due to a failure in reading the property 'getColor' that is undefined

While setting up a basic example of a line chart, I encountered an issue with the getColor on undefined error. If I remove the [results] data, the chart displays but then runs into missing data problems. It's challenging to pinpoint exactly where the ...

Employ a pivot table to visually display an array of object data

I am attempting to utilize a pivot table to organize the data provided below: $.pivotUtilities.tipsData=[ {status:"open",year:2014,value:100.00}, {status:"open",year:2015,value:200.00}, {status:"open",year:2016,va ...

Issue with AngularJs Grid: Code does not function properly when placed within an event listener

Check out this code snippet: Functional version - Grid is displayed upon page load. <html ng-app="myApp"> <head lang="en"> <meta charset="utf-8"> <title>A Simple ngGrid Example</title> <link href="http://local ...

Having difficulty engaging with external AngularJS application using PhantomJS and Capybara

Having trouble interacting with an AngularJS third party application despite installing capybara-ng, capybara/angularjs and switching to the AngularJS DSL. My setup includes ruby2.0.3 and phantomjs-2.0.0-windows. The DOMs are not fully loaded, preventing ...

Creating an array of custom objects in JavaScript.Initializing custom objects in an

Is there a proper way to prevent the error "Cannot read property 'length' of undefined" when initializing an array of custom objects in javascript? situation export class MyClass implements OnInit { myArray: MyObject[]; } example <div ...

What is the best way to create a JavaScript function that hides a DIV element when clicked?

While creating a webpage, I included a fixed sidebar that is set to hide when clicked... I've created two smaller divs for the purpose of hiding and closing the sidebar (sideBarClose, sideBarOpen) Upon clicking these divs, they successfully hide the ...

Mastering the art of seamless navigation within a single page through the magic of

I have two HTML pages: "Login.html" and "index.html". Instead of a normal href linking, I want the user to navigate to "index.html" when they click on the login button. Furthermore, I want the index page to be displayed within the codes of login.html, cre ...

What is preventing me from accessing Angular's properties in JavaScript, like $error?

For some reason, I'm unable to access angular's Form members and I can't figure out why. My code is using angular 1.4. If you want to take a look at the full code, you can find it here: http://jsbin.com/lujojiduru/edit?html,js,console,output ...

Exploring the Power of Angular Toastr Callback Functions

Hey there! I'm currently working with Angular Toastr to display messages on my screen. I have a setup where only two messages can be open at the same time - one for errors and another for warnings. These messages are persistent and require user intera ...

Restrict the amount of items fetched when processing JSON information

Here is a snippet of JSON data that I am parsing using JavaScript. "credits":{ "cast":[{ "id":819,"name":"Edward Norton","character":"The Narrator","order":0,"cast_id":4,"profile_path":"/iUiePUAQKN4GY6jorH9m23cbVli.jpg" }, {"id":287,"name": ...