Advertisement
kura2yamato

testing dtables

Mar 4th, 2019
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var TableAjax = function () {
  2.     var initPickers = function () {
  3.         // Init date pickers
  4.         $('.date-picker').datepicker({
  5.             rtl: Metronic.isRTL(),
  6.             autoclose: true
  7.         });
  8.     }
  9.  
  10.     var handleRecords = function () {
  11. //sesuaikan
  12.         var grid = $("#datatable_ajax").DataTable({
  13.             "ajax": {
  14.                     'processing': true,
  15.                     'serverSide': true,
  16.                     'url': 'http://localhost:8000/bank/index', // Ajax source
  17.                     'dataSrc': 'data', // Used to tell DataTables where the data array is in the JSON structure
  18.                     'type': 'GET'
  19.                 }
  20.         }
  21.         );
  22.  
  23.         var grid0 = new Datatable();
  24.  
  25.         grid0.init({
  26.             src: $("#datatable_ajax0"),
  27.             onSuccess: function (grid) {
  28.                 // Execute some code after table records loaded
  29.                 ubah(grid);
  30.             },
  31.             onError: function (grid) {
  32.                 // Execute some code on network or other general error  
  33.             },
  34.             onDataLoad: function(grid) {
  35.                 // Execute some code on ajax data load
  36.             },
  37.             loadingMessage: 'Loading...',
  38.             dataTable: {
  39.                 /**
  40.                  *  Here you can define a typical datatable settings from http://datatables.net/usage/options
  41.                  *  uncomment below line("dom" parameter) to fix the dropdown overflow issue in the datatable cells. The default datatable layout
  42.                  *  setup uses scrollable div(table-scrollable) with overflow:auto to enable vertical scroll(see: assets/global/scripts/datatable.js).
  43.                  *  So when dropdowns used the scrollable div should be removed.
  44.                  *  "dom": "<'row'<'col-md-8 col-sm-12'pli><'col-md-4 col-sm-12'<'table-group-actions pull-right'>>r>t<'row'<'col-md-8 col-sm-12'pli><'col-md-4 col-sm-12'>>",
  45.                  */
  46.                
  47.                 "bStateSave": true, // Save datatable state(pagination, sort, etc) in cookie.
  48.                 "lengthMenu": [
  49.                     [10, 20, 50, 100, 150, -1],
  50.                     [10, 20, 50, 100, 150, "All"] // Change per page values here
  51.                 ],
  52.                 "pageLength": 10, // Default record count per page
  53.                 "ajax": {
  54.                     'processing': true,
  55.                     'serverSide': true,
  56.                     'url': 'http://localhost:8000/bank/index', // Ajax source
  57.                     'dataSrc': 'data', // Used to tell DataTables where the data array is in the JSON structure
  58.                     'type': 'GET'
  59.                 },
  60.                 /**
  61.                  * Note that the data parameter format shown here can be used with a simplified DataTables initialisation as data is the default property that
  62.                  * DataTables looks for in the source data object
  63.                  * */
  64.                 "columns" : [
  65.                     {data: 'nama_bank'},
  66.                     {data: 'created_at'},
  67.                     {data: 'updated_at'},
  68.                 ],
  69.                 "columnDefs": [ {
  70.                     "targets": 3,
  71.                     "data": {
  72.                         data: 'id'
  73.                     },
  74.                     'render' : function(data, type, row, meta){
  75.                         return '<button type="button" class="btn btn-md btn-primary" data-id="'+data.id+'" onClick="ganti('+data.id+')" data-toggle="modal" href="#responsive">Edit</button> \
  76.                        <button type="button" class="btn btn-md btn-danger" data-id="'+data.id+'" onClick="return confirm() && hapus('+data.id+')">Delete</button>';
  77.                     }
  78.                 } ]
  79.             }
  80.         });
  81.  
  82.         // Handle group actionsubmit button click
  83.         grid.getTableWrapper().on('click', '.table-group-action-submit', function (e) {
  84.             e.preventDefault();
  85.             var action = $(".table-group-action-input", grid.getTableWrapper());
  86.             if (action.val() != "" && grid.getSelectedRowsCount() > 0) {
  87.                 grid.setAjaxParam("customActionType", "group_action");
  88.                 grid.setAjaxParam("customActionName", action.val());
  89.                 grid.setAjaxParam("id", grid.getSelectedRows());
  90.                 grid.getDataTable().ajax.reload();
  91.                 grid.clearAjaxParams();
  92.             } else if (action.val() == "") {
  93.                 Metronic.alert({
  94.                     type: 'danger',
  95.                     icon: 'warning',
  96.                     message: 'Please select an action',
  97.                     container: grid.getTableWrapper(),
  98.                     place: 'prepend'
  99.                 });
  100.             } else if (grid.getSelectedRowsCount() === 0) {
  101.                 Metronic.alert({
  102.                     type: 'danger',
  103.                     icon: 'warning',
  104.                     message: 'No record selected',
  105.                     container: grid.getTableWrapper(),
  106.                     place: 'prepend'
  107.                 });
  108.             }
  109.         });
  110.     };
  111.  
  112.     return {
  113.         // Main function to initiate the module
  114.         init: function () {
  115.             initPickers();
  116.             handleRecords();
  117.         }
  118.     };
  119. }();
  120.  
  121. // Show Data
  122. function lihat() {
  123.  
  124. }
  125.  
  126. // Delete Data
  127. function hapus(data) {
  128.     var id_delete_val = data;
  129.    
  130.     $.ajax({
  131.         'url': 'http://localhost:8000/bank/delete/'+id_delete_val,
  132.         'type': 'DELETE',
  133.         'data': {id:id_delete_val},
  134.  
  135.         success:function(data) {
  136.             $('#success').html('<div id="prefix_732904980324" class="Metronic-alerts alert alert-success fade in"><button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button>'+data.message+'</div>');
  137.         }
  138.     })
  139. }
  140.  
  141. // Edit Data
  142. function ganti(data) {
  143.     var id_ganti_val = data;
  144.  
  145.     $.ajax({
  146.         'url': 'http://localhost:8000/bank/show/'+id_ganti_val,
  147.         'type': 'GET',
  148.         'data': {id:id_ganti_val},
  149.  
  150.         success:function(data) {
  151.             $('#id_bank_edit').val(data.id);
  152.             $('#nama_bank_edit').val(data.nama_bank)
  153.             $('#bank_nama_title').html(data.nama_bank)
  154.         }
  155.     })
  156. }
  157.  
  158. // Ubah Data
  159. function ubah(grid) {
  160.     var update_id_bank = $('#id_bank_edit').val();
  161.     var update_nama_bank = $('#nama_bank_edit').val();
  162.  
  163.     $.ajax({
  164.         url: 'http://localhost:8000/bank/update/'+update_id_bank,
  165.         type: 'PUT',
  166.         data: {id:update_id_bank, nama_bank:update_nama_bank},
  167.  
  168.         // Success response
  169.         success:function(data) {
  170.             $('#nama_bank').val('');
  171.             $('#success_update').html('<div id="prefix_732904980324" class="Metronic-alerts alert alert-success fade in"><button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button>'+data.message+'</div>');
  172.             grid.draw();
  173.         },
  174.  
  175.         // Error response
  176.         error: function (request, status, error) {
  177.             $('#danger_update').html('<div id="prefix_732904980324" class="Metronic-alerts alert alert-danger fade in"><button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button>'+request.responseJSON.nama_bank+'</div>');
  178.         }
  179.     });
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement