0

Hi all i want to filtering items (from and to date) for enquiry_received_date using the daterange functionality in meanjs app. then i tried many ways but unable to get the solution if any one knows the solution please help me..... my plunk

Controller:

.filter('dateRange', function() {
    return function(records, from, to) {
        return records.filter(function(record) {
            return record.Date >= from && record.Date <= to;
        });
    }
})

Html:

<td> {{data.buyer_name}}</td>
   <td> {{data.ex_india_date}}</td>
      <td> {{data.quantity}}</td>
        <td> {{data.enquiry_received_date}}</td>

My plunk

R. Mani Selvam
  • 320
  • 8
  • 36
  • Does this solve your issue? http://stackoverflow.com/questions/25515431/ng-repeat-filtering-data-by-date-range – Brian May 26 '16 at 10:48

1 Answers1

0

First of all, your records seem to have no field like Date, so your filtering cannot work. I see that you're displaying enquiry_received_date, so I guess this is the field you want to use for filtering. This field contains a date in format YYYY-MM-DD, so you need to parse it to date. I suggest using MomentJS for this - this library makes working with date objects really easy.

Here is the code which can be used to achieve this:

Controller

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

View

In view I just added a reference to moment.js.

Working Plunker

http://plnkr.co/edit/gEPtj9yk1AxgEZiNeqY0?p=preview


Edit

If you'd like to make your filter more reusable, you could for example add another parameter with name of the field which should be used for filtering:

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));
            });
        }

View

<tr ng-repeat="data in  record | dateRange : 'enquiry_received_date' : from : to">
    <td> {{data.buyer_name}}</td>
    <td> {{data.ex_india_date}}</td>
    <td> {{data.quantity}}</td>
    <td> {{data.enquiry_received_date}}</td>
</tr>

Plunker

http://plnkr.co/edit/pczikllV7vBTVoFfcpvK?p=preview

PJDev
  • 951
  • 5
  • 20