DataTable column re-order
// install colreorder
datatables.net-colreorder (v.2.1.0)
├── datatables.net-colreorder-dt@2.1.0
├── datatables.net-colreorder@2.1.0
├── datatables.net-dt@2.3.1
├── datatables.net-staterestore-dt@1.4.1
├── datatables.net@2.3.1
npm i datatables.net-colreorder@2.1.0 datatables.net@2.3.1
npm i datatables.net-colreorder-dt@2.1.0 datatables.net-dt@2.3.1
angular.json
"node_modules/datatables.net-colreorder/js/dataTables.colReorder.js"
// Some code
import { CurrentCustomer } from 'src/app/models/current-customer';
import { DataTableConfigService } from 'src/app/services/data-table-config.service';
1.
disableOrderColumn = [4]; // Disable ordering for the last column
isForCustomer = true;
enableEdit = false;
constructor(
private dtConfigService: DataTableConfigService
) { }
2.
ngOnInit(): void {
let isForCustomer = CurrentCustomer.IsCustomer()
this.enableEdit = this.dtConfigService.hasAdminOrSupervisorAccess();
if (this.enableEdit) {
this.disableOrderColumn = [5];
}
else {
this.disableOrderColumn = [4];
}
if (!isForCustomer) {
this.getRfidList();
} else {
// this.getRfidListByCustomerId(CurrentCustomer.CustomerId());
}
}
3.
buildDtOptions() {
this.dtOptions = this.dtConfigService.buildDtOptions(
{
// pageLength: 25
// order: [[1, 'asc']],
// serverSide: true, ///<<<<<<< Enable server-side processing for paged list
},
this.disableOrderColumn,
{
//---------------------------------------------------
data: this.CarList, //<--- custom property
columns: [
{ data: 'id', name: 'id', },
{ data: 'rfidNo', name: 'rfidNo' },
{ data: 'serialNo', name: 'serialNo' },
{
data: 'isAdmin', name: 'isAdmin',
// orderable: false, ///<<<<< disable orderable
render: (data: any, type: any, row: any, meta: any) => {
if (data) {
return '<div class="text-center text-success"> Yes </div>';
}
else {
return '<div class="text-center text-danger"> No </div>';
}
}
},
{ data: 'rego', name: 'rego' },
//---------------------------------------------------
{
data: null,
render: (data: any, type: any, row: any, meta: any) => {
// console.log('✅changed', this.RfidList.size);
if (this.enableEdit) {
return (
'<div class="text-center operation-3">' +
'<button mat-button class="btn btn-outline-warning btn-sm edit-btn">' +
'<i class="fa fa-edit" title="Edit"></i>' +
'</button><span class="pe-1"></span>' +
'<button mat-button class="btn btn-outline-danger btn-sm delete-btn">' +
'<i class="fa fa-trash" title="Delete"></i>' +
'</button>' +
'</div>'
);
} else return '<div></div>';
},
createdCell: (td, cellData, rowData, row, col) => {
const self = this;
$(td)
.off('click')
.on('click', '.edit-btn', (e) => {
e.stopPropagation(); // Optional: stop event bubbling
self.editDialog('Edit', rowData);
})
.on('click', '.delete-btn', function (e) {
self.deleteDialog(rowData.id, rowData.name);
});
},
className: 'sticky-col-right',
},
],
/// below code is only for paged list
extraButtons: this.enableEdit ? [
'copy',
{
text: 'CSV',
extend: 'csvHtml5',
className: 'btn-success',
exportOptions: {
columns: ':visible:not(.noExport)'
}
}, 'pdf', 'excel', 'print',
] : [], // Add extra buttons only if the user has edit permissions
}, // <--- custom property
);
}
4.
getCarList() {
this.isLoading = true;
return this.carrierService.getAllCarriers().subscribe({
next: (res) => {
this.CarList = res;
this.buildDtOptions(); ///<<<<<<<<
this.dtTrigger.next(this.dtOptions);
this.isLoading = false;
},
error: (error) => {
console.log('Error: retrive Carrier list error - ' + error);
this.toastr.warning(error.status + '- retrive data error', 'Fail');
},
});
}Last updated
Was this helpful?