How to Filter Fields in AngularJS Based on a Start Date and End Date?

Hey everyone, I'm trying to filter items based on the start and end dates using the daterange functionality in my meanjs app. I've tried multiple approaches but haven't found a solution yet. If anyone knows how to do this, please help me out. Check out My Plunk for reference.

  • Please take a look at my Plunker for reference.

  • I am displaying the Due_date, which is the field I want to use for filtering.

  • I have implemented some functionality related to invoice_Date and terms, which results in the corresponding Due_date. For example: invoice_date : 2016-09-10, terms : 6, the resulting Due_date : 16-09-2016

  • What I am exactly looking for is to filter the Due_date as a start date and end date. For instance, if we select start date as 16-09-2016 and end date as 25-09-2016, only transactions within these dates should be displayed or filtered in the table. I have attempted to use the daterange filter to achieve this solution, but haven't been successful. Please assist me.

  • The daterange filter works fine when using ng_module as invoice_date, but we are not sure how to filter the Due_date field. Your help would be greatly appreciated. Check out My Plunker for more details.

Controller:

.filter('dateRange', function() {
     return function(records, dateKey, from, to) {
       return records.filter(function(record) {
         return !moment(record[dateKey], 'YYYY-MM-DD').isBefore(moment(from))
                && !moment(record[dateKey], 'YYYY-MM-DD').isAfter(moment(to));
            });
        }
    })

Html:

<input type="date" class="form-control" name="from" ng-model="from">

<input type="date" class="form-control" name="to" ng-model="to">

Filter:-

ng-repeat="data in  record | dateRange : 'invoice_date' : from : to"

The following field needs to be filtered in the table:

Due_date:-

<td> {{addDays(data.invoice_date,data.terms) | date:'dd-MM-yyyy'}}</td>
  • I have provided a plunker for reference: My plunker

Answer №1

If you want to customize a specific filter, follow these steps:

HTML

 <tr ng-repeat="data in  records | customFilter:from:to">
   <td> {{data.supplier_name}}</td>
   <td> {{data.invoice_date}}</td>
   <td> {{data.terms}}</td>
   <td> {{addDays(data.invoice_date,data.terms) | date:'yyyy-MM-dd'}}</td>
</tr>

JS

 app.filter("customFilter", function() {
      return function(items, from, to) {
            var start = from;
            var end =to;
            var filteredItems = [];   

            for (var i=0; i<items.length; i++){
                var date = new Date(items[i].invoice_date);
                date.setDate(date.getDate() + parseInt(items[i].terms));
                var tempDate = date;
                if (tempDate > start && tempDate < end)  {
                    filteredItems.push(items[i]);
                }
            }   
            return filteredItems;
      };
    });

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

Question about Selecting Boxes

I'm looking for the name of the HTML tool that is used to create a list of objects from a select field. It typically consists of two boxes with an arrow in between them. For example, when creating a group, Box A would contain a list of users and you c ...

AngularJS - Displaying the initial content in the <ui-view> container

Can default code be placed inside a <ui-view> element without any issues? It seems to work for me, but I can't find any definitive information on whether it's acceptable or not. Currently, I have set up the "default" view to display a list ...

Steer clear of directly accessing views in AngularJS using angular-ui-router

