/**
* App user list (jquery)
*/
'use strict';
$(function () {
var dataTablePermissions = $('.datatables-permissions'),
dt_permission,
userList = baseUrl + 'app/user/list';
// Users List datatable
if (dataTablePermissions.length) {
dt_permission = dataTablePermissions.DataTable({
ajax: assetsPath + 'json/permissions-list.json', // JSON file to add data
columns: [
// columns according to JSON
{ data: '' },
{ data: 'id' },
{ data: 'name' },
{ data: 'assigned_to' },
{ data: 'created_date' },
{ data: '' }
],
columnDefs: [
{
// For Responsive
className: 'control',
orderable: false,
searchable: false,
responsivePriority: 2,
targets: 0,
render: function (data, type, full, meta) {
return '';
}
},
{
targets: 1,
searchable: false,
visible: false
},
{
// Name
targets: 2,
render: function (data, type, full, meta) {
var $name = full['name'];
return '' + $name + '';
}
},
{
// User Role
targets: 3,
orderable: false,
render: function (data, type, full, meta) {
var $assignedTo = full['assigned_to'],
$output = '';
var roleBadgeObj = {
Admin: 'Administrator',
Manager: 'Manager',
Users: 'Users',
Support: 'Support',
Restricted:
'Restricted User'
};
for (var i = 0; i < $assignedTo.length; i++) {
var val = $assignedTo[i];
$output += roleBadgeObj[val];
}
return '' + $output + '';
}
},
{
// remove ordering from Name
targets: 4,
orderable: false,
render: function (data, type, full, meta) {
var $date = full['created_date'];
return '' + $date + '';
}
},
{
// Actions
targets: -1,
searchable: false,
title: 'Actions',
orderable: false,
render: function (data, type, full, meta) {
return (
'
'
);
}
}
],
order: [[1, 'asc']],
dom:
'<"row mx-1"' +
'<"col-sm-12 col-md-3" l>' +
'<"col-sm-12 col-md-9"<"dt-action-buttons text-xl-end text-lg-start text-md-end text-start d-flex align-items-center justify-content-md-end justify-content-center flex-wrap"<"me-4 mt-n6 mt-md-0"f>B>>' +
'>t' +
'<"row"' +
'<"col-sm-12 col-md-6"i>' +
'<"col-sm-12 col-md-6"p>' +
'>',
language: {
sLengthMenu: 'Show _MENU_',
search: '',
searchPlaceholder: 'Search Permissions',
paginate: {
next: '',
previous: ''
}
},
// Buttons with Dropdown
buttons: [
{
text: 'Add Permission',
className: 'add-new btn btn-primary mb-6 mb-md-0 waves-effect waves-light',
attr: {
'data-bs-toggle': 'modal',
'data-bs-target': '#addPermissionModal'
},
init: function (api, node, config) {
$(node).removeClass('btn-secondary');
}
}
],
// For responsive popup
responsive: {
details: {
display: $.fn.dataTable.Responsive.display.modal({
header: function (row) {
var data = row.data();
return 'Details of ' + data['name'];
}
}),
type: 'column',
renderer: function (api, rowIdx, columns) {
var data = $.map(columns, function (col, i) {
return col.title !== '' // ? Do not show row in modal popup if title is blank (for check box)
? '' +
'' +
col.title +
':' +
' | ' +
'' +
col.data +
' | ' +
'
'
: '';
}).join('');
return data ? $('').append(data) : false;
}
}
},
initComplete: function () {
// Adding role filter once table initialized
this.api()
.columns(3)
.every(function () {
var column = this;
var select = $(
''
)
.appendTo('.user_role')
.on('change', function () {
var val = $.fn.dataTable.util.escapeRegex($(this).val());
column.search(val ? '^' + val + '$' : '', true, false).draw();
});
column
.data()
.unique()
.sort()
.each(function (d, j) {
select.append('');
});
});
}
});
}
// Delete Record
$('.datatables-permissions tbody').on('click', '.delete-record', function () {
dt_permission.row($(this).parents('tr')).remove().draw();
});
// Filter form control to default size
// ? setTimeout used for multilingual table initialization
setTimeout(() => {
$('.dataTables_filter .form-control').removeClass('form-control-sm');
$('.dataTables_length .form-select').removeClass('form-select-sm');
$('.dataTables_info').addClass('ms-n1');
$('.dataTables_paginate').addClass('me-n1');
}, 300);
});