examples.datatables.editable.js 5.1 KB

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