Filtering by multiple different operators

Hello Syncfusion team, 

I am having trouble to filter for dates in an angular grid. Therefore, I want to filter for example all dates that are from August 2020. So I need to filter a time span. I tried to use a combination of the greaterThan and the lessThan operator, but it will only use the last filter. Is there a way to combine these two operators, especially for date span filtering? Or is there another way to avoid that problem? 

Thanks in advance

Jonas Czeslik

6 Replies 1 reply marked as answer

PG Praveenkumar Gajendiran Syncfusion Team September 30, 2020 11:44 AM UTC

Hi Jonas,

Greetings from Syncfusion support. 
In EJ2 Grid, we have Excel Filter with custom filter option by default. In that you can filter the date range with different operators. Please check the sample and screenshots for more information on this.

Sample:  https://stackblitz.com/edit/angular-jwwjbz?file=app.component.ts

Screenshot:
 


 
 
If you want to achieve your requirement in Menu Filter feature of Grid, we suggest you use DataRangePicker component in Grid filter menu through column.filter.ui property of grid. This is demonstrated below. Please refer the code example, screenshot and sample for more information on this.

Code Example: 
[app.component.ts]

ngOnInit(): void { 
      this.data = orderDataSource; 
      this.pageSettings = { pageCount: 5 }; 
      this.filterSettings = { type: 'Menu' }; 
      this.formatoptions = { type: 'date', format: 'M/d/y' }; 
      this.filter = { 
        ui: { 
          create: (args) => { 
            const flValInput: HTMLElement = createElement('input', { className: 'flm-input' }); 
            var vm = this 
            args.getOptrInstance.dropOptr.element.parentElement.parentElement.style.display = 
              "none";   
            var isFiltered = args.target.classList.contains("e-filtered");
           // DataRangePicker component 
            this.dateRangeInstance = new DateRangePicker({ 
              placeholder: "Select a Range", 
              value: isFiltered ? this.dateValue : null, 
              change: function (e) { 
                if (e.value) { 
                  vm.dateValue = e.value;              // In dateValue variable we get start date and end date of DataRangePicker component 
                  vm.grid.filterByColumn(             // here we filter the start date with ‘greaterthanorequal filter operator 
                    "OrderDate", 
                    "greaterthanorequal", 
                    vm.dateValue[0] 
                  ); 
                } else { 
                  this.grid.filterSettings.columns = []; 
                  this.grid.removeFilteredColsByField(args.target.id); 
                } 
              } 
            }); 

            args.target.appendChild(flValInput); 
            this.dateRangeInstance.appendTo(flValInput); 
          }, 
          write: (args) => { 
            console.log("filterval:" + args.filteredValue); 
          }, 
          read: (args) => { 
           console.log(args); 
          } 
        } 
      }; 
    } 

    actionBegin(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.grid.getColumnByField( 
            args.currentFilteringColumn 
          ).uid, 
          value: this.dateValue[1] 
        }); 
      } 
    };    


Screenshot: 
 
Please get back to us if you need further assistance.

Regards,
Praveenkumar G 



JC Jonas Czeslik October 2, 2020 12:27 PM UTC

First of all, thanks for the detailed alternatives. And the "Between" option in the excel type filter is what I need, but in my case, I will not work with the filter UI. I connected an angular grid and an angular tree. When I then click for example a 2020 node in the tree, I want to filter a certain date column and only show the records from 2020 (so between 1/1/2020 and 1/1/2021). This should work with months, days , ... too.  So, is there a way to filter like that "excel between" by code, too?

Thanks again!

Jonas Czeslik


SK Sujith Kumar Rajkumar Syncfusion Team October 5, 2020 01:06 PM UTC

Hi Jonas, 
 
You can achieve your requirement using one of the below methods, 
 
Method – 1: 
 
You can directly set the filter predicate object for the filter dates(between dates) with column field and filter operators(greaterthan and lessthan) to the Grid’s filterSettings property to achieve this requirement. This is demonstrated in the below code snippet, 
 
this.grid.filterSettings = { 
        columns: [{ field: 'OrderDate', matchCase: false, operator: 'greaterthan', predicate: 'and', value: this.startDate }, 
        { field: 'OrderDate', matchCase: false, operator: 'lessthan', predicate: 'and', value: this.endDate }] 
}; 
 
