I am creating a dynamic form based on Angular 4 Dynamic Forms .
Everything is working out great! However, I have run into an issue with the dropdown. I would like to have a dependent dropdown. When the user selects a value in the dropdown it will display checkboxes, based on an attribute - possibly name
.
service
new DropdownInput({
key: 'dropdown',
label: 'Dropdown Testing',
options: [
{key: 'example1', value: 'Example 1'},
{key: 'example2', value: 'Example 2'}
],
order: 1
}),
new CheckboxInput({
key: 'checkbox1',
label: 'checkbox1 - example1',
name: 'example1',
order: 2
}),
new CheckboxInput({
key: 'checkbox2',
label: 'checkbox2 - example1',
name: 'example1',
order: 3
}),
new CheckboxInput({
key: 'checkbox3',
label: 'checkbox3 - example2',
name: 'example2',
order: 4
}),
new CheckboxInput({
key: 'checkbox4',
label: 'checkbox4 - example2',
name: 'example2',
order: 5
})
html
<!-- CHECKBOX -->
<div class="col-xs-12" *ngSwitchCase="'checkbox'">
<input class="form-check-input"
type="checkbox"
[formControlName]="input.key"
[id]="input.key"
[name]="input.name">
<label class="control-label"
[attr.for]="input.key">{{input.label}}</label>
<a class="info-tooltip"><i class="fa fa-question-circle" aria-hidden="true"></i></a>
<span class="help-block"
*ngIf="!isValid">{{inputError}}</span>
</div>
<!-- DROPDOWN -->
<div class="col-xs-12" *ngSwitchCase="'dropdown'">
<label class="control-label"
[attr.for]="input.key">{{input.label}}</label>
<a class="info-tooltip"><i class="fa fa-question-circle" aria-hidden="true"></i></a>
<span class="help-block"
*ngIf="!isValid">{{inputError}}</span>
<select [id]="input.key"
[formControlName]="input.key">
<option *ngFor="let opt of input.options"
[value]="opt.key">{{opt.value}}</option>
</select>
</div>
So in this example if a user were to click on Example 1 - I would only like to display checkboxes 1 & 2. Is this possible? Should I be formatting the data differently?
Note 1: I have found the link on how to go from a dependent dropdown to another dropdown (Angular 2 Dynamic Forms: How to create dependent dropdown ), but do not know how to effectively use it with checkboxes.
Note 2: The reason I have my checkbox separate from textbox (not shown) is how they are displayed visually in the app. I know I could combine them for less/more efficient code.
I would add one option called
showWhen
to base model:base.model.ts
As you can see it takes
key
andvalue
. Depending on these values we can decide whether to show control or not.Now you can describe condition for showing your controls like:
Now go to the component that is called
DynamicFormQuestionComponent
in angular tutorial. In my example I called itDynamicFormComponent
. Here we need to add logic for showing/hidding control:dynamic-form.component.ts
and last thing you need to do is to add
*ngIf="!hidden"
in templateNg-run Example