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

"Integrate a URL segment into the primary URL with the help of AngularJS or JavaScript

One interesting case involves a URL path that is structured in the following way. http://localhost:12534/urlpart1/urlpart2?querystring=140 The challenge here is to replace "urlpart2" with "urlpart3" using either javascript or AngularJS. One approach is t ...

When taking a vertical picture on my iPhone, my Vue component does not function properly

Having trouble with displaying recently uploaded images on a form. If the image is taken from a phone, it may have an exif property that browsers do not handle correctly, causing the picture to appear flipped or sideways. Upside down photo To fix this is ...

Execute a zoom out action by pressing the (Ctrl) and (-) keys simultaneously in Javascript

I'm trying to figure out how to simulate a Ctrl - zoom out using Javascript. I've noticed that using the style zoom property or the transform property gives different results with white space in the corners, rather than the smooth zoom out effect ...

Utilizing PHP to fetch data from a separate webpage

This is a question that has sparked my curiosity. I am not facing any particular issue that requires an immediate solution nor do I possess the knowledge on how to achieve it. I have been contemplating whether it is feasible to utilize PHP for fetching co ...

reinitializing an array duplicate within an angular function following the use of .splice()

My goal is to create a dropdown menu in Angular that automatically removes the current page from the list when navigating to another view. I want the menu to reset and remove the now-current view every time a new view is loaded. This is the array of objec ...

"Exploring the world of AngularJS routing and using htaccess for rewriting URLs

How can I seamlessly integrate AngularJS ngRoute and htaccess rewrite? Currently, my ngRoute setup generates URLs like this: http://domain.com/#/something/somestring However, I would prefer cleaner URLs like: http://domain.com/something/somestring In ...

What is preventing me from using JavaScript to generate labels?

My current project involves creating dynamic input fields to filter products by color. I initially attempted static checkbox inputs for this purpose, which worked fine. Now, I want to achieve the same functionality but dynamically through JavaScript. Inste ...

What is preventing me from loading js and css files on my web pages?

I have developed a web application using SpringMVC with Thymeleaf and I am encountering an issue while trying to load javascript and CSS on my HTML5 pages. Here is a snippet from my login.html: <html xmlns="http://www.w3.org/1999/xhtml"> <head&g ...

Reveal Password Feature in Angular After User Click

I am working on an inventory page that includes a password field. My goal is to initially hide the password and display it as points (****) instead. I would like the password to be revealed either when clicked or through a pop-up. JS var retrieveCert ...

A growing flexbox container that expands to a specific height before allowing for scrolling

In the case of a fixed height container with two vertical divs, I am aiming for the first div to be as tall as its content, up to 80% of the parent div's height. After that point, the content within the first div should scroll. The second div will the ...

Center contents of Bootstrap row vertically

I have a simple property grid layout in Bootstrap 3 structured as follows: <div class="row"> <div class="col-md-3"> <div id="propGrid"> <div class="row pgTable"> <div class="col col-md-12"> <d ...

Envelop the phrases onto a fresh line

I'm having trouble getting the text inside a div to display correctly. When the text is too long, it just keeps stretching instead of breaking into new lines. How can I make sure that the content displays as a block within the div? Thank you. .box ...

Executing program through Socket.io alert

My NodeJS server sends notifications to clients when certain actions are performed, such as deleting a row from a grid. Socket.io broadcasts this information to all connected clients. In the example of deleting a row, one approach could be adding an `acti ...

What is the best way to create JavaScript code specifically for devices with a maximum width of 520px?

Is there a way to apply this JavaScript code specifically to devices with a maximum width of 520px? I could use some guidance on how to achieve this. // Apply code for max-width = 520px const myBtn = document.getElementById("darktheme"); const ...

What is the best way to manage asynchronous data within AngularJS directives?

Having encountered similar challenges to this specific scenario, I am currently facing issues with handling asynchronous data within my directives. The main issue arises when trying to pass asynchronously fetched data into these directives. Initially, I at ...

Transforming a Microsoft Word document containing images into HTML code

Looking for a solution to extract images from a Word document, save them to a folder, re-link them back into the document and then convert it to HTML for uploading to our intranet site. Any suggestions on how this can be achieved? ...

Customize your file input with Bootstrap 4 using bs-custom-file-input and create a Symfony 5 collection with dynamic upload fields

The issue of not having a label for the chosen filename in a bootstrap 4 upload field has been resolved using the plugin https://github.com/Johann-S/bs-custom-file-input You can utilize "bs-custom-file-input" to showcase the selected filename in the boots ...

Is it possible to resize AngularJS components to fit their parent elements

I am working with a set of LI elements within a document that require their width to be adjusted based on the number of items and the width of the container. How can I access the width of the parent container from this directive? var MyApp = angular.mod ...

Is there a way to remove most HTML tags while retaining certain ones?

Looking for a solution to filter out all HTML codes from a PHP string except for: <p> <em> <small> The strip_tags() function is helpful, but it removes all HTML tags. Is there a way to specify that only the listed tags should be removed ...

The coloring of my div elements is not displaying as intended

After reviewing the provided html and css, I am puzzled as to why the parent container is being colored green while the child items remain uncolored. It seems like all div elements are being affected by the green color. What I actually want is for the pa ...