是否可以在Primeng Datatable中添加行

时间:2017-01-10 06:11:12

标签: angularjs primeng angular-datatables

我需要为primeng datatble启用内联添加行。请帮忙。我需要添加新的文本框行或文本框和选择列表的组合作为新行,以便在单击添加新按钮而不是打开弹出窗口并接受值时填充数据表

App.component.html: -

<div>
    <p-dataTable [value]="cars" [editable]="true" [paginator]="true" [rows]="10" [responsive]="true">
       <header>CRUD for Cars</header>
        <p-column field="vin" header="Vin" [sortable]="true" [editable]="true"></p-column>
        <p-column field="year" header="Year" [sortable]="true" [editable]="true"></p-column>
        <p-column field="brand" header="Brand" [sortable]="true" [editable]="true"></p-column>
        <p-column field="color" header="Color" [sortable]="true" [editable]="true"></p-column>
        <footer><div class="ui-helper-clearfix" style="width:100%"><button type="button" pButton icon="fa-plus" style="float:left" (click)="showDialogToAdd()" label="Add"></button></div></footer>
    </p-dataTable>  


    <p-dialog header="Car Details" [(visible)]="displayDialog" [responsive]="true" showEffect="fade" [modal]="true">
        <div class="ui-grid ui-grid-responsive ui-fluid ui-grid-pad" *ngIf="car">
            <div class="ui-grid-row">
                <div class="ui-grid-col-4"><label for="vin">Vin</label></div>
                <div class="ui-grid-col-8"><input pInputText id="vin" [(ngModel)]="car.vin" /></div>
            </div>
            <div class="ui-grid-row">
                <div class="ui-grid-col-4"><label for="brand">Year</label></div>
                <div class="ui-grid-col-8"><input pInputText id="brand" [(ngModel)]="car.year" /></div>
            </div>
            <div class="ui-grid-row">
                <div class="ui-grid-col-4"><label for="brand">Brand</label></div>
                <div class="ui-grid-col-8"><input pInputText id="brand" [(ngModel)]="car.brand" /></div>
            </div>
            <div class="ui-grid-row">
                <div class="ui-grid-col-4"><label for="color">Color</label></div>
                <div class="ui-grid-col-8"><input pInputText id="color" [(ngModel)]="car.color" /></div>
            </div>
        </div>
        <footer>
            <div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
                <button type="button" pButton icon="fa-close" (click)="delete()" label="Delete"></button>
                <button type="button" pButton icon="fa-check" (click)="save()" label="Save"></button>
            </div>
        </footer>
    </p-dialog>
</div>

app.component.ts: -

import {Component} from '@angular/core';
import {Car} from './cars/car';
import {CarService} from './cars/carservice';

class PrimeCar implements Car {
    constructor(public vin?, public year?, public brand?, public color?) {}
}

@Component({
    templateUrl: 'app/app.component.html',
    selector: 'my-app'
})
export class AppComponent {

    displayDialog: boolean;

    car: Car = new PrimeCar();

    selectedCar: Car;

    newCar: boolean;

    cars: Car[];

    constructor(private carService: CarService) { }

    ngOnInit() {
        this.carService.getCarsMedium().then(cars => this.cars = cars);
    }

    showDialogToAdd() {
        this.newCar = true;
        this.car = new PrimeCar();
        this.displayDialog = true;
    }

    save() {
        if(this.newCar)
            this.cars.push(this.car);
        else
            this.cars[this.findSelectedCarIndex()] = this.car;

        this.car = null;
        this.displayDialog = false;
    }

    delete() {
        this.cars.splice(this.findSelectedCarIndex(), 1);
        this.car = null;
        this.displayDialog = false;
    }

    onRowSelect(event) {
        this.newCar = false;
        this.car = this.cloneCar(event.data);
        this.displayDialog = true;
    }

    cloneCar(c: Car): Car {
        let car = new PrimeCar();
        for(let prop in c) {
            car[prop] = c[prop];
        }
        return car;
    }

    findSelectedCarIndex(): number {
        return this.cars.indexOf(this.selectedCar);
    }
}

1 个答案:

答案 0 :(得分:0)

您可以通过编程方式将项目添加到您在幕后使用的数组中,在您的情况下为cars

addRow() {
    const newCar = {
      vin: '123',
      year: '1902',
      brand: 'Ford',
      color: 'brown'
    };
    this.cars.push(newCar);
    this.cars = this.cars.slice();
  }

我发现我必须在最后添加.slice()操作,以便在浏览器中刷新数据表。我认为那里的问题与变化检测有关,但我不记得100%。

脚注:我相信可能有更优雅和受控制的方式添加项目&#34; inline&#34;像你一直在寻找,但我仍然在努力想出一个解决方案。

相关问题