More details on this can be checked in the below help documentation link, 
 
 
Method – 2: 
 
You can also use the approach implemented in the previously provided solution(using date range picker) to achieve this requirement. 
 
For this, initially filter with the start date using Grid’s filterByColumn method and then in the Grid’s actionBegin event the additional filter operator for the same column with end date value is pushed to the filter column model. This is demonstrated in the below code snippet,  
  
var customFilter = false; 
 
// You can perform the below function operation in your use case(while selecting a node in the angular tree) 
function filterGridValues(e) { 
            var grid = document.getElementById("Grid").ej2_instances[0]; 
            // Flag variable is used to identify this case in the Grid’s action begin event 
           this.customFilter = true; 
            // Filter the required column with start date value 
            // GridInstance.filterByColumn(Column field name, filter operator, filter value) 
           this.grid.filterByColumn('OrderDate', 'greaterthan', this.startDate);   
} 
 
// Grid’s actionBegin event handler 
function onActionBegin(args) { 
        // Check for filter column and flag enabled 
        if (args.requestType === "filtering" && args.currentFilteringColumn === "OrderDate" && this.customFilter) { 
           this.customFilter = false; 
            // End date value is added as additional filter value with ‘lessthan’ filter operator  and endDate value 
            args.columns.push({ actualFilterValue: {}, actualOperator: {}, field: "OrderDate", ignoreAccent: false, isForeignKey: false, matchCase: false, operator: "lessthan", predicate: "and", uid: this.grid.getColumnByField(args.currentFilteringColumn).uid, value: this.endDate }); 
        } 
 
  
 
Let us know if you have any concerns. 
 
Regards, 
Sujith R 



AN Antonio July 22, 2021 05:21 PM UTC

Hello,

I'm trying to implement this sample: https://stackblitz.com/edit/angular-daterange-filter-hahght?file=app.component.ts 


But it is not working with the latest packages versions, I'm getting the following error:


core.js:5967 ERROR TypeError: Cannot read property 'isDestroyed' of undefined

    at DateFilterUI.destroy (ej2-grids.es2015.js:24979)

    at Observer.notify (ej2-base.es2015.js:2156)

    at GridComponent.notify (ej2-base.es2015.js:7032)

    at FilterMenuRenderer.closeDialog (ej2-grids.es2015.js:25035)

    at Filter.clickHandler (ej2-grids.es2015.js:26573)

    at ZoneDelegate.invokeTask (zone-evergreen.js:399)

    at Object.onInvokeTask (core.js:28269)

    at ZoneDelegate.invokeTask (zone-evergreen.js:398)

    at Zone.runTask (zone-evergreen.js:167)

    at ZoneTask.invokeTask [as invoke] (zone-evergreen.js:480)


If I downgrade Calendar and Grid to 18.2.56 and 18.2.59 it works... Is there a way to make it work with latest versions.


Thank in advance.



PG Praveenkumar Gajendiran Syncfusion Team July 23, 2021 01:54 PM UTC

Hi Jonas,

Thanks for your update.

We validated your reported scenario and we can reproduce the reported behavior at our end. We have confirmed this is an issue from our side and logged a bug for the same as “Rendered custom component via grid’s filter template is not destroyed properly”. At Syncfusion, we are committed to fixing all validated defects (subject to technical feasibility and Product Development Life Cycle ) and will include the defect fix in our upcoming patch release which will be rolled out on August 11, 2021.  
    
You can now track the current status of your request, review the proposed resolution timeline, and contact us for any further inquiries through this link.    
    
Regards,  
Praveenkumar G 



PG Praveenkumar Gajendiran Syncfusion Team August 11, 2021 07:48 AM UTC

Hi Jonas,

We are glad to announce that our Essential Javascript2 patch release (v19.2.55) has been rolled out successfully and in that release we have added the fix for “Rendered custom component via grid’s filter template is not destroyed properly” issue . So please update your package to this version to include the fix.

Find the below sample for your reference. 
We thank you for your support and appreciate your patience in waiting for this release. Please get in touch with us if you would require further assistance. 
 
Regards,            
Praveenkumar G 



Marked as answer
Loader.
Up arrow icon