我将如何在 angular 4 中动态填充 ng2-smart-table 的列

How would i dynamically populate the columns of ng2-smart-table in angular 4

我是 Angular 的新手,在找到解决方法之前遇到了严重的问题。 使用此 link 预览解决方案

 <https://plnkr.co/edit/OtJI13uA89caf8TG5lbI?p=preview>?

要动态填充您的 ng2-smart-table,您可以按照以下步骤操作。 1. 在您的模块中导入智能 table 组件。 从 "ng2-smart-table";

导入 { LocalDataSource }

2.add下面的代码给你class.

     @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++;
      }

     }