Is it possible to add a drop-down to an editable data table?
I tried the code below, but I did not expect it. Is there any way?
<p-dataTable [value]="cars" sortMode="multiple" [editable]="true" [rows]="10" [paginator]="true" [globalFilter]="gb">
<header>Car List</header>
<p-column field="vin" header="Vin" [sortable]="true" [filter]="true"></p-column>
<p-column field="year" header="Year" [sortable]="true" [editable]="true" [filter]="true" filterMatchMode="contains"></p-column>
<p-column field="brand" header="Bland" [sortable]="true" [editable]="true" [filter]="true" filterMatchMode="contains"></p-column>
<p-column field="type" header="Body Type" [editable]="true" [filter]="true">
<template let-car="rowData" pTemplate type="body">
<p-dropdown [options]="types" [(ngModel)]="car.type"></p-dropdown>
</template>
</p-column>
</p-dataTable>
export class CarsComponent implements OnInit {
cars: Car[];
types: any[];
constructor(private carService: CarService) { }
ngOnInit() {
this.carService.getCarsSmall().then(cars => this.cars = cars);
this.types = [
{value: 1, label: "Sedan"},
{value: 2, label: "Coupe"},
{value: 3, label: "Wagon"},
{value: 4, label: "Mini"},
{value: 5, label: "SUV"},
];
}
}
@mikosu3 did you find the solution for your problem? I am facing a similar issue. When a new car object is added, the value in all dropdowns is same. Let me know if you need any more details from my side.
@supriya2992 I responded with the following sources. editable to false
<p-column field="type" header="Body Type" [editable]="false" [filter]="true">
<template let-car="rowData" pTemplate type="body">
<select [(ngModel)]="car.type" [ngStyle]='{"width": "100%","border": "none", "background": "rgba(255,255,255,0)"}'>
<option *ngFor="let row of types" [value]="row.value">{{row.label}}</option>
</select>
</template>
</p-column>
Oh ok thanks. So basically you have used normal HTML select instead of primeng dropdown.
@mikosu3 I am still facing the problem if i use html select instead of p-dropdown.
@supriya2992 Have you set the editable option to false?
I solved it this way in the meantime. But I think that the way I do is not the best
Issue with editable datatable with dropdown. On editing the data from dropdown then the value of the shwoing in the table, which should be lable.
Example: Dropdown Example value label 1 Newyork 2 Auli
On the selection of city id 1 Newyork (label) should be displayed there, not its value. Currently its displaying 1 instead of Newyork