|
|
@@ -18,39 +18,132 @@ import 'datatables.net-responsive-dt';
|
|
|
window.$ = window.jQuery = $;
|
|
|
window.JSZip = JSZip;
|
|
|
// Initialize DataTable
|
|
|
-document.addEventListener('livewire:init', function () {
|
|
|
+let dataTableInstance;
|
|
|
+var searchTerms ={};
|
|
|
+function initializeDataTable() {
|
|
|
var dataTableName = document.getElementById('datatable-config').dataset.datatableName;
|
|
|
var dataTableColumnsConfig = JSON.parse(document.getElementById('datatable-config').dataset.datatableColumnsConfig);
|
|
|
- $('#'+dataTableName+'-table').DataTable({
|
|
|
-
|
|
|
- responsive: true,
|
|
|
- processing: true,
|
|
|
- serverSide: true,
|
|
|
- orderCellsTop: true, // Allows inputs in the header
|
|
|
- fixedHeader: true, // Optional: keeps header fixed while scrolling
|
|
|
- ajax: {
|
|
|
- url: '/api/report-data/' + dataTableName, // Dynamically construct the URL with the datatable name
|
|
|
- type: 'GET', // HTTP method
|
|
|
- },
|
|
|
- columns: dataTableColumnsConfig,
|
|
|
- dom: 'lBfrtip', // DataTables layout controls
|
|
|
- buttons: [
|
|
|
- { extend: 'copyHtml5', className: 'dt-button' },
|
|
|
- { extend: 'csvHtml5', className: 'dt-button' },
|
|
|
- { extend: 'excelHtml5', className: 'dt-button' },
|
|
|
- { extend: 'pdfHtml5', className: 'dt-button' },
|
|
|
- { extend: 'print', className: 'dt-button' },
|
|
|
- ],
|
|
|
- layout: {
|
|
|
- topStart: 'buttons' // Buttons positioned at the top-left
|
|
|
+ var dataTableHeadersConfig = JSON.parse(document.getElementById('datatable-config').dataset.datatableHeadersConfig);
|
|
|
+ if ($.fn.DataTable.isDataTable(`#${dataTableName}-table`)) {
|
|
|
+ dataTableInstance.destroy();
|
|
|
+ $(`#${dataTableName}-table tbody`).empty(); // Clear rows to prevent residue
|
|
|
+ }
|
|
|
+
|
|
|
+ dataTableInstance = $(`#${dataTableName}-table`).DataTable({
|
|
|
+ responsive: true,
|
|
|
+ retrieve: true,
|
|
|
+ searching: false,
|
|
|
+ processing: true,
|
|
|
+ serverSide: true,
|
|
|
+ orderCellsTop: true,
|
|
|
+ fixedHeader: true,
|
|
|
+ ajax: {
|
|
|
+ url: '/api/report-data/' + dataTableName,
|
|
|
+ type: 'POST',
|
|
|
+ dataSrc: function (json) {
|
|
|
+ //console.log('Returned data:', json);
|
|
|
+ //populate any dropdown headers
|
|
|
+ //console.log(dataTableHeadersConfig);
|
|
|
+ dataTableHeadersConfig.forEach(function(headerConfig, index){
|
|
|
+
|
|
|
+ if(headerConfig.searchtype == "dropdown")
|
|
|
+ {
|
|
|
+ //console.log('Tyson here');
|
|
|
+ var dropdown = $('#'+headerConfig.header.replace(' ', '-')+'FilterDropdown');
|
|
|
+ //console.log(dropdown);
|
|
|
+ dropdown.empty().append($('<option>',{
|
|
|
+ value: '',
|
|
|
+ text: '-- Not Filtered --'
|
|
|
+ }));
|
|
|
+ json.dropdowndata[index].forEach(function (option) {
|
|
|
+ var selected = false;
|
|
|
+ if(json.searchterms && json.searchterms[index]){
|
|
|
+ Object.keys(searchTerms[index]).forEach(function (key){
|
|
|
+ var searchValue = searchTerms[index][key]
|
|
|
+ console.log('value: '+searchValue)
|
|
|
+ if(option === searchValue)
|
|
|
+ {
|
|
|
+ selected = true;
|
|
|
+ }})}
|
|
|
+
|
|
|
+ dropdown.append($('<option>',{
|
|
|
+ value: option,
|
|
|
+ text: option
|
|
|
+ }).prop('selected', selected))});
|
|
|
+ }});
|
|
|
+
|
|
|
+
|
|
|
+ return json.data;
|
|
|
},
|
|
|
- columnDefs: [
|
|
|
- { responsivePriority: 1, targets: 0 }, // Prioritize visibility of employee name
|
|
|
- { responsivePriority: 2, targets: -1 } // Secondary priority for office location
|
|
|
- ]
|
|
|
- });
|
|
|
+ data: function (d) {
|
|
|
+ d.searchTerms = searchTerms || {};
|
|
|
+ }
|
|
|
+ },
|
|
|
+ columns: dataTableColumnsConfig,
|
|
|
+ dom: 'lBfrtip',
|
|
|
+ buttons: [
|
|
|
+ { extend: 'copyHtml5', className: 'dt-button' },
|
|
|
+ { extend: 'csvHtml5', className: 'dt-button' },
|
|
|
+ { extend: 'excelHtml5', className: 'dt-button' },
|
|
|
+ { extend: 'pdfHtml5', className: 'dt-button' },
|
|
|
+ { extend: 'print', className: 'dt-button' },
|
|
|
+ ],
|
|
|
+ layout: {
|
|
|
+ topStart: 'buttons'
|
|
|
+ },
|
|
|
+ columnDefs: [
|
|
|
+ { responsivePriority: 1, targets: 0 },
|
|
|
+ { responsivePriority: 2, targets: -1 }
|
|
|
+ ]
|
|
|
+ });
|
|
|
+}
|
|
|
+document.addEventListener('livewire:init', function () {
|
|
|
+
|
|
|
+ console.log('Initializing DataTable');
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ initializeDataTable();
|
|
|
+
|
|
|
|
|
|
});
|
|
|
+let inner = {};
|
|
|
+function getFilterInput(searchTerm, index, searchType, extra){
|
|
|
+
|
|
|
+
|
|
|
+ switch (searchType) {
|
|
|
+ case "text":
|
|
|
+ case "number":
|
|
|
+ inner[index] = searchTerm;
|
|
|
+ break;
|
|
|
+ case "date":
|
|
|
+ if($.isEmptyObject(inner[index])) {
|
|
|
+ inner[index] = {"start": "", "end": ""};
|
|
|
+ }
|
|
|
+ if (extra === "start") {
|
|
|
+ inner[index].start = searchTerm;
|
|
|
+ }
|
|
|
+ else if (extra === "end") {
|
|
|
+ inner[index].end = searchTerm;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "dropdown":
|
|
|
+ inner[index] = searchTerm;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ searchTerms[index] = inner;
|
|
|
+ console.log(searchTerms[index]);
|
|
|
+ dataTableInstance.ajax.reload();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+window.getFilterInput = getFilterInput;
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
|