I don't know if this is possible but i have built a dynamic form component which works quite well and i can control a number of items through a configuration file.
One item i have not been able to control is the width of the input field, I have tried a dozen ways but none seem to work. i.e setting width in the component when creating the form control etc
Does anyone know if what i am trying to do is actually possible and can point me in the right direction.
Template Code
<div fxLayout="row">
<div [formGroup]="formGroup" fxLayoutAlign="start" fxLayoutGap="10px">
<div *ngFor="let form_elem of formTemplate">
<div *ngIf="form_elem.visible === 'true'">
<mat-form-field>
<mat-label>{{ form_elem.label }}</mat-label>
<input
matInput
type="text"
formControlName="{{ form_elem.name }}"
class="{{ form_elem.width }} "
/>
</mat-form-field>
</div>
</div>
</div>
</div>
Typescript Code
import {
ChangeDetectionStrategy,
Component,
EventEmitter,
Input,
Output
} from "@angular/core";
import { FormGroup, FormControl } from "@angular/forms";
@Component({
selector: "fw-form-array-item",
templateUrl: "./form-array-item.component.html",
styleUrls: ["./form-array-item.component.scss"],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class FormArrayItemComponent {
@Output() remove = new EventEmitter<FormGroup>();
@Input() formGroup: FormGroup;
@Input() formTemplate: any;
constructor() {}
ngOnInit() {
let group = {};
this.formTemplate.forEach(input_template => {
group[input_template.label] = new FormControl('');
});
}
}
Form Template Config File
export class OrdersProductsFormTemplate {
config = [
{
type: "textBox",
label: "id",
name: "id",
width: "50%",
justify: "left",
visible: 'true',
disabled: true
},
{
type: "textBox",
label: "Name",
name: "name",
width: "100%",
justify: "left",
visible: 'true',
disabled: false
},
{
type: "textBox",
label: "Currency",
name: "currency",
width: "100%",
justify: "left",
visible: 'true',
disabled: false
},
{
type: "textBox",
label: "Retail",
name: "retailPrice",
width: "100%",
justify: "left",
visible: 'true',
disabled: false
},
{
type: "textBox",
label: "Supplier Price",
name: "supplierPrice",
width: "100%",
justify: "left",
visible: 'true',
disabled: false
}
];
}