Skip to content

conditional options #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions src/lib/src/widget-library/select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@ import { buildTitleMap, isArray } from '../shared';
[id]="'control' + layoutNode?._id"
[name]="controlName">
<ng-template ngFor let-selectItem [ngForOf]="selectList">
<option *ngIf="!isArray(selectItem?.items)"
<option *ngIf="!isArray(selectItem?.items) && showOption(selectItem, layoutNode)"
[value]="selectItem?.value">
<span [innerHTML]="selectItem?.name"></span>
</option>
<optgroup *ngIf="isArray(selectItem?.items)"
[label]="selectItem?.group">
<option *ngFor="let subItem of selectItem.items"
[value]="subItem?.value">
<span [innerHTML]="subItem?.name"></span>
</option>
<ng-container *ngFor="let subItem of selectItem.items">
<option *ngIf="showOption(subItem, layoutNode)"
[value]="subItem?.value">
<span [innerHTML]="subItem?.name"></span>
</option>
</ng-container>
</optgroup>
</ng-template>
</select>
Expand All @@ -46,18 +48,20 @@ import { buildTitleMap, isArray } from '../shared';
[name]="controlName"
(change)="updateValue($event)">
<ng-template ngFor let-selectItem [ngForOf]="selectList">
<option *ngIf="!isArray(selectItem?.items)"
<option *ngIf="!isArray(selectItem?.items) && showOption(selectItem, layoutNode)"
[selected]="selectItem?.value === controlValue"
[value]="selectItem?.value">
<span [innerHTML]="selectItem?.name"></span>
</option>
<optgroup *ngIf="isArray(selectItem?.items)"
[label]="selectItem?.group">
<option *ngFor="let subItem of selectItem.items"
[attr.selected]="subItem?.value === controlValue"
[value]="subItem?.value">
<span [innerHTML]="subItem?.name"></span>
</option>
<ng-container *ngFor="let subItem of selectItem.items">
<option *ngIf="showOption(subItem, layoutNode) && showOption(selectItem, layoutNode)"
[attr.selected]="subItem?.value === controlValue"
[value]="subItem?.value">
<span [innerHTML]="subItem?.name"></span>
</option>
</ng-container>
</optgroup>
</ng-template>
</select>
Expand Down Expand Up @@ -92,4 +96,22 @@ export class SelectComponent implements OnInit {
updateValue(event) {
this.jsf.updateValue(this, event.target.value);
}

showOption(item: any, layoutNode: any): boolean {
if (item.conditionKey !== undefined && layoutNode.options.conditionMap !== undefined) {
Copy link

@johnedvard johnedvard Feb 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am just wondering if it is equally correct to write if(item.conditionKey && layoutNode.options.conditionMap) skipping the !== undefined. I mean, if 0 and null are unacceptable values as well.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not familiar with what is best practice. If this is the style that goes through the rest of the code in this repo, then by all means, its better to keep it this way (using !== undefined).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right

item.options = new Object();

for (const condition of layoutNode.options.conditionMap) {
if (condition.key === item.conditionKey) {
item.options.condition = condition.condition;
return this.jsf.evaluateCondition(item, this.dataIndex);
}
}
} else if (item !== undefined && item.condition !== undefined) {
item.options = new Object();
item.options.condition = item.condition;
return this.jsf.evaluateCondition(item, this.dataIndex);
}
return true;
}
}