Advertisement
stevenle

DataTables example

Jun 24th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function initializeDataTableAjax( name, selectClause, fromClause, columns, editFunction, deleteFunction, additionalCommands )
  2. {
  3.     var options = {
  4.         deferRender: true,
  5.         ajax: {
  6.             'url' : '/etc/ajax_list_provider.php',
  7.             'data':{
  8.                 'selectClause'  : selectClause,
  9.                 'fromClause'    : fromClause
  10.             }
  11.         },
  12.         columns: columns,
  13.         columnDefs: [
  14.             { responsivePriority: 10, targets: 'inline-edit-indicator'}
  15.         ],
  16.         createdRow: _dtCreatedRow
  17.     };
  18.  
  19.     _completeDatatablesInit( name, additionalCommands, options );
  20.  
  21.     $("#datatable tbody").on( 'click', 'a.edit', editFunction ).on( 'click', 'a.delete', deleteFunction );
  22.  
  23.     $("#datatable tbody tr td:first-child").attr( 'align', 'center' );
  24.  
  25. }
  26.  
  27. function _completeDatatablesInit( name, additionalCommands, options )
  28. {
  29.     options.pageLength = 25;
  30.     options.oLanguage = { "sSearch": "" };
  31.     options.responsive = true;
  32.     options.colReorder = true;
  33.     options.stateSave = true;
  34.     options.keys = true;
  35.     options.order = [[ 1, 'asc' ]];
  36.     options.lengthMenu = [[10, 25, 50, -1], [10, 25, 50, "All"]];
  37.  
  38.     var buttons = [
  39.         {
  40.             text: name ? 'Add New ' + name : 'Add New',
  41.             className: 'table-command table-add',
  42.             ctrlKey: true,
  43.             key: 'a',
  44.             action: function()
  45.             {
  46.                 var form = document.forms.action_form;
  47.  
  48.                 if( document.forms.action_form )
  49.                 {
  50.                     form.task.value = 'add';
  51.                     form.submit();
  52.                 }
  53.             }
  54.         },
  55.         {
  56.             extend: 'pdf',
  57.             text: 'PDF',
  58.             className: 'table-export',
  59.             orientation: 'landscape',
  60.             exportOptions: {
  61.                 columns: ':not(.noSort)',
  62.                 orthogonal: 'export'
  63.             }
  64.         },
  65.         {
  66.             extend: 'excel',
  67.             text: 'XLSX',
  68.             className: 'table-export',
  69.             exportOptions: {
  70.                 columns: ':not(.noSort)',
  71.                 orthogonal: 'export'
  72.             }
  73.         },
  74.         {
  75.             extend: 'csv',
  76.             className: 'table-export',
  77.             exportOptions: {
  78.                 columns: ':not(.noSort)',
  79.                 orthogonal: 'export'
  80.             }
  81.         },
  82.         {
  83.             extend: 'print',
  84.             className: 'table-export',
  85.             exportOptions: {
  86.                 columns: ':not(.noSort)',
  87.                 orthogonal: 'export'
  88.             }
  89.         }
  90.     ];
  91.  
  92.     if( additionalCommands )
  93.     {
  94.         buttons.push( additionalCommands );
  95.     }
  96.  
  97.     options.buttons = buttons;
  98.  
  99.     options.initComplete = _dtInitComplete;
  100.  
  101.     var dataTable = $("#datatable");
  102.     var table = dataTable.DataTable( options );
  103.  
  104.     dataTable.thfloat();
  105.  
  106.     return dataTable;
  107. }
  108.  
  109. function _dtCreatedRow(row, data, dataIndex )
  110. {
  111.     $( row ).children('td.inline-edit').on( 'click', function(){inlineEdit( this );} );
  112. }
  113.  
  114. function _dtInitComplete(settings, json) {
  115.  
  116.     //Build the buttons
  117.     //[...]
  118.  
  119.     table.on( 'key-focus', function ( e, datatable, cell ) {
  120.         console.log('key-focus ' + cell.node() );//cell is undefined and therefore this fails
  121.         inlineEdit( cell.node() );
  122.     } );
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement