Advertisement
Guest User

Datatable

a guest
Aug 27th, 2018
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(document).ready(function () {
  2.                   // hide loader after succesfully loaded data into datatable by ajax
  3.             $.ajax({
  4.                 "url": url,
  5.                 "dataType": 'json',
  6.                 "success": function (json) {
  7.                         for (var i = 0; i < json.columns.length; i++) {
  8.                             $("#customersTbl tfoot tr ").append('<th></th>')
  9.                          
  10.                     };
  11.                     toDataTable(json);
  12.                     $("customersTbl").show();
  13.                     $(".loader1337").hide();
  14.                     $.fn.dataTable.tables({ visible: true, api: true }).columns.adjust();
  15.          
  16.                 }
  17.             });
  18.         });
  19.  
  20.  
  21.         function toDataTable(json) {
  22.             var table = $('#customersTbl').DataTable({
  23.                 columns: json.columns,
  24.                 data: json.data,
  25.                 language: {
  26.                     search: '',
  27.                     searchPlaceholder: 'Zoeken'
  28.                 },
  29.                 buttons: [
  30.                     {
  31.                         extend: 'colvis',
  32.                         text: '<i class="fa fa-wrench"></i>',
  33.                         init: function (api, node, config) {
  34.                             $(node).removeClass('btn-secondary')
  35.                             $(node).addClass('btn-outline-secondary')
  36.                         },
  37.                         columnText: function (dt, idx, title) {
  38.                             return title;
  39.                         }
  40.                     },
  41.                     {
  42.                         extend: 'copyHtml5',
  43.                         text: '<i class="fa fa-copy"></i>',
  44.                         titleAttr: 'Kopie naar klembord',
  45.                         init: function (api, node, config) {
  46.                             $(node).removeClass('btn-secondary')
  47.                             $(node).addClass('btn-outline-secondary')
  48.                         }
  49.                     },
  50.                     {
  51.                         extend: 'excelHtml5',
  52.                         text: '<i class="fa fa-file-excel"></i>',
  53.                         titleAttr: 'Excel',
  54.                         init: function (api, node, config) {
  55.                             $(node).removeClass('btn-secondary')
  56.                             $(node).addClass('btn-outline-secondary')
  57.                         }
  58.                     },
  59.                     {
  60.                         extend: 'pdfHtml5',
  61.                         text: '<i class="fas fa-file-pdf"></i>',
  62.                         titleAttr: 'PDF',
  63.                         init: function (api, node, config) {
  64.                             $(node).removeClass('btn-secondary')
  65.                             $(node).addClass('btn-outline-secondary')
  66.                         }
  67.  
  68.                     },
  69.                     {
  70.                         extend: 'csvHtml5',
  71.                         text: '<i class="fas fa-code"></i>',
  72.                         titleAttr: 'CSV',
  73.                         init: function (api, node, config) {
  74.                             $(node).removeClass('btn-secondary')
  75.                             $(node).addClass('btn-outline-secondary')
  76.                         }
  77.                     }
  78.                 ],
  79.  
  80.                 responsive: true,
  81.                 bLengthChange: false,
  82.                 bInfo: false,
  83.                 colReorder: true,
  84.                 scrollY: '500px',
  85.                 scroller: true,
  86.                 scrollCollapse: true,
  87.                 stateSave: true,
  88.                 select: true,
  89.                 dom: 'Bfrtil',
  90.                 initComplete: function () {
  91.                  
  92.                     // create filter for every column mentioned in array below.
  93.                     this.api().columns().every(function () {
  94.                         var column = this;
  95.                         var select = $('<select><option value=""></option></select>')
  96.                             .appendTo($(column.footer()).empty())
  97.                             .on('change', function () {
  98.                                 var val = $.fn.dataTable.util.escapeRegex(
  99.                                     $(this).val()
  100.                                 );
  101.                                 column
  102.                                     .search(val ? '^' + val + '$' : '', true, false)
  103.                                     .draw();
  104.                             });
  105.  
  106.                         column.data().unique().sort().each(function (d, j) {
  107.                             select.append('<option value="' + d + '">' + d + '</option>')
  108.                         });
  109.                     });
  110.                 }
  111.             });
  112.  
  113.  
  114.             // trigger colomn reorder after reordering / resizing  etc etc
  115.             table.on('column-visibility.dt column-reorder.dt init.dt responsive-resize responsive-display', function () {
  116.                 $.fn.dataTable.tables({ visible: true, api: true }).columns.adjust();
  117.             });
  118.  
  119.      
  120.             // when clicked on an col that has an URL in it (a link) return false so the default redirect to the clicked rows edit screen doesn't get triggered.
  121.             // else go to edit/single page with data.id from row
  122.             table.on('click', 'tbody td', function () {
  123.                 if ($(this).html().indexOf('href=') != -1) {
  124.                     return;
  125.                 }
  126.                 var data = table.row($(this).parent()).data();
  127.                 console.log("row ID:" + data.id)
  128.                 window.open('viewitem.aspx?tab=1&name=employee&item_id=' + data.id, '_self');
  129.             })
  130.  
  131.             $("#customersTbl_filter").css("float", "right");
  132.             $(".dataTables_info").hide();
  133.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement