PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/resources/sass/porto/js/examples/examples.datatables.editable.js

https://gitlab.com/laher01/factu40
JavaScript | 255 lines | 192 code | 54 blank | 9 comment | 10 complexity | f3bd1e9e7da231c63ad6b0b61709d48d MD5 | raw file
  1. /*
  2. Name: Tables / Editable - Examples
  3. Written by: Okler Themes - (http://www.okler.net)
  4. Theme Version: 2.1.1
  5. */
  6. (function($) {
  7. 'use strict';
  8. var EditableTable = {
  9. options: {
  10. addButton: '#addToTable',
  11. table: '#datatable-editable',
  12. dialog: {
  13. wrapper: '#dialog',
  14. cancelButton: '#dialogCancel',
  15. confirmButton: '#dialogConfirm',
  16. }
  17. },
  18. initialize: function() {
  19. this
  20. .setVars()
  21. .build()
  22. .events();
  23. },
  24. setVars: function() {
  25. this.$table = $( this.options.table );
  26. this.$addButton = $( this.options.addButton );
  27. // dialog
  28. this.dialog = {};
  29. this.dialog.$wrapper = $( this.options.dialog.wrapper );
  30. this.dialog.$cancel = $( this.options.dialog.cancelButton );
  31. this.dialog.$confirm = $( this.options.dialog.confirmButton );
  32. return this;
  33. },
  34. build: function() {
  35. this.datatable = this.$table.DataTable({
  36. dom: '<"row"<"col-lg-6"l><"col-lg-6"f>><"table-responsive"t>p',
  37. aoColumns: [
  38. null,
  39. null,
  40. null,
  41. { "bSortable": false }
  42. ]
  43. });
  44. window.dt = this.datatable;
  45. return this;
  46. },
  47. events: function() {
  48. var _self = this;
  49. this.$table
  50. .on('click', 'a.save-row', function( e ) {
  51. e.preventDefault();
  52. _self.rowSave( $(this).closest( 'tr' ) );
  53. })
  54. .on('click', 'a.cancel-row', function( e ) {
  55. e.preventDefault();
  56. _self.rowCancel( $(this).closest( 'tr' ) );
  57. })
  58. .on('click', 'a.edit-row', function( e ) {
  59. e.preventDefault();
  60. _self.rowEdit( $(this).closest( 'tr' ) );
  61. })
  62. .on( 'click', 'a.remove-row', function( e ) {
  63. e.preventDefault();
  64. var $row = $(this).closest( 'tr' ),
  65. itemId = $row.attr('data-item-id');
  66. $.magnificPopup.open({
  67. items: {
  68. src: _self.options.dialog.wrapper,
  69. type: 'inline'
  70. },
  71. preloader: false,
  72. modal: true,
  73. callbacks: {
  74. change: function() {
  75. _self.dialog.$confirm.on( 'click', function( e ) {
  76. e.preventDefault();
  77. $.ajax({
  78. url: '/',
  79. method: 'GET',
  80. data: {
  81. id: itemId
  82. },
  83. success: function() {
  84. _self.rowRemove( $row );
  85. }
  86. });
  87. $.magnificPopup.close();
  88. });
  89. },
  90. close: function() {
  91. _self.dialog.$confirm.off( 'click' );
  92. }
  93. }
  94. });
  95. });
  96. this.$addButton.on( 'click', function(e) {
  97. e.preventDefault();
  98. _self.rowAdd();
  99. });
  100. this.dialog.$cancel.on( 'click', function( e ) {
  101. e.preventDefault();
  102. $.magnificPopup.close();
  103. });
  104. return this;
  105. },
  106. // ==========================================================================================
  107. // ROW FUNCTIONS
  108. // ==========================================================================================
  109. rowAdd: function() {
  110. this.$addButton.attr({ 'disabled': 'disabled' });
  111. var actions,
  112. data,
  113. $row;
  114. actions = [
  115. '<a href="#" class="hidden on-editing save-row"><i class="fas fa-save"></i></a>',
  116. '<a href="#" class="hidden on-editing cancel-row"><i class="fas fa-times"></i></a>',
  117. '<a href="#" class="on-default edit-row"><i class="fas fa-pencil-alt"></i></a>',
  118. '<a href="#" class="on-default remove-row"><i class="far fa-trash-alt"></i></a>'
  119. ].join(' ');
  120. data = this.datatable.row.add([ '', '', '', actions ]);
  121. $row = this.datatable.row( data[0] ).nodes().to$();
  122. $row
  123. .addClass( 'adding' )
  124. .find( 'td:last' )
  125. .addClass( 'actions' );
  126. this.rowEdit( $row );
  127. this.datatable.order([0,'asc']).draw(); // always show fields
  128. },
  129. rowCancel: function( $row ) {
  130. var _self = this,
  131. $actions,
  132. i,
  133. data;
  134. if ( $row.hasClass('adding') ) {
  135. this.rowRemove( $row );
  136. } else {
  137. data = this.datatable.row( $row.get(0) ).data();
  138. this.datatable.row( $row.get(0) ).data( data );
  139. $actions = $row.find('td.actions');
  140. if ( $actions.get(0) ) {
  141. this.rowSetActionsDefault( $row );
  142. }
  143. this.datatable.draw();
  144. }
  145. },
  146. rowEdit: function( $row ) {
  147. var _self = this,
  148. data;
  149. data = this.datatable.row( $row.get(0) ).data();
  150. $row.children( 'td' ).each(function( i ) {
  151. var $this = $( this );
  152. if ( $this.hasClass('actions') ) {
  153. _self.rowSetActionsEditing( $row );
  154. } else {
  155. $this.html( '<input type="text" class="form-control input-block" value="' + data[i] + '"/>' );
  156. }
  157. });
  158. },
  159. rowSave: function( $row ) {
  160. var _self = this,
  161. $actions,
  162. values = [];
  163. if ( $row.hasClass( 'adding' ) ) {
  164. this.$addButton.removeAttr( 'disabled' );
  165. $row.removeClass( 'adding' );
  166. }
  167. values = $row.find('td').map(function() {
  168. var $this = $(this);
  169. if ( $this.hasClass('actions') ) {
  170. _self.rowSetActionsDefault( $row );
  171. return _self.datatable.cell( this ).data();
  172. } else {
  173. return $.trim( $this.find('input').val() );
  174. }
  175. });
  176. this.datatable.row( $row.get(0) ).data( values );
  177. $actions = $row.find('td.actions');
  178. if ( $actions.get(0) ) {
  179. this.rowSetActionsDefault( $row );
  180. }
  181. this.datatable.draw();
  182. },
  183. rowRemove: function( $row ) {
  184. if ( $row.hasClass('adding') ) {
  185. this.$addButton.removeAttr( 'disabled' );
  186. }
  187. this.datatable.row( $row.get(0) ).remove().draw();
  188. },
  189. rowSetActionsEditing: function( $row ) {
  190. $row.find( '.on-editing' ).removeClass( 'hidden' );
  191. $row.find( '.on-default' ).addClass( 'hidden' );
  192. },
  193. rowSetActionsDefault: function( $row ) {
  194. $row.find( '.on-editing' ).addClass( 'hidden' );
  195. $row.find( '.on-default' ).removeClass( 'hidden' );
  196. }
  197. };
  198. $(function() {
  199. EditableTable.initialize();
  200. });
  201. }).apply(this, [jQuery]);