@@ -7,13 +7,11 @@ import { takeUntil, first } from 'rxjs/operators';
77import { IForOfState } from '../../directives/for-of/for_of.directive' ;
88import { IgxColumnComponent } from '../columns/column.component' ;
99import { IFilteringOperation } from '../../data-operations/filtering-condition' ;
10- import { GridBaseAPIService } from '../api.service' ;
1110import { IColumnResizeEventArgs } from '../common/events' ;
12- import { GridType } from '../common/grid.interface' ;
1311import { OverlaySettings , PositionSettings , VerticalAlignment } from '../../services/overlay/utilities' ;
1412import { IgxOverlayService } from '../../services/overlay/overlay' ;
1513import { useAnimation } from '@angular/animations' ;
16- import { fadeIn , fadeOut } from '../../animations/main' ;
14+ import { fadeIn } from '../../animations/main' ;
1715import { ExcelStylePositionStrategy } from './excel-style/excel-style-position-strategy' ;
1816import { AbsoluteScrollStrategy } from '../../services/overlay/scroll/absolute-scroll-strategy' ;
1917import { IgxGridExcelStyleFilteringComponent } from './excel-style/grid.excel-style-filtering.component' ;
@@ -56,7 +54,7 @@ export class IgxFilteringService implements OnDestroy {
5654 public activeFilterCell = 0 ;
5755 grid : IgxGridBaseDirective ;
5856
59- constructor ( private gridAPI : GridBaseAPIService < IgxGridBaseDirective & GridType > , private _moduleRef : NgModuleRef < any > ,
57+ constructor ( private _moduleRef : NgModuleRef < any > ,
6058 private iconService : IgxIconService , private _overlayService : IgxOverlayService ) { }
6159
6260 ngOnDestroy ( ) : void {
@@ -200,41 +198,61 @@ export class IgxFilteringService implements OnDestroy {
200198 */
201199 public filter ( field : string , value : any , conditionOrExpressionTree ?: IFilteringOperation | IFilteringExpressionsTree ,
202200 ignoreCase ?: boolean ) {
203- const col = this . gridAPI . get_column_by_name ( field ) ;
201+ const col = this . grid . getColumnByName ( field ) ;
204202 const filteringIgnoreCase = ignoreCase || ( col ? col . filteringIgnoreCase : false ) ;
205203
206204 if ( conditionOrExpressionTree ) {
207- this . gridAPI . filter ( field , value , conditionOrExpressionTree , filteringIgnoreCase ) ;
205+ this . filter_internal ( field , value , conditionOrExpressionTree , filteringIgnoreCase ) ;
208206 } else {
209207 const expressionsTreeForColumn = this . grid . filteringExpressionsTree . find ( field ) ;
210208 if ( ! expressionsTreeForColumn ) {
211209 throw new Error ( 'Invalid condition or Expression Tree!' ) ;
212210 } else if ( expressionsTreeForColumn instanceof FilteringExpressionsTree ) {
213- this . gridAPI . filter ( field , value , expressionsTreeForColumn , filteringIgnoreCase ) ;
211+ this . filter_internal ( field , value , expressionsTreeForColumn , filteringIgnoreCase ) ;
214212 } else {
215213 const expressionForColumn = expressionsTreeForColumn as IFilteringExpression ;
216- this . gridAPI . filter ( field , value , expressionForColumn . condition , filteringIgnoreCase ) ;
214+ this . filter_internal ( field , value , expressionForColumn . condition , filteringIgnoreCase ) ;
217215 }
218216 }
219217 const eventArgs = this . grid . filteringExpressionsTree . find ( field ) as FilteringExpressionsTree ;
220218 // Wait for the change detection to update filtered data through the pipes and then emit the event.
221219 requestAnimationFrame ( ( ) => this . grid . onFilteringDone . emit ( eventArgs ) ) ;
222220 }
223221
222+ public filter_global ( term , condition , ignoreCase ) {
223+ if ( ! condition ) {
224+ return ;
225+ }
226+
227+ const grid = this . grid ;
228+ const filteringTree = grid . filteringExpressionsTree ;
229+ grid . endEdit ( false ) ;
230+ if ( grid . paging ) {
231+ grid . page = 0 ;
232+ }
233+
234+ filteringTree . filteringOperands = [ ] ;
235+ for ( const column of grid . columns ) {
236+ this . prepare_filtering_expression ( filteringTree , column . field , term ,
237+ condition , ignoreCase || column . filteringIgnoreCase ) ;
238+ }
239+
240+ grid . filteringExpressionsTree = filteringTree ;
241+ }
242+
224243 /**
225244 * Clears the filter of a given column if name is provided. Otherwise clears the filters of all columns.
226245 */
227246 public clearFilter ( field : string ) : void {
228247 if ( field ) {
229- const column = this . gridAPI . get_column_by_name ( field ) ;
248+ const column = this . grid . getColumnByName ( field ) ;
230249 if ( ! column ) {
231250 return ;
232251 }
233252 }
234253
235254 this . isFiltering = true ;
236-
237- this . gridAPI . clear_filter ( field ) ;
255+ this . clear_filter ( field ) ;
238256
239257 // Wait for the change detection to update filtered data through the pipes and then emit the event.
240258 requestAnimationFrame ( ( ) => this . grid . onFilteringDone . emit ( null ) ) ;
@@ -252,11 +270,26 @@ export class IgxFilteringService implements OnDestroy {
252270 this . isFiltering = false ;
253271 }
254272
273+ public clear_filter ( fieldName : string ) {
274+ const grid = this . grid ;
275+ grid . endEdit ( false ) ;
276+ const filteringState = grid . filteringExpressionsTree ;
277+ const index = filteringState . findIndex ( fieldName ) ;
278+
279+ if ( index > - 1 ) {
280+ filteringState . filteringOperands . splice ( index , 1 ) ;
281+ } else if ( ! fieldName ) {
282+ filteringState . filteringOperands = [ ] ;
283+ }
284+
285+ grid . filteringExpressionsTree = filteringState ;
286+ }
287+
255288 /**
256289 * Filters all the `IgxColumnComponent` in the `IgxGridComponent` with the same condition.
257290 */
258291 public filterGlobal ( value : any , condition , ignoreCase ?) {
259- this . gridAPI . filter_global ( value , condition , ignoreCase ) ;
292+ this . filter_global ( value , condition , ignoreCase ) ;
260293
261294 // Wait for the change detection to update filtered data through the pipes and then emit the event.
262295 requestAnimationFrame ( ( ) => this . grid . onFilteringDone . emit ( this . grid . filteringExpressionsTree ) ) ;
@@ -435,6 +468,53 @@ export class IgxFilteringService implements OnDestroy {
435468 return this . grid . filteredData ;
436469 }
437470
471+ private filter_internal ( fieldName : string , term , conditionOrExpressionsTree : IFilteringOperation | IFilteringExpressionsTree ,
472+ ignoreCase : boolean ) {
473+ const grid = this . grid ;
474+ const filteringTree = grid . filteringExpressionsTree ;
475+ this . grid . endEdit ( false ) ;
476+
477+ if ( grid . paging ) {
478+ grid . page = 0 ;
479+ }
480+
481+ const fieldFilterIndex = filteringTree . findIndex ( fieldName ) ;
482+ if ( fieldFilterIndex > - 1 ) {
483+ filteringTree . filteringOperands . splice ( fieldFilterIndex , 1 ) ;
484+ }
485+
486+ this . prepare_filtering_expression ( filteringTree , fieldName , term , conditionOrExpressionsTree , ignoreCase , fieldFilterIndex ) ;
487+ grid . filteringExpressionsTree = filteringTree ;
488+ }
489+
490+ private prepare_filtering_expression ( filteringState : IFilteringExpressionsTree , fieldName : string , searchVal ,
491+ conditionOrExpressionsTree : IFilteringOperation | IFilteringExpressionsTree , ignoreCase : boolean , insertAtIndex = - 1 ) {
492+
493+ let newExpressionsTree ;
494+ const oldExpressionsTreeIndex = filteringState . findIndex ( fieldName ) ;
495+ const expressionsTree = conditionOrExpressionsTree instanceof FilteringExpressionsTree ?
496+ conditionOrExpressionsTree as IFilteringExpressionsTree : null ;
497+ const condition = conditionOrExpressionsTree instanceof FilteringExpressionsTree ?
498+ null : conditionOrExpressionsTree as IFilteringOperation ;
499+ const newExpression : IFilteringExpression = { fieldName, searchVal, condition, ignoreCase } ;
500+
501+ if ( oldExpressionsTreeIndex === - 1 ) {
502+ // no expressions tree found for this field
503+ if ( expressionsTree ) {
504+ if ( insertAtIndex > - 1 ) {
505+ filteringState . filteringOperands . splice ( insertAtIndex , 0 , expressionsTree ) ;
506+ } else {
507+ filteringState . filteringOperands . push ( expressionsTree ) ;
508+ }
509+ } else if ( condition ) {
510+ // create expressions tree for this field and add the new expression to it
511+ newExpressionsTree = new FilteringExpressionsTree ( filteringState . operator , fieldName ) ;
512+ newExpressionsTree . filteringOperands . push ( newExpression ) ;
513+ filteringState . filteringOperands . push ( newExpressionsTree ) ;
514+ }
515+ }
516+ }
517+
438518 private isFilteringTreeComplex ( expressions : IFilteringExpressionsTree | IFilteringExpression ) : boolean {
439519 if ( ! expressions ) {
440520 return false ;
0 commit comments