Advertisement
Guest User

js

a guest
Jun 19th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Name:           Tables / Editable - Examples
  3. Written by:     Okler Themes - (http://www.okler.net)
  4. Theme Version:  2.2.0
  5. */
  6.  
  7. (function($) {
  8.  
  9.     'use strict';
  10.  
  11.     var EditableTable = {
  12.  
  13.         options: {
  14.             addButton: '#addToTable',
  15.             table: '#datatable-editable',
  16.             dialog: {
  17.                 wrapper: '#dialog',
  18.                 cancelButton: '#dialogCancel',
  19.                 confirmButton: '#dialogConfirm',
  20.             }
  21.         },
  22.  
  23.         initialize: function() {
  24.             this
  25.                 .setVars()
  26.                 .build()
  27.                 .events();
  28.         },
  29.  
  30.         setVars: function() {
  31.             this.$table             = $( this.options.table );
  32.             this.$addButton         = $( this.options.addButton );
  33.  
  34.             // dialog
  35.             this.dialog             = {};
  36.             this.dialog.$wrapper    = $( this.options.dialog.wrapper );
  37.             this.dialog.$cancel     = $( this.options.dialog.cancelButton );
  38.             this.dialog.$confirm    = $( this.options.dialog.confirmButton );
  39.  
  40.             return this;
  41.         },
  42.  
  43.         build: function() {
  44.             this.datatable = this.$table.DataTable({
  45.                 dom: '<"row"<"col-lg-6"l><"col-lg-6"f>><"table-responsive"t>p',
  46.                 aoColumns: [
  47.                     null,
  48.                     null,
  49.                     null,
  50.                     null,
  51.                     null,
  52.                     null,
  53.                     null,
  54.                     null,
  55.                     { "bSortable": false }
  56.                 ]
  57.             });
  58.  
  59.             window.dt = this.datatable;
  60.  
  61.             return this;
  62.         },
  63.  
  64.         events: function() {
  65.             var _self = this;
  66.  
  67.             this.$table
  68.                 .on('click', 'a.save-row', function( e ) {
  69.                     e.preventDefault();
  70.  
  71.                     _self.rowSave( $(this).closest( 'tr' ) );
  72.                 })
  73.                 .on('click', 'a.cancel-row', function( e ) {
  74.                     e.preventDefault();
  75.  
  76.                     _self.rowCancel( $(this).closest( 'tr' ) );
  77.                 })
  78.                 .on('click', 'a.edit-row', function( e ) {
  79.                     e.preventDefault();
  80.  
  81.                     _self.rowEdit( $(this).closest( 'tr' ) );
  82.                 })
  83.                 .on( 'click', 'a.remove-row', function( e ) {
  84.                     e.preventDefault();
  85.  
  86.                     var $row = $(this).closest( 'tr' ),
  87.                         itemId = $row.attr('data-item-id');
  88.  
  89.                     $.magnificPopup.open({
  90.                         items: {
  91.                             src: _self.options.dialog.wrapper,
  92.                             type: 'inline'
  93.                         },
  94.                         preloader: false,
  95.                         modal: true,
  96.                         callbacks: {
  97.                             change: function() {
  98.                                 _self.dialog.$confirm.on( 'click', function( e ) {
  99.                                     e.preventDefault();
  100.  
  101.                                     $.ajax({
  102.                                         url: '/',
  103.                                         method: 'GET',
  104.                                         data: {
  105.                                             id: itemId
  106.                                         },
  107.                                         success: function() {
  108.                                             _self.rowRemove( $row );
  109.                                         }
  110.                                     });
  111.  
  112.                                     $.magnificPopup.close();
  113.                                 });
  114.                             },
  115.                             close: function() {
  116.                                 _self.dialog.$confirm.off( 'click' );
  117.                             }
  118.                         }
  119.                     });
  120.                 });
  121.  
  122.             this.$addButton.on( 'click', function(e) {
  123.                 e.preventDefault();
  124.  
  125.                 _self.rowAdd();
  126.             });
  127.  
  128.             this.dialog.$cancel.on( 'click', function( e ) {
  129.                 e.preventDefault();
  130.                 $.magnificPopup.close();
  131.             });
  132.  
  133.             return this;
  134.         },
  135.  
  136.         // ==========================================================================================
  137.         // ROW FUNCTIONS
  138.         // ==========================================================================================
  139.         rowAdd: function() {
  140.             this.$addButton.attr({ 'disabled': 'disabled' });
  141.  
  142.             var actions,
  143.                 data,
  144.                 $row;
  145.  
  146.             actions = [
  147.                 '<a href="#" class="hidden on-editing save-row"><i class="fas fa-save"></i></a>',
  148.                 '<a href="#" class="hidden on-editing cancel-row"><i class="fas fa-times"></i></a>',
  149.                 '<a href="#" class="on-default edit-row"><i class="fas fa-pencil-alt"></i></a>',
  150.                 '<a href="#" class="on-default remove-row"><i class="far fa-trash-alt"></i></a>'
  151.             ].join(' ');
  152.  
  153.             data = this.datatable.row.add([ '', '', '', '', '', '', '', '', actions ]);
  154.             $row = this.datatable.row( data[0] ).nodes().to$();
  155.  
  156.             $row
  157.                 .addClass( 'adding' )
  158.                 .find( 'td:last' )
  159.                 .addClass( 'actions' );
  160.  
  161.             this.rowEdit( $row );
  162.  
  163.             this.datatable.order([0,'asc']).draw(); // always show fields
  164.         },
  165.  
  166.         rowCancel: function( $row ) {
  167.             var _self = this,
  168.                 $actions,
  169.                 i,
  170.                 data;
  171.  
  172.             if ( $row.hasClass('adding') ) {
  173.                 this.rowRemove( $row );
  174.             } else {
  175.  
  176.                 data = this.datatable.row( $row.get(0) ).data();
  177.                 this.datatable.row( $row.get(0) ).data( data );
  178.  
  179.                 $actions = $row.find('td.actions');
  180.                 if ( $actions.get(0) ) {
  181.                     this.rowSetActionsDefault( $row );
  182.                 }
  183.  
  184.                 this.datatable.draw();
  185.             }
  186.         },
  187.  
  188.         rowEdit: function( $row ) {
  189.             var _self = this,
  190.                 data;
  191.  
  192.             data = this.datatable.row( $row.get(0) ).data();
  193.  
  194.             $row.children( 'td' ).each(function( i ) {
  195.                 var $this = $( this );
  196.  
  197.                 if ( $this.hasClass('actions') ) {
  198.                     _self.rowSetActionsEditing( $row );
  199.                 } else if( $this.hasClass('sorting_1') ) {
  200.                     if( $this.hasClass('sorting_1-check-icon') ) {
  201.                         $this.html( '<i class="fas fa-check"></i>' );
  202.                     }
  203.                     if( $this.hasClass('sorting_1-lock-icon') ) {
  204.                         $this.html( '<i class="fas fa-lock"></i>' );
  205.                     }
  206.                 } else {
  207.  
  208.                     if( $row.hasClass( 'adding' ) && $this.is(':first-child') ) {
  209.                         $this.html( '<i class="fas fa-plus"></i>' );
  210.                     } else {
  211.                         $this.html( '<input type="text" class="form-control input-block" value="' + data[i] + '"/>' );
  212.                     }
  213.                 }
  214.             });
  215.         },
  216.  
  217.         rowSave: function( $row ) {
  218.             var _self     = this,
  219.                 $actions,
  220.                 values    = [];
  221.  
  222.             if ( $row.hasClass( 'adding' ) ) {
  223.                 this.$addButton.removeAttr( 'disabled' );
  224.                 $row.removeClass( 'adding' );
  225.             }
  226.  
  227.             values = $row.find('td').map(function() {
  228.                 var $this = $(this);
  229.  
  230.                 if ( $this.hasClass('actions') ) {
  231.                     _self.rowSetActionsDefault( $row );
  232.                     return _self.datatable.cell( this ).data();
  233.                 } else if( $this.hasClass('sorting_1') ) {
  234.                     if( $this.hasClass('sorting_1-check-icon') ) {
  235.                         return $.trim( '<i class="fas fa-check"></i>' );
  236.                     } else if( $this.hasClass('sorting_1-lock-icon') ) {
  237.                         return $.trim( '<i class="fas fa-lock"></i>' );
  238.                     } else {
  239.                         if( !$row.attr('data-item-id') ) {
  240.                             return $.trim( '<i class="fas fa-plus"></i>' );
  241.                         } else {
  242.                             return $.trim( $this.find('input').val() );
  243.                         }
  244.                     }
  245.                 } else {
  246.                     return $.trim( $this.find('input').val() );
  247.                 }
  248.             });
  249.  
  250.             this.datatable.row( $row.get(0) ).data( values );
  251.  
  252.             $actions = $row.find('td.actions');
  253.             if ( $actions.get(0) ) {
  254.                 this.rowSetActionsDefault( $row );
  255.             }
  256.  
  257.             this.datatable.draw();
  258.         },
  259.  
  260.         rowRemove: function( $row ) {
  261.             if ( $row.hasClass('adding') ) {
  262.                 this.$addButton.removeAttr( 'disabled' );
  263.             }
  264.  
  265.             this.datatable.row( $row.get(0) ).remove().draw();
  266.         },
  267.  
  268.         rowSetActionsEditing: function( $row ) {
  269.             $row.find( '.on-editing' ).removeClass( 'hidden' );
  270.             $row.find( '.on-default' ).addClass( 'hidden' );
  271.         },
  272.  
  273.         rowSetActionsDefault: function( $row ) {
  274.             $row.find( '.on-editing' ).addClass( 'hidden' );
  275.             $row.find( '.on-default' ).removeClass( 'hidden' );
  276.         }
  277.  
  278.     };
  279.  
  280.     $(function() {
  281.         EditableTable.initialize();
  282.     });
  283.  
  284. }).apply(this, [jQuery]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement