Advertisement
Guest User

Untitled

a guest
Jun 11th, 2021
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(function() {
  2.  
  3.     var options = [
  4.       {
  5.         id: "A",
  6.         color: "btn-info",
  7.         icon: "far fa-folder-open",
  8.         func: "load(this.id);",
  9.       }
  10.     ];
  11.  
  12.     var params = {
  13.       table: "resTable",
  14.       url: "Model/DB_Reservations.php",
  15.       post: {
  16.         action: "loadAllRes",
  17.       },
  18.       sort: [[4, "desc"]],
  19.       download: true,
  20.       sort: true
  21.     };
  22.  
  23.     loadTable(params, options);
  24. });
  25.  
  26. function load(id){
  27.     redirectUrl("reservation/information?id="+id.substr(1));
  28. }
  29.  
  30.  
  31.  
  32. function loadTable(params, options = []) {
  33.  
  34.     if (params.download)
  35.       dom = "<'row my-1'<'col-md-8 col-sm-12'l><'col-md-4 col-sm-12 text-right'B>> <'row'<'col-sm-12'tr>> <'row'<'col-sm-5'i><'col-sm-7'p>>";
  36.     else
  37.       dom = "<'row my-1'<'col-sm-12'l>> <'row'<'col-sm-12'tr>> <'row'<'col-sm-5'i><'col-sm-7'p>>";
  38.  
  39.     if (params.save == null){
  40.       params.save = true; //Default
  41.     }
  42.  
  43.     if (params.count == null){
  44.       params.count = true;
  45.     }
  46.  
  47.     if (params.filter == null){
  48.       params.filter = true;
  49.     }
  50.  
  51.     if (params.finalCallback == null){
  52.       novis = [0];
  53.     }else{
  54.       novis = [0, $("#"+params.table+" thead tr th").length - 1];
  55.     }
  56.  
  57.     var columns = [
  58.       {
  59.         "targets": 0,
  60.         "orderable": false,
  61.         "render": function ( data, type, row, meta ) {
  62.           str = "";
  63.           for(var i = 0; i < options.length; i++){
  64.               str += "<button id='"+options[i].id+data+"' class='btn "+options[i].color+" mr-1' onclick='"+options[i].func+"' data-toggle='tooltip' title='"+options[i].tooltip+"'><span class='"+options[i].icon+"'></span></button>";
  65.           }
  66.           return str;
  67.         }
  68.       },
  69.       {
  70.         "targets": novis, //Hide last column from column visibility
  71.         "className": 'noVis'
  72.       }
  73.     ];
  74.  
  75.     if (params.columns == null){
  76.       params.columns = columns; //Default
  77.     }else{
  78.       params.columns.concat(columns);
  79.     }
  80.  
  81.  
  82.  
  83.     return $('#'+params.table).DataTable( {
  84.       "deferRender": true,
  85.       "processing": false,
  86.       "serverSide": true,
  87.       "ajax": {
  88.         "url": secureUrl(params.url),
  89.         "type": "POST",
  90.         "data": params.post,
  91.         "done": function(xhr){
  92.           console.log(xhr);
  93.         },
  94.         "error": function (xhr, error, code){
  95.           console.log(error);
  96.           console.log(code);
  97.           console.log(xhr);
  98.         },
  99.       },
  100.       "pageLength": 25,
  101.       "language": lang,
  102.       "columnDefs": params.columns,
  103.       "rowCallback": function(row, data){
  104.         if(params.rowCallback != null){
  105.           params.rowCallback(row, data);
  106.         }
  107.       }, //post process' each row after it have been generated for each table draw, but before it is rendered into the document
  108.       "order": params.sort,
  109.       "dom": dom,
  110.       "buttons": [
  111.         {
  112.           extend: 'excel',
  113.           exportOptions: {
  114.             columns: ':visible'
  115.           }
  116.         },
  117.         {
  118.           extend: 'pdf',
  119.           exportOptions: {
  120.             columns: ':visible'
  121.           }
  122.         },
  123.         {
  124.           extend: 'print',
  125.           exportOptions: {
  126.             columns: ':visible'
  127.           }
  128.         },
  129.         {
  130.           extend: 'colvis',
  131.           columns: ':not(.noVis)'
  132.         }
  133.       ],
  134.       "stateSave": params.save,
  135.       'stateSaveParams': function(settings, data) { //Prevent last column to show after stateSave
  136.         data.columns.forEach(function(column) {
  137.           column.search.search = "";
  138.           delete column.visible;
  139.         });
  140.       },
  141.       "responsive": {
  142.         details: false
  143.       },
  144.       "autoWidth": false,
  145.       "bSortCellsTop": true,
  146.       "bLengthChange": params.count,
  147.       "bFilter": params.filter,
  148.       "initComplete": function () {
  149.         if ($('#'+params.table+" .datatableSearch")[0] != null){
  150.  
  151.           // apply the search
  152.           this.api().columns().every( function () {
  153.               var that = this;
  154.  
  155.               $( 'input, select', this.footer() ).on( 'keyup change clear', function () {
  156.                 if ( that.search() !== this.value ) {
  157.                   that.search( this.value ).draw();
  158.                 }
  159.               });
  160.           } );
  161.         }
  162.  
  163.         if(params.finalCallback != null){
  164.           params.finalCallback();
  165.         }
  166.       }
  167.     });
  168.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement