Using AngularJS to filter ng-repeat based on object properties that are empty

I am working with an array of objects that have multiple properties. Each object has an object.comment property, which may either be filled with a string ('comment' : 'comment text'), or empty ('comment' : '').

To display the results, I use ng-repeat as shown below:

<div class="row msf-row" 
     ng-repeat="record in filteredRecords = (recordlist | filter:dateFilter | filter: search )" 
>

My goal is to add a checkbox filter that will show only the results where the object.comment property is filled when the checkbox is checked, and show all results when it is unchecked.

Here's my current filter setup:

<form role="form">
            <div class="form-group col-md-3">
              <input type='daterange' 
              placeholder="Date range" 
              class="form-control"
              format="DD/MM/YYYY" 
              ng-model="dates"
              ranges="ranges" />
            </div>

            <div class="form-group col-md-1">
              <input class="form-control" placeholder="Time" ng-model="search.time">
            </div>

            <div class="form-group col-md-1">
              <input class="form-control" placeholder="Car" ng-model="search.car">
            </div>

            <div class="form-group col-md-2">
              <input class="form-control" placeholder="Driver" ng-model="search.driver">
            </div>

            <div class="form-group col-md-2">
              <input class="form-control" placeholder="From" ng-model="search.from">
            </div>

            <div class="form-group col-md-2">
              <input class="form-control" placeholder="Destination" ng-model="search.destination">
            </div>

            <div class="form-group col-md-1">
              <input class="form-control" placeholder="Pax" ng-model="search.pax">
            </div>

            <div class="col-md-1">
              <div class="checkbox">
                <label>
                  <input type="checkbox" 
                         ng-model="search.cancelled" 
                         ng-change="search.cancelled = search.cancelled ? true : undefined"
                  > Cancelled 
                </label>
              </div>
            </div> 

            <div class="col-md-2">
              <div class="checkbox">
                <label>
                  <input type="checkbox" 
                         ng-model="search.comment" 
                         ng-change="search.comment = search.comment ? true : undefined"
                  > Commented records
                </label>
              </div>
            </div>

</form>

While I have successfully implemented a filter based on whether the object.cancelled property is true or false, I am struggling to do the same for the object.comment property being empty or containing a string.

Could anyone provide some assistance?

Answer №1

If you want to create a custom filter, check out the demo below:

app.filter('emptyString', [
  function() {
    return function(input, param) {
      if (!param) return input

      var ret = [];
      if (!angular.isDefined(param)) param = true;

      if (param) {
        angular.forEach(input, function(v) {
          if (angular.isDefined(v.comment) && v.comment) {
            v.comment = v.comment.replace(/^\s*/g, '');
            ret.push(v);
          }
        });
      }

      return ret;
    };
  }
])

var app = angular.module('app', []);

app.controller('homeCtrl', function($scope) {

  $scope.recordlist = [{
      time: "10/11/2014",
      comment: "super"
    }, {
      time: "10/11/20004",
      comment: ""
    }, {
      time: "10/11/2005",
      comment: ""
    }, {
      time: "11/1/2014",
      comment: "that was ok"
    }

  ];

});

app.filter('emptyString', [
  function() {
    return function(input, param) {
      if (!param) return input

      var ret = [];
      if (!angular.isDefined(param)) param = true;

      if (param) {
        angular.forEach(input, function(v) {
          if (angular.isDefined(v.comment) && v.comment) {
            v.comment = v.comment.replace(/^\s*/g, '');
            ret.push(v);
          }
        });
      }

      return ret;
    };
  }
])
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" type="text/css" />

<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script>

<div ng-app="app">
  <div ng-controller="homeCtrl">
    <form role="form">
      <div class="form-group col-md-3">
        <input type='daterange' placeholder="Date range" class="form-control" format="DD/MM/YYYY" ng-model="dates" ranges="ranges" />
      </div>

      <div class="form-group col-md-1">
        <input class="form-control" placeholder="Time" ng-model="search.time">
      </div>

      <div class="form-group col-md-1">
        <input class="form-control" placeholder="Car" ng-model="search.car">
      </div>

      <div class="form-group col-md-2">
        <input class="form-control" placeholder="Driver" ng-model="search.driver">
      </div>

      <div class="form-group col-md-2">
        <input class="form-control" placeholder="From" ng-model="search.from">
      </div>

      <div class="form-group col-md-2">
        <input class="form-control" placeholder="Destination" ng-model="search.destination">
      </div>

      <div class="form-group col-md-1">
        <input class="form-control" placeholder="Pax" ng-model="search.pax">
      </div>

      <div class="col-md-1">
        <div class="checkbox">
          <label>
            <input type="checkbox" ng-model="search.cancelled" ng-change="search.cancelled = search.cancelled ? true : undefined">Cancelled
          </label>
        </div>
      </div>

      <div class="col-md-2">
        <div class="checkbox">
          <label>
            <input type="checkbox" ng-model="search.comment">Commented records
          </label>
        </div>
      </div>

    </form>
    <div class="container">
      <div class="row " ng-repeat="record in filteredRecords = (recordlist |  emptyString : search.comment   )">
        <div class="col-xs-12" <span class="label">{{record.time}} <span> <strong>{{record.comment}}</strong></p> </div>
    </div>
    </div>
  </div>

Answer №2

To solve this issue, you can utilize the ng-hide directive:

<div class="row"
    ng-repeat="record in filteredRecords"
    ng-hide="search.comment && (!record.comment || record.comment === '')">
