| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591 |
- /**
- * DataTables Extensions (js)
- */
-
- 'use strict';
-
- document.addEventListener('DOMContentLoaded', function (e) {
- const dt_scrollable_table = document.querySelector('.dt-scrollableTable');
- let dt_scrollableTable;
-
- // Scrollable
- // --------------------------------------------------------------------
-
- if (dt_scrollable_table) {
- dt_scrollableTable = new DataTable(dt_scrollable_table, {
- ajax: assetsPath + 'json/table-datatable.json',
- columns: [
- { data: 'full_name' },
- { data: 'post' },
- { data: 'email' },
- { data: 'city' },
- { data: 'start_date' },
- { data: 'salary' },
- { data: 'age' },
- { data: 'experience' },
- { data: '' },
- { data: '' }
- ],
- columnDefs: [
- {
- // Label
- targets: -2,
- render: function (data, type, full, meta) {
- const statusNumber = full.status;
- const statuses = {
- 1: { title: 'Current', class: 'bg-label-primary' },
- 2: { title: 'Professional', class: 'bg-label-success' },
- 3: { title: 'Rejected', class: 'bg-label-danger' },
- 4: { title: 'Resigned', class: 'bg-label-warning' },
- 5: { title: 'Applied', class: 'bg-label-info' }
- };
-
- if (typeof statuses[statusNumber] === 'undefined') {
- return data;
- }
-
- return `
- <span class="badge ${statuses[statusNumber].class}">
- ${statuses[statusNumber].title}
- </span>
- `;
- }
- },
- {
- // Actions
- targets: -1,
- title: 'Actions',
- searchable: false,
- className: 'd-flex align-items-center',
- orderable: false,
- render: function (data, type, full, meta) {
- return (
- '<div class="d-inline-block">' +
- '<a href="javascript:;" class="btn btn-icon dropdown-toggle hide-arrow" data-bs-toggle="dropdown"><i class="bx bx-dots-vertical-rounded icon-base"></i></a>' +
- '<div class="dropdown-menu dropdown-menu-end m-0">' +
- '<a href="javascript:;" class="dropdown-item">Details</a>' +
- '<a href="javascript:;" class="dropdown-item">Archive</a>' +
- '<div class="dropdown-divider"></div>' +
- '<a href="javascript:;" class="dropdown-item text-danger delete-record">Delete</a>' +
- '</div>' +
- '</div>' +
- '<a href="javascript:;" class="item-edit text-body"><i class="bx bxs-edit icon-base"></i></a>'
- );
- }
- }
- ],
- // Scroll options
- scrollY: '300px',
- scrollX: true,
- layout: {
- topStart: {
- rowClass: 'row mx-3 my-0 justify-content-between',
- features: [
- {
- pageLength: {
- menu: [7, 10, 25, 50, 100],
- text: 'Show_MENU_entries'
- }
- }
- ]
- },
- topEnd: {
- search: {
- placeholder: ''
- }
- },
- bottomStart: {
- rowClass: 'row mx-3 justify-content-between',
- features: ['info']
- },
- bottomEnd: 'paging'
- },
- language: {
- paginate: {
- next: '<i class="icon-base bx bx-chevron-right scaleX-n1-rtl icon-18px"></i>',
- previous: '<i class="icon-base bx bx-chevron-left scaleX-n1-rtl icon-18px"></i>',
- first: '<i class="icon-base bx bx-chevrons-left scaleX-n1-rtl icon-18px"></i>',
- last: '<i class="icon-base bx bx-chevrons-right scaleX-n1-rtl icon-18px"></i>'
- }
- },
- initComplete: function (settings, json) {
- // Add the mti-n1 class to the first row in tbody
- dt_scrollable_table.querySelector('tbody tr:first-child').classList.add('border-top-0');
- }
- });
- }
-
- // FixedHeader
- // --------------------------------------------------------------------
-
- const dt_fixedheader_table = document.querySelector('.dt-fixedheader');
- let dt_fixedheader;
-
- if (dt_fixedheader_table) {
- dt_fixedheader = new DataTable(dt_fixedheader_table, {
- ajax: assetsPath + 'json/table-datatable.json',
- columns: [
- { data: '' },
- { data: 'id', orderable: false, render: DataTable.render.select() },
- { data: 'id' },
- { data: 'full_name' },
- { data: 'email' },
- { data: 'start_date' },
- { data: 'salary' },
- { data: 'status' },
- { data: '' }
- ],
- columnDefs: [
- {
- className: 'control',
- orderable: false,
- targets: 0,
- responsivePriority: 3,
- render: function (data, type, full, meta) {
- return '';
- }
- },
- {
- // For Checkboxes
- targets: 1,
- orderable: false,
- render: function () {
- return '<input type="checkbox" class="dt-checkboxes form-check-input">';
- },
- checkboxes: {
- selectAllRender: '<input type="checkbox" class="form-check-input">'
- },
- responsivePriority: 4
- },
- {
- targets: 2,
- visible: false
- },
- {
- // Avatar image/badge, Name and post
- targets: 3,
- render: function (data, type, full, meta) {
- const userImg = full.avatar;
- const name = full.full_name;
- const post = full.post;
- let output;
-
- if (userImg) {
- // For Avatar image
- output = `<img src="${assetsPath}img/avatars/${userImg}" alt="Avatar" class="rounded-circle">`;
- } else {
- // For Avatar badge
- const stateNum = Math.floor(Math.random() * 6);
- const states = ['success', 'danger', 'warning', 'info', 'dark', 'primary', 'secondary'];
- const state = states[stateNum];
- const initials = (name.match(/\b\w/g) || []).map(i => i.toUpperCase()).join('');
- output = `<span class="avatar-initial rounded-circle bg-label-${state}">${initials}</span>`;
- }
-
- // Creates full output for row
- const rowOutput = `
- <div class="d-flex justify-content-start align-items-center">
- <div class="avatar-wrapper">
- <div class="avatar me-2">
- ${output}
- </div>
- </div>
- <div class="d-flex flex-column">
- <span class="emp_name text-truncate">${name}</span>
- <small class="emp_post text-truncate text-body-secondary">${post}</small>
- </div>
- </div>
- `;
-
- return rowOutput;
- },
- responsivePriority: 5
- },
- {
- responsivePriority: 1,
- targets: 4
- },
- {
- responsivePriority: 2,
- targets: 6
- },
-
- {
- // Label
- targets: -2,
- render: function (data, type, full, meta) {
- const statusNumber = full.status;
- const statuses = {
- 1: { title: 'Current', class: 'bg-label-primary' },
- 2: { title: 'Professional', class: 'bg-label-success' },
- 3: { title: 'Rejected', class: 'bg-label-danger' },
- 4: { title: 'Resigned', class: 'bg-label-warning' },
- 5: { title: 'Applied', class: 'bg-label-info' }
- };
-
- if (typeof statuses[statusNumber] === 'undefined') {
- return data;
- }
-
- return `
- <span class="badge ${statuses[statusNumber].class}">
- ${statuses[statusNumber].title}
- </span>
- `;
- }
- },
- {
- // Actions
- targets: -1,
- title: 'Actions',
- className: 'd-flex align-items-center',
- orderable: false,
- render: function (data, type, full, meta) {
- return (
- '<div class="d-inline-block">' +
- '<a href="javascript:;" class="btn btn-icon dropdown-toggle hide-arrow" data-bs-toggle="dropdown"><i class="bx bx-dots-vertical-rounded icon-base"></i></a>' +
- '<div class="dropdown-menu dropdown-menu-end m-0">' +
- '<a href="javascript:;" class="dropdown-item">Details</a>' +
- '<a href="javascript:;" class="dropdown-item">Archive</a>' +
- '<div class="dropdown-divider"></div>' +
- '<a href="javascript:;" class="dropdown-item text-danger delete-record">Delete</a>' +
- '</div>' +
- '</div>' +
- '<a href="javascript:;" class="btn btn-icon item-edit"><i class="bx bxs-edit icon-base"></i></a>'
- );
- }
- }
- ],
- select: {
- style: 'multi',
- selector: 'td:nth-child(2)'
- },
- order: [[2, 'desc']],
- layout: {
- topStart: {
- rowClass: 'row mx-3 my-0 justify-content-between',
- features: [
- {
- pageLength: {
- menu: [7, 10, 25, 50, 100],
- text: 'Show_MENU_entries'
- }
- }
- ]
- },
- topEnd: {
- search: {
- placeholder: ''
- }
- },
- bottomStart: {
- rowClass: 'row mx-3 justify-content-between',
- features: ['info']
- },
- bottomEnd: 'paging'
- },
- displayLength: 7,
- language: {
- paginate: {
- next: '<i class="icon-base bx bx-chevron-right scaleX-n1-rtl icon-18px"></i>',
- previous: '<i class="icon-base bx bx-chevron-left scaleX-n1-rtl icon-18px"></i>',
- first: '<i class="icon-base bx bx-chevrons-left scaleX-n1-rtl icon-18px"></i>',
- last: '<i class="icon-base bx bx-chevrons-right scaleX-n1-rtl icon-18px"></i>'
- }
- },
- responsive: {
- details: {
- display: DataTable.Responsive.display.modal({
- header: function (row) {
- var data = row.data();
- return 'Details of ' + data['full_name'];
- }
- }),
- type: 'column',
- renderer: function (api, rowIdx, columns) {
- const data = columns
- .map(function (col) {
- return col.title !== '' // Do not show row in modal popup if title is blank (for check box)
- ? `<tr data-dt-row="${col.rowIndex}" data-dt-column="${col.columnIndex}">
- <td>${col.title}:</td>
- <td>${col.data}</td>
- </tr>`
- : '';
- })
- .join('');
-
- if (data) {
- const div = document.createElement('div');
- div.classList.add('table-responsive');
- const table = document.createElement('table');
- div.appendChild(table);
- table.classList.add('table');
- const tbody = document.createElement('tbody');
- tbody.innerHTML = data;
- table.appendChild(tbody);
- return div;
- }
- return false;
- }
- }
- }
- });
- // Fixed header
- if (window.Helpers.isNavbarFixed()) {
- const navHeight = document.getElementById('layout-navbar').offsetHeight;
- new DataTable.FixedHeader(dt_fixedheader).headerOffset(navHeight);
- } else {
- new DataTable.FixedHeader(dt_fixedheader);
- }
-
- //? The 'delete-record' class is necessary for the functionality of the following code.
- document.addEventListener('click', function (e) {
- if (e.target.classList.contains('delete-record')) {
- dt_fixedheader.row(e.target.closest('tr')).remove().draw();
- const modalEl = document.querySelector('.dtr-bs-modal');
- if (modalEl && modalEl.classList.contains('show')) {
- const modal = bootstrap.Modal.getInstance(modalEl);
- modal?.hide();
- }
- }
- });
- }
-
- // FixedColumns
- // --------------------------------------------------------------------
-
- const dt_fixedcolumns_table = document.querySelector('.dt-fixedcolumns');
- let dt_fixedcolumns;
-
- if (dt_fixedcolumns_table) {
- let tableTitle = document.createElement('h5');
- tableTitle.classList.add('card-title', 'mb-0', 'text-md-start', 'text-center', 'pb-md-0', 'pb-6');
- tableTitle.innerHTML = 'Fixed Columns';
- dt_fixedcolumns = new DataTable(dt_fixedcolumns_table, {
- ajax: assetsPath + 'json/table-datatable.json',
- columns: [
- { data: 'full_name' },
- { data: 'post' },
- { data: 'email' },
- { data: 'city' },
- { data: 'start_date' },
- { data: 'salary' },
- { data: 'age' },
- { data: 'experience' },
- { data: 'status' },
- { data: 'id' }
- ],
- columnDefs: [
- {
- // Label
- targets: -2,
- render: function (data, type, full, meta) {
- const statusNumber = full.status;
- const statuses = {
- 1: { title: 'Current', class: 'bg-label-primary' },
- 2: { title: 'Professional', class: 'bg-label-success' },
- 3: { title: 'Rejected', class: 'bg-label-danger' },
- 4: { title: 'Resigned', class: 'bg-label-warning' },
- 5: { title: 'Applied', class: 'bg-label-info' }
- };
-
- if (typeof statuses[statusNumber] === 'undefined') {
- return data;
- }
-
- return `
- <span class="badge ${statuses[statusNumber].class}">
- ${statuses[statusNumber].title}
- </span>
- `;
- }
- },
- {
- // Actions
- targets: -1,
- title: 'Actions',
- searchable: false,
- className: 'd-flex align-items-center',
- orderable: false,
- render: function (data, type, full, meta) {
- return (
- '<div class="d-inline-block">' +
- '<a href="javascript:;" class="btn btn-icon dropdown-toggle hide-arrow" data-bs-toggle="dropdown"><i class="bx bx-dots-vertical-rounded icon-base"></i></a>' +
- '<div class="dropdown-menu dropdown-menu-end m-0">' +
- '<a href="javascript:;" class="dropdown-item">Details</a>' +
- '<a href="javascript:;" class="dropdown-item">Archive</a>' +
- '<div class="dropdown-divider"></div>' +
- '<a href="javascript:;" class="dropdown-item text-danger delete-record"></i>Delete</a>' +
- '</div>' +
- '</div>' +
- '<a href="javascript:;" class="item-edit text-body"><i class="bx bxs-edit icon-base"></i></a>'
- );
- }
- }
- ],
- layout: {
- topStart: {
- rowClass: 'row card-header pt-0 pb-0',
- features: [tableTitle]
- },
- topEnd: {
- search: {
- placeholder: ''
- }
- },
- bottomStart: {
- rowClass: 'row mx-3 justify-content-between',
- features: ['info']
- },
- bottomEnd: 'paging'
- },
- scrollY: 300,
- scrollX: true,
- scrollCollapse: true,
- paging: false,
- info: false,
- // Fixed column option
- fixedColumns: {
- start: 1
- },
- initComplete: function (settings, json) {
- // Add the mti-n1 class to the first row in tbody
- dt_fixedcolumns_table.querySelector('tbody tr:first-child').classList.add('border-top-0');
- }
- });
-
- //? The 'delete-record' class is necessary for the functionality of the following code.
- document.addEventListener('click', function (e) {
- if (e.target.classList.contains('delete-record')) {
- dt_fixedcolumns.row(e.target.closest('tr')).remove().draw();
- const modalEl = document.querySelector('.dtr-bs-modal');
- if (modalEl && modalEl.classList.contains('show')) {
- const modal = bootstrap.Modal.getInstance(modalEl);
- modal?.hide();
- }
- }
- });
- }
-
- // Select
- // --------------------------------------------------------------------
-
- const dt_select_table = document.querySelector('.dt-select-table');
- let dt_select;
-
- if (dt_select_table) {
- dt_select = new DataTable(dt_select_table, {
- ajax: assetsPath + 'json/table-datatable.json',
- columns: [
- { data: 'id', orderable: false, render: DataTable.render.select() },
- { data: 'full_name' },
- { data: 'post' },
- { data: 'email' },
- { data: 'city' },
- { data: 'start_date' },
- { data: 'salary' },
- { data: 'status' }
- ],
- columnDefs: [
- {
- // For Checkboxes
- targets: 0,
- searchable: false,
- orderable: false,
- render: function () {
- return '<input type="checkbox" class="dt-checkboxes form-check-input">';
- },
- checkboxes: {
- selectRow: true,
- selectAllRender: '<input type="checkbox" class="form-check-input">'
- }
- },
- {
- // Label
- targets: -1,
- render: function (data, type, full, meta) {
- const statusNumber = full.status;
- const statuses = {
- 1: { title: 'Current', class: 'bg-label-primary' },
- 2: { title: 'Professional', class: 'bg-label-success' },
- 3: { title: 'Rejected', class: 'bg-label-danger' },
- 4: { title: 'Resigned', class: 'bg-label-warning' },
- 5: { title: 'Applied', class: 'bg-label-info' }
- };
-
- if (typeof statuses[statusNumber] === 'undefined') {
- return data;
- }
-
- return `
- <span class="badge ${statuses[statusNumber].class}">
- ${statuses[statusNumber].title}
- </span>
- `;
- }
- }
- ],
- order: [[1, 'desc']],
- layout: {
- topStart: {
- rowClass: 'row mx-3 my-0 justify-content-between',
- features: [
- {
- pageLength: {
- menu: [7, 10, 25, 50, 100],
- text: 'Show_MENU_entries'
- }
- }
- ]
- },
- topEnd: {
- search: {
- placeholder: ''
- }
- },
- bottomStart: {
- rowClass: 'row mx-3 justify-content-between',
- features: ['info']
- },
- bottomEnd: 'paging'
- },
- language: {
- paginate: {
- next: '<i class="icon-base bx bx-chevron-right scaleX-n1-rtl icon-18px"></i>',
- previous: '<i class="icon-base bx bx-chevron-left scaleX-n1-rtl icon-18px"></i>',
- first: '<i class="icon-base bx bx-chevrons-left scaleX-n1-rtl icon-18px"></i>',
- last: '<i class="icon-base bx bx-chevrons-right scaleX-n1-rtl icon-18px"></i>'
- }
- },
- select: {
- // Select style
- style: 'multi'
- }
- });
- }
-
- // Filter form control to default size
- // ? setTimeout used for multilingual table initialization
- setTimeout(() => {
- const elementsToModify = [
- { selector: '.dt-search .form-control', classToRemove: 'form-control-sm', classToAdd: 'ms-4' },
- { selector: '.dt-length .form-select', classToRemove: 'form-select-sm' },
- { selector: '.dt-layout-table', classToRemove: 'row mt-2' },
- { selector: '.dt-layout-end', classToAdd: 'mt-0' },
- { selector: '.dt-layout-end .dt-search', classToAdd: 'mt-0 mt-md-6' },
- { selector: '.dt-layout-full', classToRemove: 'col-md col-12', classToAdd: 'table-responsive' }
- ];
-
- // Delete record
- elementsToModify.forEach(({ selector, classToRemove, classToAdd }) => {
- document.querySelectorAll(selector).forEach(element => {
- if (classToRemove) {
- classToRemove.split(' ').forEach(className => element.classList.remove(className));
- }
- if (classToAdd) {
- classToAdd.split(' ').forEach(className => element.classList.add(className));
- }
- });
- });
- }, 100);
- });
|