We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Date Range Filter

Hi,

I need to be able to filter a column by a date range using the DataGrid and a custom data adaptor using the grid component in Vue.

How can I get a date range picker to be usable as a filter?

Thanks.

9 Replies

RR Rajapandi Ravi Syncfusion Team July 30, 2020 11:45 AM UTC

Hi Joshua, 

Greetings from syncfusion support 

We have analyzed your query and prepared sample based on your requirement filter the column with daterangepicker in custom adaptor. You can filter the Grid by using daterange picker component externally using filterSettings property. Please refer the below code example , sample and documentation for more information. 

 
<template> 
  <div id="app"> 
    <div class="wrapper"> 
      <ejs-daterangepicker :placeholder="waterMark"></ejs-daterangepicker> 
      <Button type="button" @click="change">Filter</Button> 
    </div> 
    <ejs-grid :allowFiltering="true" :filterSettings='filterOptions' :dataSource="data"> 
      <e-columns> 
        <e-column field="Sno" headerText="SNO" textAlign="Right" width="150"></e-column> 
        <e-column field="CustomerID" headerText="Customer ID" width="120"></e-column> 
        <e-column field="Freight" headerText="Freight" textAlign="Right" format="C2" width="90"></e-column> 
        <e-column 
          field="OrderDate" 
          headerText="Order Date" 
          textAlign="Right" 
          format="yMd" 
          type="date" 
          width="120" 
        ></e-column> 
        <e-column 
          field="ShippedDate" 
          headerText="Shipped Date" 
          textAlign="Right" 
          format="yMd" 
          type="date" 
          width="120" 
        ></e-column> 
      </e-columns> 
    </ejs-grid> 
  </div> 
</template> 
<script> 
import Vue from "vue"; 
import { GridPlugin, Filter } from "@syncfusion/ej2-vue-grids"; 
import { DataManager, ODataAdaptor } from "@syncfusion/ej2-data"; 
import { DateRangePickerPlugin } from "@syncfusion/ej2-vue-calendars"; 
 
Vue.use(GridPlugin); 
Vue.use(DateRangePickerPlugin); 
 
class SerialNoAdaptor extends ODataAdaptor { 
  processResponse() { 
    let i = 0; 
    //calling base class processResponse function 
    let original = super.processResponse.apply(this, arguments); 
    //Adding serial number 
    original.result.forEach(item => (item["Sno"] = ++i)); 
    return { result: original.result, count: original.count }; 
  } 
} 
 
export default { 
  data() { 
    let SERVICE_URI = 
    return { 
      data: new DataManager({ 
        url: SERVICE_URI, 
        adaptor: new SerialNoAdaptor() 
      }), 
      filterOptions: { 
           type: 'Menu' 
        } 
    }; 
  }, 
  methods: { 
    change(args) { 
      var datepicker = document.getElementsByClassName("e-daterangepicker")[0] 
        .ej2_instances[0]; 
      var grid = document.getElementsByClassName("e-grid")[0].ej2_instances[0]; 
      var dateValue1 = datepicker.value[0]; 
      var dataValue2 = datepicker.value[1]; //get date values from date range picker 
      grid.filterSettings.columns = [ 
        { 
          value: dateValue1, 
          operator: "greaterthanorequal", 
          field: "OrderDate", 
          predicate: "and" 
        }, 
        { 
          value: dataValue2, 
          operator: "lessthanorequal", 
          field: "OrderDate", 
          predicate: "and" 
        } 
      ]; 
    } 
  }, 
  provide: { 
    grid: [Filter] 
  } 
}; 
</script> 
<style> 
.wrapper { 
  max-width: 250px; 
  margin: 0 auto; 
} 
</style> 
 



Screenshot:  

 

Regards, 
Rajapandi R 



JD Joshua Dunn July 30, 2020 12:15 PM UTC

Hi,

Thanks for the reply.

I want to use my filter in the grid, so in place of the usual filter in the popup there, not as an external button.