</div>

This code will hide the element if search.comment is selected and record.comment is not defined or empty.

You can view a working example on Plunker: http://plnkr.co/edit/m8rA0rVxqgWS0NWukJdf?p=preview

Does this meet your expectations?

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

Using TypeScript to extend functionality from Array

I am currently working on designing a robust data model for an AngularJS application, and I am interested in having my model inherit from Array<BaseModel>. However, I have not yet discovered a foolproof way to accomplish this. In a hypothetical scen ...

Is the spread operator in React failing to function as anticipated?

In my current project, I encountered an issue while trying to pass a GeolocationCoordinates object to a child component using the spread operator. Strangely, in the child props, it appears as an empty object: interface HUDState { geoCoords: Geolocation ...

What is the best way to locate a user-provided string within word boundaries using JavaScript regex?

Employing JavaScript, I am currently searching a body of text. Users are given the option to input any string they desire, and then I aim to search for that specific string, ensuring it is considered a "whole word" located between boundaries. All I need i ...

Utilize an external JavaScript file function within an AngularJS controller

I have an external JavaScript file with additional functions that I am trying to call from an Angular controller. Here is an example of the external.js file: ... ... function fun() { ... ... } ... ... The controller in question is called acccountCon ...

Hold off on creating the directive until the page has fully loaded and is operating smoothly

I'm currently developing a one-page application, but I'm facing performance issues with certain parts that are running too slow. The sluggishness is mainly due to the fact that I'm displaying 400 complex elements in a repeater for users to s ...

An easy way to insert a horizontal line between your text

Currently, I have two text responses from my backend and I'm considering how to format them as shown in the design below. Is it possible to automatically add a horizontal line to separate the texts if there are two or more broadcasts instead of displa ...

Tic-Tac-Toe: The square's value stays unchangeable

Currently, I am working on creating a tic-tac-toe game using pure vanilla Javascript, so I am aiming to keep it as simple as possible. I have run into an issue and need some guidance. The main requirement is that once a square has been clicked and filled ...

Form values not being transmitted correctly in Ajax POST request

I'm facing an issue with my form submission through AJAX where the values are coming out incorrect. Strangely, when I use a normal POST submit it works perfectly fine! <script type="text/javascript"> $(function () { $('#B ...

Misunderstandings between HTML and CSS

Can someone please clarify how the functionality of the active-slide class? Code: <div class="slider"> <div class="slide active-slide">..</div> <div class = "slide slide-feature">..</div> <div class = "slide">..</di ...

Can we implement attribute selectors in Material-UI while utilizing makeStyles?

Is it possible to extract all the div elements with specific Mui class names such as access-MuiPickersCalendarHeader-switchHeader, access-MuiPickersDay-day? My components are styled using StylesProvider which adds "access" as a prefix to the original Mater ...

In JavaScript, the code is designed to recognize and return one specific file type for a group of files that have similar formats (such as 'mp4' or 'm4v' being recognized as 'MOV')

I am working on a populateTable function where I want to merge different file types read from a JSON file into one display type. Specifically, I am trying to combine mp4 and m4v files into MOV format. However, despite not encountering any errors in my code ...

Looking for a way to update a world map image by selecting multiple checkboxes to apply a flood fill color to different countries using Mogrify

Exploring different methods to achieve my goal, I am wondering if creating a web service where users can track visited countries on a world map can be done in a simpler way than using Javascript or Ajax. The idea is for users to mark the countries they hav ...

Ways to navigate back to the homepage if a parameter is not detected in Angular's ui-router

I am working with Angular ui-router and have set up the following routes. When navigating to /3872, it successfully displays a user with that ID and the correct template, which is perfect. However, the issue arises when entering an ID that does not exist ...

Having trouble generating a readable output bundle due to the following error message: "The entry module cannot be found: Error: Unable to resolve './src/index.js'"

I'm currently working with Webpack version 6.14.8 within an Asp.net Core Razor Pages project in Visual Studio 2019. My objective is to generate a readable output file, and here's the directory structure I've set up: |-->wwwroot -----> ...

Is it possible for JavaScript to detect elements or scripts within a hidden element using display="none"?

Is it possible for scripts to locate elements that have been hidden in the DOM by setting their attribute to display="none"? ...

pause the timer once the interval reaches 0

In order to accomplish the task of stopping the timer once it reaches 00:00 and displaying an alert message saying "countdown stopped", I need to implement a function that will handle this. The timer should start again after the stoppage occurs, which mean ...

Turn off choices by utilizing data type attribute values within select2 version 4

I'm attempting to deactivate the options by using the data-type attribute specified for each option in select2. Unfortunately, my attempts have been unsuccessful thus far. In addition, I am encountering this error within the change event handler: ...

Creating a secured page with Node.js and Express: Password Protection

I am a beginner with node.js/express! Currently, I am working on developing a chat site along with a chat admin site. When I navigate to _____:3000/admin, it prompts me for a password through a form. I am looking for a way to verify if the entered passwor ...

Angular JS Error - $injector:modulerr: Module Not Found

Attempting to preview an angular application on a local server using npm, bower, and grunt. The code being used is identical to what a colleague is successfully running from a cloned repository. However, while they can view the full app without issues, I e ...

What is the process for transferring a Python variable to JavaScript when the Python code is a cgi script?

I am currently working on a JavaScript file where I am attempting to assign a variable based on data received from a Python CGI script. My approach involves using the Ajax GET method to retrieve the data and set the variable within that method. Below is a ...