2017-10-18 96 views

回答

0

要dymanically填充您的NG2智能表,你可以按照下面的步驟。 1.在你的模塊中導入智能表組件。 從「ng2-smart-table」導入{LocalDataSource};

2.將以下代碼添加到您的類中。

 @Component({ 
     selector: 'my-app', 
     template: ` 
     <div> 
      <h2>Hello {{name}}</h2> 
      <button (click)="addColumn()">Add Column</button> 
      <ng2-smart-table [settings]="settings" [source]="source"></ng2-smart-table> 
     </div> 

     `, 
    }) 
    export class ResultComponent implements OnInit 
     { 
     source: LocalDataSource; 
     i = 0; 
     settings; 
     mySettings = { 
     mode: 'inline', 
     actions: { 
      delete:false, 
     }, 
     add: { 
     confirmCreate: true, 
     }, 
     delete: { 
     confirmDelete: true, 
     }, 
     edit: { 
     confirmSave: true, 
     }, 
     columns: { 

     } 
     }; 

    //method that adds the column. You can use trigger events to do this 
    public addColumn() { 
      this.mySettings.columns["new column " + this.i] = { title: 'new column 
     ' + this.i.toString()}; 
      this.settings = Object.assign({}, this.mySettings); 
      this.i++; 
     } 

    }