DevExtreme v26.1 is now available.

Explore our newest features/capabilities and share your thoughts with us.

Your search did not match any results.

Angular Form - Customize Fields at Runtime

The Form component allows you to add/remove items and specify item visibility at runtime. The Form only updates the affected items and does not re-render the entire form.

In this demo, the "Show Address" checkbox specifies the visibility of address-related fields. Review the component's code to see how to set the "HomeAddress" group's visible attribute.

Click the "Add phone" button to add a new phone editor. This action adds a new item to the items array of the "phones" group. Each phone editor contains a button that deletes it from the items array and the Form.

Backend API
<div class="long-title"> <h3>Personal details</h3> </div> <div id="form-container"> <dx-form id="form" [colCount]="2" [formData]="employee"> <dxi-form-item itemType="group"> <dxi-form-item itemType="group" caption="Personal Data"> <dxi-form-item dataField="FirstName"></dxi-form-item> <dxi-form-item dataField="LastName"></dxi-form-item> <dxi-form-item editorType="dxCheckBox" [editorOptions]="checkBoxOptions" ></dxi-form-item> </dxi-form-item> <dxi-form-item itemType="group"> <dxi-form-item itemType="group" caption="Home Address" name="HomeAddress" [visible]="isHomeAddressVisible" > <dxi-form-item dataField="Address"></dxi-form-item> <dxi-form-item dataField="City"></dxi-form-item> </dxi-form-item> </dxi-form-item> </dxi-form-item> <dxi-form-item itemType="group" caption="Phones" name="phones-container"> @for (phone of phoneOptions; track $index) { <dxi-form-item itemType="group" name="phones"> <dxi-form-item [dataField]="'Phones[' + $index + ']'" [editorOptions]="phoneOptions[$index]" > <dxo-form-label [text]="'Phone ' + ($index + 1)"> </dxo-form-label> </dxi-form-item> </dxi-form-item> } <dxi-form-item itemType="button" horizontalAlignment="left" cssClass="add-phone-button" [buttonOptions]="addPhoneButtonOptions" > </dxi-form-item> </dxi-form-item> </dx-form> </div>
import { bootstrapApplication } from '@angular/platform-browser'; import { ChangeDetectorRef, Component, enableProdMode, provideZoneChangeDetection, } from '@angular/core'; import { DxFormModule } from 'devextreme-angular'; import { DxCheckBoxTypes } from 'devextreme-angular/ui/check-box'; import { DxButtonTypes } from 'devextreme-angular/ui/button'; import { DxTextBoxTypes } from 'devextreme-angular/ui/text-box'; import { Employee, Service } from './app.service'; if (!/localhost/.test(document.location.host)) { enableProdMode(); } @Component({ selector: 'demo-app', providers: [Service], templateUrl: './app.component.html', styleUrls: ['./app.component.css'], imports: [ DxFormModule, ], }) export class AppComponent { employee: Employee; isHomeAddressVisible = true; phoneOptions: DxTextBoxTypes.Properties[] = []; checkBoxOptions: DxCheckBoxTypes.Properties = { text: 'Show Address', value: true, onValueChanged: (e: DxCheckBoxTypes.ValueChangedEvent) => { this.isHomeAddressVisible = e.component.option('value'); this.changeDetectorRef.detectChanges(); }, }; addPhoneButtonOptions: DxButtonTypes.Properties = { icon: 'add', text: 'Add phone', onClick: () => { this.employee.Phones.push(''); this.phoneOptions = this.getPhonesOptions(this.employee.Phones); this.changeDetectorRef.detectChanges(); }, }; constructor(service: Service, private changeDetectorRef: ChangeDetectorRef) { this.employee = service.getEmployee(); this.phoneOptions = this.getPhonesOptions(this.employee.Phones); } getPhonesOptions = (phones: string[]) => phones.map((_, index) => this.generateNewPhoneOptions(index)); generateNewPhoneOptions(index: number): DxTextBoxTypes.Properties { return { mask: '(X00) 000-0000', maskRules: { X: /[01-9]/ }, buttons: [{ name: 'trash', location: 'after', options: { stylingMode: 'text', icon: 'trash', onClick: () => { this.employee.Phones.splice(index, 1); this.phoneOptions = this.getPhonesOptions(this.employee.Phones); this.changeDetectorRef.detectChanges(); }, }, }], }; } } bootstrapApplication(AppComponent, { providers: [ provideZoneChangeDetection({ eventCoalescing: true, runCoalescing: true }), ], });
::ng-deep #form-container { margin: 10px 10px 30px; } ::ng-deep .long-title h3 { font-family: 'Segoe UI Light', 'Helvetica Neue Light', 'Segoe UI', 'Helvetica Neue', 'Trebuchet MS', Verdana; font-weight: 200; font-size: 28px; text-align: center; margin-bottom: 20px; } ::ng-deep .add-phone-button { margin-top: 10px; }
import { Injectable } from '@angular/core'; export class Employee { FirstName: string; LastName: string; Address: string; City: string; Phones: string[]; } const employee: Employee = { FirstName: 'John', LastName: 'Heart', Address: '351 S Hill St., Los Angeles, CA', City: 'Atlanta', Phones: ['8005552797', '8005953232'], }; @Injectable() export class Service { getEmployee(): Employee { return employee; } }
<!DOCTYPE html> <html lang="en"> <head> <title>DevExtreme Demo</title> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0" /> <link rel="stylesheet" type="text/css" href="https://cdn3.devexpress.com/jslib/26.1.5/css/dx.light.css" /> </head> <body class="dx-viewport"> <div class="demo-container"> <demo-app>Loading...</demo-app> </div> <script src="https://cdn.jsdelivr.net/npm/zone.js@0.16.2/bundles/zone.umd.js"></script> <script src="./bundle.js" type="module"></script> </body> </html>