简体   繁体   English

如何在顶部的自定义表格中添加一行?

[英]How to add a row in my custom table on top?

I have written a custom table in which you can add rows with a button.我编写了一个自定义表格,您可以在其中使用按钮添加行。 How can I set it so that the newly inserted row is displayed at the top of my table?如何设置它以使新插入的行显示在表格的顶部?

My Code:我的代码:

// For the form
  kagListForm: FormGroup;
  rows: FormArray;
  submitted = false;

 // Variables for Data Table
  public columns = [];
  private all = false;
  public index: number;
  public originalData: any = [];

 public displayedColumns: EditColumns[] = [
    { attribute: 'kagNumber', name: 'Kag-Nr.', object: null },
    { attribute: 'kagText', name: 'Bezeichnung', object: null }
  ];
ngOnInit() {

    // To initialize data table
    this.columns = this.displayedColumns.map(c => c.attribute);

    // KagList form
    this.kagListForm = this.formBuilder.group({
      rows: this.formBuilder.array([])
    });
  }
// To add new row in the kagList
  addRow() {
    const formArray = this.kagListForm.get('rows') as FormArray;
    const start = (this.rows && this.rows !== null) ? this.rows.controls.length : 0;
    for (let i = start; i < (start + 1); i++) {
      const rowData = {
        id: new FormControl(`new-${i}`),
        kagNumber: new FormControl(''),
        kagText: new FormControl(''),
      };
      const createRow = this.formBuilder.group(rowData);
      formArray.push(createRow);
    }
    this.rows = formArray;
  }

Instead of going with FormArray.push() you could go with FormArray.insert(0) which would add the row at the top (as per the doc ).而不是使用FormArray.push()您可以使用 go 使用FormArray.insert(0)将行添加到顶部(根据文档)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM