Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(function() {
- var options = [
- {
- id: "A",
- color: "btn-info",
- icon: "far fa-folder-open",
- func: "load(this.id);",
- }
- ];
- var params = {
- table: "resTable",
- url: "Model/DB_Reservations.php",
- post: {
- action: "loadAllRes",
- },
- sort: [[4, "desc"]],
- download: true,
- sort: true
- };
- loadTable(params, options);
- });
- function load(id){
- redirectUrl("reservation/information?id="+id.substr(1));
- }
- function loadTable(params, options = []) {
- if (params.download)
- 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>>";
- else
- dom = "<'row my-1'<'col-sm-12'l>> <'row'<'col-sm-12'tr>> <'row'<'col-sm-5'i><'col-sm-7'p>>";
- if (params.save == null){
- params.save = true; //Default
- }
- if (params.count == null){
- params.count = true;
- }
- if (params.filter == null){
- params.filter = true;
- }
- if (params.finalCallback == null){
- novis = [0];
- }else{
- novis = [0, $("#"+params.table+" thead tr th").length - 1];
- }
- var columns = [
- {
- "targets": 0,
- "orderable": false,
- "render": function ( data, type, row, meta ) {
- str = "";
- for(var i = 0; i < options.length; i++){
- 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>";
- }
- return str;
- }
- },
- {
- "targets": novis, //Hide last column from column visibility
- "className": 'noVis'
- }
- ];
- if (params.columns == null){
- params.columns = columns; //Default
- }else{
- params.columns.concat(columns);
- }
- return $('#'+params.table).DataTable( {
- "deferRender": true,
- "processing": false,
- "serverSide": true,
- "ajax": {
- "url": secureUrl(params.url),
- "type": "POST",
- "data": params.post,
- "done": function(xhr){
- console.log(xhr);
- },
- "error": function (xhr, error, code){
- console.log(error);
- console.log(code);
- console.log(xhr);
- },
- },
- "pageLength": 25,
- "language": lang,
- "columnDefs": params.columns,
- "rowCallback": function(row, data){
- if(params.rowCallback != null){
- params.rowCallback(row, data);
- }
- }, //post process' each row after it have been generated for each table draw, but before it is rendered into the document
- "order": params.sort,
- "dom": dom,
- "buttons": [
- {
- extend: 'excel',
- exportOptions: {
- columns: ':visible'
- }
- },
- {
- extend: 'pdf',
- exportOptions: {
- columns: ':visible'
- }
- },
- {
- extend: 'print',
- exportOptions: {
- columns: ':visible'
- }
- },
- {
- extend: 'colvis',
- columns: ':not(.noVis)'
- }
- ],
- "stateSave": params.save,
- 'stateSaveParams': function(settings, data) { //Prevent last column to show after stateSave
- data.columns.forEach(function(column) {
- column.search.search = "";
- delete column.visible;
- });
- },
- "responsive": {
- details: false
- },
- "autoWidth": false,
- "bSortCellsTop": true,
- "bLengthChange": params.count,
- "bFilter": params.filter,
- "initComplete": function () {
- if ($('#'+params.table+" .datatableSearch")[0] != null){
- // apply the search
- this.api().columns().every( function () {
- var that = this;
- $( 'input, select', this.footer() ).on( 'keyup change clear', function () {
- if ( that.search() !== this.value ) {
- that.search( this.value ).draw();
- }
- });
- } );
- }
- if(params.finalCallback != null){
- params.finalCallback();
- }
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement