Skip to content

Commit 81916da

Browse files
feat(query-builder): add public api for commit and discard current state (#14910)
* feat(query-builder): add public api for commit and discard current state * chore(*): update changelog --------- Co-authored-by: teodosia <[email protected]>
1 parent c9e92a1 commit 81916da

File tree

8 files changed

+356
-15
lines changed

8 files changed

+356
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ All notable changes for each version of this project will be documented in this
1717
- `IgxQueryBuilder`
1818
- Introduced ability to create nested queries by specifying IN/NOT IN operators.
1919
- Added the `entities` property that accepts an array of `EntityType` objects describing an entity with its name and an array of fields. The `fields` input property has been deprecated and will be removed in a future version. Automatic migrations are available and will be applied on `ng update`.
20+
- Added the `canCommit`, `commit` and `discard` public methods that allows the user to save/discard the current state of the expression tree.
2021
- Added option to template the search value input:
2122
```
2223
<ng-template igxQueryBuilderSearchValue

projects/igniteui-angular/src/lib/query-builder/query-builder-tree.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ <h6 class="igx-filter-empty__title">
100100
)
101101
}}
102102
</span>
103-
<span *ngIf="!expressionItem.expression.condition.isUnary">
103+
<span *ngIf="!expressionItem.expression.condition?.isUnary">
104104
<ng-container *ngIf="expressionItem.expression.searchTree">
105105
<strong>{{expressionItem.expression.searchTree.entity}}</strong>&nbsp;/ {{formatReturnFields(expressionItem.expression.searchTree)}}
106106
</ng-container>
@@ -145,7 +145,7 @@ <h6 class="igx-filter-empty__title">
145145
<div #tooltipRef="tooltip" igxTooltip>
146146
@if(expressionItem.expression.searchTree){
147147
{{expressionItem.expression.searchTree.returnFields.join(', ')}}
148-
} @else if (expressionItem.expression.condition.isUnary) {
148+
} @else if (expressionItem.expression.condition?.isUnary) {
149149
{{getConditionFriendlyName(expressionItem.expression.condition?.name)}}
150150
} @else {
151151
@if(getFormatter(expressionItem.expression.fieldName)) {

projects/igniteui-angular/src/lib/query-builder/query-builder-tree.component.ts

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -747,20 +747,22 @@ export class IgxQueryBuilderTreeComponent implements AfterViewInit, OnDestroy {
747747

748748
const innerQuery = this.innerQueries.filter(q => q.isInEditMode())[0]
749749
if (innerQuery && this.selectedField?.filters?.condition(this.selectedCondition)?.isNestedQuery) {
750-
if (!this._editedExpression.expression.searchTree) {
750+
if (!this._editedExpression.expression.searchTree && innerQuery.expressionTree) {
751751
this._editedExpression.expression.searchTree = new FilteringExpressionsTree(innerQuery.expressionTree.operator);
752752
}
753753

754-
this._editedExpression.expression.searchTree.entity = innerQuery.selectedEntity.name;
755-
this._editedExpression.expression.searchTree.returnFields = innerQuery.selectedReturnFields;
756-
this._editedExpression.expression.searchTree.filteringOperands = innerQuery.expressionTree.filteringOperands;
757-
this._editedExpression.expression.searchTree.operator = innerQuery.expressionTree.operator;
758-
this._editedExpression.expression.searchTree.fieldName = innerQuery.expressionTree.fieldName;
754+
if (this._editedExpression.expression.searchTree) {
755+
this._editedExpression.expression.searchTree.entity = innerQuery.selectedEntity.name;
756+
this._editedExpression.expression.searchTree.returnFields = innerQuery.selectedReturnFields;
757+
this._editedExpression.expression.searchTree.filteringOperands = innerQuery.expressionTree?.filteringOperands;
758+
this._editedExpression.expression.searchTree.operator = innerQuery.expressionTree?.operator;
759+
this._editedExpression.expression.searchTree.fieldName = innerQuery.expressionTree?.fieldName;
760+
}
759761
} else {
760762
this._editedExpression.expression.searchTree = null;
761763
}
762764

763-
if (this.selectedField.filters.condition(this.selectedCondition).isUnary) {
765+
if (this.selectedField.filters.condition(this.selectedCondition)?.isUnary) {
764766
this._editedExpression.expression.searchVal = null;
765767
}
766768

@@ -816,16 +818,52 @@ export class IgxQueryBuilderTreeComponent implements AfterViewInit, OnDestroy {
816818
return this.selectedField && this.selectedCondition &&
817819
(
818820
(
819-
(!!this.searchValue.value || (!!this.searchValueTemplate && !!this._editedExpression.expression.searchVal)) &&
821+
((!Array.isArray(this.searchValue.value) && !!this.searchValue.value) || (Array.isArray(this.searchValue.value) && this.searchValue.value.length !== 0)) &&
820822
!(this.selectedField?.filters?.condition(this.selectedCondition)?.isNestedQuery)
821823
) ||
822824
(
823-
innerQuery && !!innerQuery.expressionTree && innerQuery._editedExpression == undefined && innerQuery.selectedReturnFields.length > 0
825+
this.selectedField?.filters?.condition(this.selectedCondition)?.isNestedQuery && innerQuery && !!innerQuery.expressionTree && innerQuery._editedExpression == undefined && innerQuery.selectedReturnFields.length > 0
824826
) ||
825-
this.selectedField.filters.condition(this.selectedCondition).isUnary
827+
this.selectedField.filters.condition(this.selectedCondition)?.isUnary
826828
);
827829
}
828830

831+
/**
832+
* @hidden @internal
833+
*/
834+
public canCommitCurrentState(): boolean {
835+
const innerQuery = this.innerQueries.filter(q => q.isInEditMode())[0];
836+
if (innerQuery) {
837+
return this.selectedReturnFields?.length > 0 && innerQuery.canCommitCurrentState();
838+
} else {
839+
return this.selectedReturnFields?.length > 0 &&
840+
(
841+
(!this._editedExpression && !!this.rootGroup) || // no edited expr, root group
842+
(this._editedExpression && !this.selectedField && (this.expressionTree && this.expressionTree.filteringOperands[0] !== this._editedExpression.expression)) || // empty edited expr with at least one other expr
843+
(this._editedExpression && this.operandCanBeCommitted() === true) // valid edited expr
844+
);
845+
}
846+
}
847+
848+
/**
849+
* @hidden @internal
850+
*/
851+
public commitCurrentState(): void {
852+
const innerQuery = this.innerQueries.filter(q => q.isInEditMode())[0];
853+
if (innerQuery) {
854+
innerQuery.commitCurrentState();
855+
}
856+
857+
if (this._editedExpression) {
858+
if (this.selectedField) {
859+
this.commitOperandEdit();
860+
} else {
861+
this.deleteItem(this._editedExpression);
862+
this._editedExpression = null;
863+
}
864+
}
865+
}
866+
829867
/**
830868
* @hidden @internal
831869
*
@@ -931,7 +969,7 @@ export class IgxQueryBuilderTreeComponent implements AfterViewInit, OnDestroy {
931969

932970
if (!this.selectedField) {
933971
this.fieldSelect.input.nativeElement.focus();
934-
} else if (this.selectedField.filters.condition(this.selectedCondition).isUnary) {
972+
} else if (this.selectedField.filters.condition(this.selectedCondition)?.isUnary) {
935973
this.conditionSelect.input.nativeElement.focus();
936974
} else {
937975
const input = this.searchValueInput?.nativeElement || this.picker?.getEditElement();
@@ -1335,6 +1373,9 @@ export class IgxQueryBuilderTreeComponent implements AfterViewInit, OnDestroy {
13351373
let groupItem: ExpressionGroupItem;
13361374
if (expressionTree) {
13371375
groupItem = new ExpressionGroupItem(expressionTree.operator, parent);
1376+
if (!expressionTree.filteringOperands) {
1377+
return groupItem;
1378+
}
13381379

13391380
for (const expr of expressionTree.filteringOperands) {
13401381
if (expr instanceof FilteringExpressionsTree) {

0 commit comments

Comments
 (0)