Thanks.


SK Sujith Kumar Rajkumar Syncfusion Team July 31, 2020 10:58 AM UTC

Hi Joshua, 

Your requirement can be achieved by rendering the date range picker either in the filter bar using filter bar template or in the filter menu. So can you please confirm which way you require to render the control based on which we will check and provide the solution for it. Both of these requirements are documented in the below help documentation link which you can check for your reference, 



Based on the confirmation we will provide the further details. 

Let us know if you have any concerns. 

Regards, 
Sujith R 



JD Joshua Dunn July 31, 2020 11:02 AM UTC

Hi,

I want to render the custom date picker in the filter menu.

I've managed to do that, but the actual act of filtering is quite cumbersome.

As found in another post, I implemented the following:

Custom filter w/ DateRangePicker:

        get dateRangeFilter(): any {
            let self = this;
            return {
                ui: {
                    create: (args) => {
                        args.getOptrInstance.dropOptr.element.parentElement.parentElement.style.display =
                            "none";
                        if (self.dateRangePicker.value == null) {
                            self.dateRangePicker = new DateRangePicker({
                                placeholder: "Select a Range",
                                change: function (e) {
                                    if (e != undefined && e.value) {
                                        self.dateValue = e.value;
                                        (self as any).$refs.entityGrid.clearFiltering(["orderDate"]);
                                        (self as any).$refs.entityGrid.filterByColumn(
                                            "orderDate",
                                            "greaterthanorequal",
                                            self.dateRangePicker.value[0]
                                        );
                                    } else {
                                        (self as any).$refs.entityGrid.filterSettings.columns = [];
                                        (self as any).$refs.entityGrid.removeFilteredColsByField("orderDate");
                                    }
                                }
                            });
                        }
                        let flValInput = createElement("input", { className: "flm-input" });
                        args.target.appendChild(flValInput);
                        self.dateRangePicker.appendTo(flValInput);
                    },
                    write: (args) => {
                    },
                    read: (args) => {
                    }
                }
            }
        }

However, to actually get the filter to work, I have to hook into the "actionBegin" method and add another filter there:

        public actionBegin(args): void {
            // Check for filter column
            if (
                args.requestType === "filtering" &&
                args.currentFilteringColumn === "orderDate"
            ) {
                // End date value is added as additional filter value with ‘lessthan’ filter operator
                args.columns.push({
                    actualFilterValue: {},
                    actualOperator: {},
                    field: "orderDate",
                    ignoreAccent: false,
                    isForeignKey: false,
                    matchCase: false,
                    operator: "lessthan",
                    predicate: "and",
                    uid: (this as any).$refs.entityGrid.getColumnByField(args.currentFilteringColumn).uid,
                    value: this.dateValue[1]
                });
            }
        }

What I want to do, is only have to implement this in the custom filter change method.

Thanks.


SK Sujith Kumar Rajkumar Syncfusion Team August 3, 2020 12:04 PM UTC

Hi Joshua, 
 
We checked the shared information and based on that would like to let you know that for your requirement the method that you have mentioned is one way of achieving it. This is because, since the value needs to be filtered between a range the second date needs to be pushed into the filter column in the actionBegin event(As in your shared code snippet) so as to filter between these values. However, if you need to perform the filter as initial filter settings(As updated for external filter button) then you can achieve it by adding the following code snippet in the date range picker’s change event handler, 
 
