我正在使用https://akveo.github.io/ng2-smart-table/#/documentation的ng2-smart-table
现场演示:http://akveo.com/ngx-admin/pages/tables/smart-table
请帮我解决以下问题:
我在.ts和.html文件中写了下面的代码:
智能表component.ts:
actions: {
add: false,
edit: true,
delete: false,
custom: [{ name: 'ourCustomAction'}],
position: 'left',
columnTitle: 'Actions'
},
mode: 'external',
智能表component.html:
<nb-card-body>
<ng2-smart-table [settings]="settings" allowFiltering='true' allowPaging='true' [source]="windchillData"
(deleteConfirm)="onDeleteConfirm($event)" (custom)="onCustomAction($event)" (edit)="onEdit($event)">
</ng2-smart-table>
</nb-card-body>
1-我想选择多行并调用一个函数,以便我需要在ng2-smart-table中编写此代码?
回答:
要在ng2-smart-table
中选择多行,您需要在settings
对象中添加配置。
例:
settings = {
// This `selectMode` is the configuration for selecting multiple rows in the table using checkbox
selectMode: 'multi',
delete: {
confirmDelete: true,
deleteButtonContent: 'Delete data',
saveButtonContent: 'save',
cancelButtonContent: 'cancel'
},
add: {
confirmCreate: true,
},
edit: {
confirmSave: true,
},
columns: {
// columns configuration
},
};
2-我可以选择多行并在所选行上调用一个函数吗?
ng2-smart-table
有一个获取userSelectedRows
的事件,我们可以使用该事件获取所有选定的行,然后调用函数对所有选定的行执行某些操作。
例:
<ng2-smart-table [settings]="settings" allowFiltering='true' allowPaging='true' [source]="windchillData" (deleteConfirm)="onDeleteConfirm($event)" (custom)="onCustomAction($event)" (edit)="onEdit($event)" (userRowSelect)="onUserRowSelect($event)"></ng2-smart-table>
component.ts
中创建事件处理程序以获取所选行onUserRowSelect(event) {
this.selectedRows = event.selected;
}
按钮在html
<button (click)="onClick()"> Get Selected </button>
单击component.ts
中的处理程序
onClick() {
// It will console all the selected rows
console.log(this.selectedRows);
}
在这里你可以看到这个工作:https://stackblitz.com/edit/example-ng2-smart-table-18qsws
我对这个库不太熟悉,但是以下应该有所帮助:
HTML
<button (click)="logAllSelectedRows()">log All selected</button>
<ng2-smart-table [settings]="settings" [source]="source" (deleteConfirm)="onDeleteConfirm($event)" (rowSelect)="rowSelectedHandler($event)">
TS
selectedRows = [];
rowSelectedHandler(rowData:{isSelected:boolean, data:any}){
if(rowData.isSelected === false){
/*remove row*/
this.selectedRows = this.selectedRows.filter((rowItem)=>rowItem .id !== rowData.data.id)
}else {
/*add row*/
this.selectedRows = [...this.selectedRows, ...rowData.data];
console.log('added rowdata');
}
}
logAllSelectedRows(){
console.log(this.selectedRows);
}
在ng2-smart-table设置([settings]="settings"
)中,添加此行以启用选择多行:
selectMode: 'multi',
然后,在您的模板中,将(userRowSelect)事件添加到ng2-smart-table:
<ng2-smart-table [settings]="settings" allowFiltering='true' allowPaging='true' [source]="windchillData" (deleteConfirm)="onDeleteConfirm($event)" (custom)="onCustomAction($event)" (edit)="onEdit($event)" (userRowSelect)="onUserRowSelect($event)"></ng2-smart-table>
在组件中,更新每个(userRowSelect)的选定行列表:
private selectedRows: any;
// ...
onUserRowSelect(event) {
this.selectedRows = event.selected;
}
仍然在组件中,添加一个功能,使用所选行执行所需的操作:
public doSomething() {
// Do something with `this.selectedRows`
}
添加一个按钮,并在单击时调用您的功能!
<button (click)="doSomething()"> Run code on selected rows! </button>