Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.75 KB | None | 0 0
  1. $(document).ready(function() {
  2.  
  3. $.extend( jQuery.fn.dataTableExt.oSort, {
  4. "date-uk-pre": function (a){
  5. return parseInt(moment(a, "DD/MM/YYYY").format("X"), 10);
  6. },
  7. "date-uk-asc": function (a, b) {
  8. return a - b;
  9. },
  10. "date-uk-desc": function (a, b) {
  11. return b - a;
  12. }
  13. });
  14.  
  15.  
  16.  
  17. var edit_date_col_num = $('th:contains("Edit Date")').index();
  18. var entry_date_col_num = $('th:contains("Entry Date")').index();
  19.  
  20. var table = $('.mainTable').DataTable( {
  21.  
  22. pageLength: 20,
  23. colReorder: true,
  24. stateSave: true,
  25. stateDuration: 0,
  26.  
  27. columnDefs: [
  28. { "type": "date-uk", targets: [ edit_date_col_num, entry_date_col_num ] }
  29. ],
  30.  
  31. dom: 'Blfrtip',
  32. buttons: [
  33. 'copy', 'csv', 'excel', 'print',
  34. {
  35. extend: 'colvis',
  36. collectionLayout: 'fixed four-column',
  37. postfixButtons: [ 'colvisRestore' ]
  38. }
  39. ],
  40.  
  41. language: {
  42. "emptyTable": "No results match your search",
  43. "zeroRecords": "No results found",
  44. //"processing": "<div align='center'><img src='/static/ajax-loader.gif'></div>",
  45. //"loadingRecords": "<div align='center'><img src='/static/ajax-loader.gif'></div>",
  46. },
  47.  
  48. processing: true,
  49. serverSide: true,
  50. ajax: {
  51. "url": EE.BASE + "&C=addons_modules&M=show_module_cp&module=ion&method=ajax_results",
  52. "type": "POST"
  53. },
  54. columns: [
  55. <?php
  56. foreach($columns AS $key => $val)
  57. {
  58. $visible_cols = array('field_id_325', 'field_id_326', 'field_id_697', 'field_id_649', 'field_id_689', 'field_id_632', 'entry_date', 'edit_date', 'field_id_698', 'field_id_666');
  59.  
  60. $visible = 'false';
  61. if(in_array($key,$visible_cols))
  62. {
  63. $visible = 'true';
  64. }
  65. echo '{';
  66. echo ' "visible": '.$visible.',';
  67. echo ' "data": "'.$key.'",';
  68. echo ' "class": "'.$key.'",';
  69. echo '},';
  70. }
  71. ?>
  72. ],
  73. <?php
  74. $id = $this->input->get('id');
  75. $action = $this->input->get('action');
  76. if(isset($action) && $action == 'load' && isset($id) && $id != '') :
  77. ?>
  78. "stateLoadCallback": function (settings) {
  79. var o;
  80.  
  81. // Send an Ajax request to the server to get the data. Note that
  82. // this is a synchronous request since the data is expected back from the
  83. // function
  84. $.ajax( {
  85. "url": EE.BASE + "&C=addons_modules&M=show_module_cp&module=ion&method=state_save&action=load&id=<?php echo $id;?>",
  86. "async": false,
  87. "dataType": "json",
  88. "success": function (response) {
  89. response = JSON.parse(response);
  90. //console.log(response);
  91. o = response;
  92. }
  93. });
  94.  
  95. return o;
  96. },
  97. <?php
  98. endif;
  99. ?>
  100.  
  101. initComplete: function (settings) {
  102. this.api().columns().every( function () {
  103. var column = this;
  104.  
  105. // Get the column number
  106. var col_num = column[0][0];
  107.  
  108. // Do not do a select for date fields
  109. if(col_num ==edit_date_col_num || col_num ==entry_date_col_num)
  110. {
  111. var date_range = $('from <input name="edit_date_min" id="edit_date_min"/>')
  112. .appendTo( $(column.footer()).empty() )
  113. .on( 'keyup', function () {
  114. var val = $.fn.dataTable.util.escapeRegex(
  115. $(this).val()
  116. );
  117.  
  118. column
  119. .search( val ? ''+val+'' : '', true, false )
  120. .draw();
  121. } );
  122. }
  123. else
  124. {
  125. var select = $('<select><option value=""></option></select>')
  126. .appendTo( $(column.footer()).empty() )
  127. .on( 'change', function () {
  128. var val = $.fn.dataTable.util.escapeRegex(
  129. $(this).val()
  130. );
  131.  
  132. column
  133. .search( val ? '^'+val+'$' : '', true, false )
  134. .draw();
  135. } );
  136.  
  137. column.data().unique().sort().each( function ( d, j ) {
  138. select.append( '<option value="'+d+'">'+d+'</option>' )
  139. } );
  140.  
  141. }
  142. } );
  143.  
  144. // Need to re-apply the selection to the select dropdowns
  145. var colsSearch = settings.aoPreSearchCols;
  146. var cols = settings.aoColumns;
  147.  
  148. // Iterate through all cols to assign value to select/input fields
  149. for (var i = 0; i < cols.length; i++)
  150. {
  151. if(cols[i].bVisible == true)
  152. {
  153. var cell_class = cols[i].class;
  154.  
  155. var value = colsSearch[i].sSearch;
  156.  
  157. if(value!='')
  158. {
  159. value = value.replace("^", "").replace("$","").replace("\/", "/").replace("\/", "/");
  160. $("."+cell_class).find(":input").val(value);
  161. }
  162.  
  163. }
  164. }
  165.  
  166. },
  167.  
  168. } );
  169.  
  170.  
  171. // Save a datatables state by clicking the save button
  172. $( ".save_state" ).click(function(e) {
  173. e.preventDefault();
  174.  
  175. table.destroy();
  176. $('.mainTable').DataTable( {
  177.  
  178. colReorder: true,
  179. stateSave: true,
  180. stateDuration: 0,
  181.  
  182. "stateSaveCallback": function (settings, data) {
  183.  
  184. var save_name = $('.save_name').val();
  185.  
  186. // Send an Ajax request to the server with the state object
  187. $.ajax( {
  188. "url": EE.BASE + "&C=addons_modules&M=show_module_cp&module=ion&method=state_save&action=save&save_name="+save_name,
  189. "data": data,
  190. "dataType": "json",
  191. "type": "POST",
  192. "success": function (response)
  193. {
  194. //console.log(response);
  195. }
  196. } );
  197.  
  198. },
  199.  
  200.  
  201. });
  202.  
  203.  
  204. //table.state.save();
  205. window.location.replace(EE.BASE + "&C=addons_modules&M=show_module_cp&module=ion&method=applications");
  206. });
  207.  
  208.  
  209. $( ".clear_state" ).click(function(e) {
  210. e.preventDefault();
  211. table.state.clear();
  212. window.location.replace(EE.BASE + "&C=addons_modules&M=show_module_cp&module=ion&method=applications");
  213. });
  214.  
  215.  
  216. $.fn.dataTableExt.afnFiltering.push(
  217. function( oSettings, aData, iDataIndex ) {
  218. var iFini = document.getElementById('edit_date_min').value;
  219. var iStartDateCol = edit_date_col_num;
  220.  
  221. iFini=iFini.substring(6,10) + iFini.substring(3,5)+ iFini.substring(0,2);
  222.  
  223. var datofini=aData[iStartDateCol].substring(6,10) + aData[iStartDateCol].substring(3,5)+ aData[iStartDateCol].substring(0,2);
  224.  
  225. if ( iFini === "" )
  226. {
  227. return true;
  228. }
  229. else if ( iFini <= datofini)
  230. {
  231. return true;
  232. }
  233. return false;
  234. console.log("Search: "+iFini);
  235. console.log("Date: "+datofini);
  236. }
  237. );
  238.  
  239.  
  240. // Event listener to the two range filtering inputs to redraw on input
  241. $('#edit_date_min, #edit_date_max').keyup( function() {
  242. table.draw();
  243. } );
  244.  
  245.  
  246. } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement