11import { 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' ;
44import { format } from 'sql-formatter' ;
55
66const API_ENDPOINT = 'https://data-northwind.indigo.design' ;
@@ -9,16 +9,13 @@ const API_ENDPOINT = 'https://data-northwind.indigo.design';
99 selector : 'app-query-builder-sql-sample' ,
1010 styleUrls : [ './query-builder-sql-sample.component.scss' ] ,
1111 templateUrl : 'query-builder-sql-sample.component.html' ,
12- imports : [ IgxQueryBuilderComponent , IgxGridComponent ]
12+ imports : [ IgxQueryBuilderComponent , IgxGridComponent , IgxColumnComponent ]
1313} )
14- export class QueryBuilderSqlSampleComponent implements OnInit {
15- @ViewChild ( 'queryBuilder' , { static : true } )
16- public queryBuilder : IgxQueryBuilderComponent ;
17-
14+ export class QueryBuilderSqlSampleComponent implements OnInit , AfterViewInit {
1815 @ViewChild ( 'grid' , { static : true } )
1916 public grid : IgxGridComponent ;
2017
21- public gridData = [ ] ;
18+ public data : any [ ] = [ ] ;
2219 public entities : EntityType [ ] = [ ] ;
2320 public expressionTree : IExpressionTree ;
2421 public sqlQuery : string = 'SQL Query will be displayed here' ;
@@ -28,13 +25,10 @@ export class QueryBuilderSqlSampleComponent implements OnInit {
2825 public ngOnInit ( ) : void {
2926 this . setEntities ( ) ;
3027 this . setInitialExpressionTree ( ) ;
31- this . executeQuery ( ) ;
3228 }
3329
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 ( ) ;
3832 }
3933
4034 private setEntities ( ) {
@@ -97,7 +91,7 @@ export class QueryBuilderSqlSampleComponent implements OnInit {
9791 searchTree : categoriesTree
9892 } ) ;
9993
100- const suppliersTree = new FilteringExpressionsTree ( 0 , undefined , 'Suppliers' , [ '* ' ] ) ;
94+ const suppliersTree = new FilteringExpressionsTree ( 0 , undefined , 'Suppliers' , [ 'supplierId' , 'companyName' , 'contactName' , 'contactTitle '] ) ;
10195 suppliersTree . filteringOperands . push ( {
10296 fieldName : 'supplierId' ,
10397 conditionName : IgxStringFilteringOperand . instance ( ) . condition ( 'inQuery' ) . name ,
@@ -190,12 +184,20 @@ export class QueryBuilderSqlSampleComponent implements OnInit {
190184 }
191185 }
192186
193- private setGridData ( ) {
187+ public onChange ( ) {
188+ const sqlQuery = this . transformExpressionTreeToSqlQuery ( this . expressionTree ) ;
189+ this . sqlQuery = format ( sqlQuery ) ;
190+
194191 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 ] ;
197194 this . grid . isLoading = false ;
195+ this . cdr . detectChanges ( ) ;
196+ this . calculateColsInView ( ) ;
198197 } ) ;
199- this . cdr . detectChanges ( ) ;
198+ }
199+
200+ private calculateColsInView ( ) {
201+ this . grid . columns . forEach ( column => column . hidden = ! this . expressionTree . returnFields . includes ( column . field ) ) ;
200202 }
201203}
0 commit comments