In my AngularJS App setup, I have the following configuration: angular .module('MyApp') .config(['$stateProvider', '$urlRouterProvider', '$locationProvider', function($stateProvider, $urlRouterProvi ...

Message displayed within Ng-repeat loop

Having trouble implementing a tooltip within an ng-repeat for each item in a td element. Whenever the mouse hovers over an item inside the td, I want a tooltip to display with more details. The code below shows my setup, using ng-if to prevent displaying e ...

Button inside table cell not showing Bootstrap tooltip

I've implemented a feature where each row in a table has a button that triggers a pop-up modal when clicked. I want these buttons to display tooltips when hovered over and when focused. Below is an example of the HTML structure: <link hr ...

Exploring the origins of CSS through patch design

Want to learn how to create a background like this using CSS? Check out this link for inspiration! ...

Do form validations affect the values assigned to ng-model objects?

If a form has the structure below: <form> <input type="text" required ng-model='myValue' ng-maxlength='5'></input> {{myValue}} {{myValue.length}} </form> Is there a way to prevent the model from b ...

Tips for creating a two-tier selection filter system

I'm having an issue with this code. My objective is to create two filters for this table. The select element with id="myInput" should determine which rows appear in the table and apply the first filter. Here is the JavaScript code: function myFunctio ...

Is there a feature on GitHub that allows for the rendering of HTML code similar to a compiler?

When using Jupyter notebook, I've learned that you can access compiled content like this - https://github.com/ctgk/PRML/blob/63499fffa5c19ec58ece35ed51692ff64112e4dd/notebooks/ch01_Introduction.ipynb by using .ipynb files. However, as a newcomer to Gi ...

Encountering a NaN outcome when summing values from various select options

I'm working on a project that involves adding up the prices based on the surface chosen by the user. I'm struggling with calculating the partial cost when the user's choice changes. totalSum.ts num: Calculation totalAmount: number cate ...

Fetching data using HTML script is not possible

Struggling to retrieve data from mongoDB and send it to the client side using res, but I keep getting undefined. Can anyone offer some assistance? Thank you all! //// server.js app.get('/user/show', userController.userList); //// controller cons ...

Activate browser scrollbar functionality

Below is the HTML code snippet: <html> <head> <style> #parent { position : absolute; width : 500px; height : 500px; } #top { position : absolute; width : 100%; height: 100%; z-index : 5; } #bottom { position : absolute; width : 100%; ...

Can someone help me locate the file using the inspect element feature?

Today, I encountered a small issue on my website that I wanted to fix. Although I was able to make the necessary changes using Inspect Element, I couldn't locate the file where it needed to be changed. The website in question is gesher-jds.org/giving. ...

Issue with retrieving checkbox values in AngularJS using Bootstrap Toggle

For my AngularJs project, I am incorporating Bootstrap Toggle by using a custom Angular directive that I created specifically for this plugin. myApp.directive('iosToggle', function(){ return { restrict: 'A', link: f ...

What are the implications of declaring a controller as a variable within itself or utilizing $scope?

As I dive into learning and utilizing AngularJS, certain concepts still remain unclear to me. Here is my query: within my application, there is a controller that utilizes $http to fetch data from the backend upon initialization. Following a comprehensive ...

The inclusion of the <div> tag disrupts the functionality of the Material UI Grid

While attempting to enclose my element within a <div> that is nested inside the <Grid>, I realized that this was causing the <Grid> layout to break! Surprisingly, when I tried the same approach with Bootstrap grid system, this issue did n ...

What is the reason behind needing to set both top/bottom and left/right to 0 so that my body::before can effectively cover the entire page?

I am currently diving into the world of front-end web development and I am having some trouble grasping the concept behind the behavior of body::before. Below is a snippet of my CSS code: body::before { content: ""; position: fixed; width: ...

Issues with AngularJS Opening the Datepicker upon Click

Currently, I am developing an application using AngularJS, JQuery, and Bootstrap. In this project, I have incorporated a customized date range picker from www.daterangepicker.com. Issue: The problem arises when I attempt to open the date range picker by c ...

When using JQuery's .each method, only the first two elements from an array of automatically generated elements are retrieved

I have a loop that creates multiple divs with the class panel. @for(comment <- event.getCommentsSorted()) { However, when I try to manipulate each of these divs using jQuery's .each, only the first two divs are selected. $(window).on('load& ...

Exploring the depths of nested parameterized routes using ui-router in AngularJS

I've been exploring the concept of "nested parameterized routes" in search of a solution, but so far I haven't found exactly what I need. My goal is to establish user-friendly routes for my API, similar to the examples below: /api/v1/project/li ...