get dateRangeFilter(): any { 
   let self = this; 
   return { 
       ui: { 
          create: (args) => { 
                       . 
                       . 
            change: function (e) { 
               if (e != undefined && e.value) { 
                   var grid = document.getElementsByClassName("e-grid")[0].ej2_instances[0]; 
                   var dateValue1 = e.value[0]; 
                   var dataValue2 = e.value[1]; //get date values from date range picker  
                   grid.filterSettings.columns = [ 
                     { 
                        value: dateValue1, 
                        operator: "greaterthanorequal", 
                        field: "OrderDate", 
                        predicate: "and" 
                     }, 
                     { 
                        value: dataValue2, 
                        operator: "lessthanorequal", 
                        field: "OrderDate", 
                        predicate: "and" 
                     }  
               }  
                       . 
                       . 
} 
 
If we misunderstood your query, then please get back to us. 
 
Regards, 
Sujith R 



JD Joshua Dunn August 3, 2020 01:26 PM UTC

Hi,

Thanks for the reply.

I don't want to use an external filter button and I want to be able to set these values dynamically, not just at initial load.

What I want to do provide all of this information in the change event of the filter. 

Ideally, filterByColumn() would allow me to add  more than one condition.

Thanks,

Josh


RR Rajapandi Ravi Syncfusion Team August 5, 2020 01:48 PM UTC

Hi Joshua, 

Thanks for the update 

Based on your query we suspect that you like filter the column by using DataRangePicker in Grid filter menu and you like to perform the filter operation in change event of dataRangePicker.  

In previous update we have achieved the filter operation in change event of dateRangePicker not with external button. Please refer the below code example and sample for more information. 

     
export default {  
  name: "App",  
  data() {  
    let dateRangeInstance = null;  
    let dateValue = "";  
    let customFilter = false;  
    let vm = this;  
    return {  
      data: orderData,  
      filterSettings: { type"Menu" },  
      filterdate: {  
        ui: {  
          create: function(args) {  
            args.getOptrInstance.dropOptr.element.parentElement.parentElement.style.display =  
              "none";  
            var isFiltered = event.target.classList.contains("e-filtered");  
            vm.dateRangeInstance = new DateRangePicker({  
              placeholder: "Select a Range",  
              value: isFiltered ? vm.dateValue : null,  
              change: function(e) {  
                if (e.value) {  
                  vm.dateValue = e.value;  
                  vm.$refs.pgridtable.filterByColumn(  
                    "OrderDate",  
                    "greaterthanorequal",  
                    vm.dateValue[0]  
                  );  
                } else {  
                  vm.$refs.pgridtable.filterSettings.columns = [];  
                  vm.$refs.pgridtable.removeFilteredColsByField(args.target.id);  
                }  
              }  
            });  
            let flValInput = createElement("input", { className: "flm-input" });  
            args.target.appendChild(flValInput);  
            vm.dateRangeInstance.appendTo(flValInput);  
          },  
        . . .  
          }  
        }  
      }  
    };  
  },  
methods: {  
    actionBeginfunction(args) {  
      // Check for filter column  
      if (  
        args.requestType === "filtering" &&  
        args.currentFilteringColumn === "OrderDate"  
      ) {  
        // End date value is added as additional filter value with ‘lessthan’ filter operator  
        args.columns.push({  
          actualFilterValue: {},  
          actualOperator: {},  
          field: "OrderDate",  
          ignoreAccent: false,  
          isForeignKey: false,  
          matchCase: false,  
          operator: "lessthan",  
          predicate: "and",  
          uid: this.$refs.pgridtable.getColumnByField(args.currentFilteringColumn).uid,  
          value: this.dateValue[1]  
        });  
      }  
    }  
  } 
 
 
 


Regards, 
Rajapandi R 



PE Peter Esser December 14, 2020 01:05 PM UTC

Hi there, we've seen your example, works great. Is this also possible with filter option "FilterBar".
We want to reduce the amount of clicks for filtering, so the FilterBar is better in achieving this. It would be great that instead of the plain "input", you get the same daterange picker in the FilterBar.

Hope you can provide us the example.


RR Rajapandi Ravi Syncfusion Team December 15, 2020 12:25 PM UTC

Hi Peter, 

We have analyzed your query and we could seen that you like to render filterbar with dateRangePicker component. You can achieve your requirement by using the Filterbar Template feature of Grid. We have already discussed about your requirement in our Documentation. Please refer the below documentation for more information. 


Regards,
Rajapandi R


Loader.
Up arrow icon