1
1
import { HttpClient } from '@angular/common/http' ;
2
- import { ChangeDetectorRef , Component , OnInit , ViewChild } from '@angular/core' ;
3
- import { EntityType , FilteringExpressionsTree , IExpressionTree , IgxGridComponent , IgxQueryBuilderComponent , IgxStringFilteringOperand } from 'igniteui-angular' ;
2
+ import { AfterViewInit , ChangeDetectorRef , Component , OnInit , ViewChild } from '@angular/core' ;
3
+ import { EntityType , FilteringExpressionsTree , IExpressionTree , IgxColumnComponent , IgxGridComponent , IgxNumberFilteringOperand , IgxQueryBuilderComponent , IgxStringFilteringOperand } from 'igniteui-angular' ;
4
4
import { format } from 'sql-formatter' ;
5
5
6
6
const API_ENDPOINT = 'https://data-northwind.indigo.design' ;
@@ -9,16 +9,13 @@ const API_ENDPOINT = 'https://data-northwind.indigo.design';
9
9
selector : 'app-query-builder-sql-sample' ,
10
10
styleUrls : [ './query-builder-sql-sample.component.scss' ] ,
11
11
templateUrl : 'query-builder-sql-sample.component.html' ,
12
- imports : [ IgxQueryBuilderComponent , IgxGridComponent ]
12
+ imports : [ IgxQueryBuilderComponent , IgxGridComponent , IgxColumnComponent ]
13
13
} )
14
- export class QueryBuilderSqlSampleComponent implements OnInit {
15
- @ViewChild ( 'queryBuilder' , { static : true } )
16
- public queryBuilder : IgxQueryBuilderComponent ;
17
-
14
+ export class QueryBuilderSqlSampleComponent implements OnInit , AfterViewInit {
18
15
@ViewChild ( 'grid' , { static : true } )
19
16
public grid : IgxGridComponent ;
20
17
21
- public gridData = [ ] ;
18
+ public data : any [ ] = [ ] ;
22
19
public entities : EntityType [ ] = [ ] ;
23
20
public expressionTree : IExpressionTree ;
24
21
public sqlQuery : string = 'SQL Query will be displayed here' ;
@@ -28,13 +25,10 @@ export class QueryBuilderSqlSampleComponent implements OnInit {
28
25
public ngOnInit ( ) : void {
29
26
this . setEntities ( ) ;
30
27
this . setInitialExpressionTree ( ) ;
31
- this . executeQuery ( ) ;
32
28
}
33
29
34
- public executeQuery ( ) {
35
- const sqlQuery = this . transformExpressionTreeToSqlQuery ( this . expressionTree ) ;
36
- this . sqlQuery = format ( sqlQuery ) ;
37
- this . setGridData ( ) ;
30
+ public ngAfterViewInit ( ) : void {
31
+ this . onChange ( ) ;
38
32
}
39
33
40
34
private setEntities ( ) {
@@ -97,7 +91,7 @@ export class QueryBuilderSqlSampleComponent implements OnInit {
97
91
searchTree : categoriesTree
98
92
} ) ;
99
93
100
- const suppliersTree = new FilteringExpressionsTree ( 0 , undefined , 'Suppliers' , [ '* ' ] ) ;
94
+ const suppliersTree = new FilteringExpressionsTree ( 0 , undefined , 'Suppliers' , [ 'supplierId' , 'companyName' , 'contactName' , 'contactTitle '] ) ;
101
95
suppliersTree . filteringOperands . push ( {
102
96
fieldName : 'supplierId' ,
103
97
conditionName : IgxStringFilteringOperand . instance ( ) . condition ( 'inQuery' ) . name ,
@@ -190,12 +184,20 @@ export class QueryBuilderSqlSampleComponent implements OnInit {
190
184
}
191
185
}
192
186
193
- private setGridData ( ) {
187
+ public onChange ( ) {
188
+ const sqlQuery = this . transformExpressionTreeToSqlQuery ( this . expressionTree ) ;
189
+ this . sqlQuery = format ( sqlQuery ) ;
190
+
194
191
this . grid . isLoading = true ;
195
- this . http . post ( `${ API_ENDPOINT } /QueryBuilder/ExecuteQuery` , this . expressionTree ) . subscribe ( data => {
196
- this . gridData = Object . values ( data ) [ 0 ] as any [ ] ;
192
+ this . http . post ( `${ API_ENDPOINT } /QueryBuilder/ExecuteQuery` , this . expressionTree ) . subscribe ( data => {
193
+ this . data = Object . values ( data ) [ 0 ] ;
197
194
this . grid . isLoading = false ;
195
+ this . cdr . detectChanges ( ) ;
196
+ this . calculateColsInView ( ) ;
198
197
} ) ;
199
- this . cdr . detectChanges ( ) ;
198
+ }
199
+
200
+ private calculateColsInView ( ) {
201
+ this . grid . columns . forEach ( column => column . hidden = ! this . expressionTree . returnFields . includes ( column . field ) ) ;
200
202
}
201
203
}
0 commit comments