5

我有一个由 ng2-smart-table 构建的表,表中的数据有两种状态,分别是DraftReady。当 时data.status = 'Draft',可以为 CRUD 目的显示操作列,但随后状态变为data.status = 'Ready',我想禁用操作列。如何有条件地做到这一点?

桌面设置:

  tableSettings = {
    add: {
      addButtonContent: '<i class="fas fa-plus fa-fw"></i>',
      createButtonContent: '<i class="fas fa-plus fa-fw"></i>',
      cancelButtonContent: '<i class="fas fa-times fa-fw"></i>',
      confirmCreate: true
    },
    edit: {
      editButtonContent: '<i class="fas fa-pencil-alt fa-fw"></i>',
      saveButtonContent: '<i class="fas fa-check fa-fw"></i>',
      cancelButtonContent: '<i class="fas fa-times fa-fw"></i>',
      confirmSave: true
    },
    delete: {
      deleteButtonContent: '<i class="far fa-trash-alt fa-fw"></i>',
      confirmDelete: true
    },

    columns: {
      title: {
        title: 'Title',
        type: 'text',
        filter: false,
      },
      description: {
        title: 'description',
        type: 'text',
        filter: false,
      }
    }
  };

ngOnInit() {
  this.apiService.getData.subscribe((res: any) => {
    this.data = res;
    console.log(this.data.status);
  });
}
4

4 回答 4

6

我会自己定制。

settings = {
    columns: {
        name: {
            title: 'Name',
            filter: true
        }
    },
    mode: 'external',
    actions: {
        delete: false,
        add: false,
        position: 'right'
    },
    rowClassFunction: (row) => {
        var curUserId = localStorage.getItem('user_id');
        if(row.data.createdBy == parseInt(curUserId)){
            return '';
        } else {
            return 'hide-action';
        }
    },
    edit: {
        editButtonContent: '<i class="ti-pencil text-info m-r-10"></i>'
    }
};
source = new LocalDataSource([{name:'Jai', createdBy:12}, {name:'Jack', createdBy:63}])

添加你的css文件

.hide-action td.ng2-smart-actions a {
    display: none;
}

我的要求是使用编辑授权用户。

于 2018-10-12T14:06:11.877 回答
3

也许有点晚了,但是在你的设置中设置“actions: false”,你可以用一个变量做动态

于 2018-09-12T13:24:02.373 回答
2

遵循我的方法:首先我创建了一个自定义操作组件:

[...]
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { ViewCell } from 'ng2-smart-table';


@Component({
  selector: 'ngx-custom-actions',
  template: `
  <div class="btn-group btn-group-sm" role="group">
  <button (click)="doEdit()"
          nbButton
          outline
          status="info"
          size="small"
          [disabled]="!isEditable"
          class="mr-2">
    Editar
  </button>
  <button (click)="doDelete()"
          nbButton
          outline
          status="danger"
          [disabled]="!isEditable"
          size="small">
    Excluir
  </button>
</div>
  `,
})
export class CustomActionsComponent implements ViewCell, OnInit {

  isEditable: boolean;

  @Input() value: string | number;
  @Input() rowData: any;

  @Output() edit: EventEmitter<any> = new EventEmitter();
  @Output() delete: EventEmitter<any> = new EventEmitter();

  ngOnInit() {
    this.isEditable = this.value === 'Draft';
  }

  doEdit() {
    this.edit.emit(this.rowData);
  }
  doDelete() {
    this.delete.emit(this.rowData);
  }

}

在模块注册并在 entryComponents 注册!

@NgModule({
  declarations: [FormsEngineListComponent, CustomActionsComponent],
  imports: [
    // others imports
    Ng2SmartTableModule,
  ],
  entryComponents: [
    CustomActionsComponent,
  ],
})
export class FormsEngineModule { }

在 ng2-smart-table 上使用您的配置

list.component.ts

settings = {
  actions: false,
  hideSubHeader: true,
  columns: {
    id: {
      title: 'ID',
      type: 'number',
    },
    actions: {
      title: 'Ações',
      type: 'custom',
      renderComponent: CustomActionsComponent,
      onComponentInitFunction(instance) {
        instance.edit.subscribe(row => {
          console.log('edit', row);
        });
        instance.delete.subscribe(row => {
          console.log('delete', row);
        });
      },
    }
  },
};

demoData = [
  {id: '122323', actions: 'Draft'},
  {id: '2342342', actions: 'Ready'},
];

ngOnInit() {
  this.source.load(this.demoData);
}
于 2019-08-16T21:27:16.867 回答
1

隐藏编辑和删除图标的非常有用的提示 (ngx-smart-table) 将此添加到您的组件 css

:host /deep/ ng2-smart-table table > tbody > tr.hide-action > td > ng2-st-tbody-edit-delete {
  display:none;
  visibility: hidden;
}
于 2019-03-13T13:11:06.637 回答