ekobudiyanto

Untitled

May 18th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 11.83 KB | None | 0 0
  1. "use strict";
  2. var KTDatatablesExtensionButtons = function() {
  3.  
  4.     let datatables = function() {
  5.  
  6.         // begin first table
  7.         var table = $('#kt_datatable1').DataTable({
  8.             responsive: true,
  9.             // Pagination settings
  10.             dom: `<'row'<'col-sm-6 text-left'f><'col-sm-6 text-right'B>>
  11.             <'row'<'col-sm-12'tr>>
  12.             <'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7 dataTables_pager'lp>>`,
  13.  
  14.             buttons: [
  15.                 'print',
  16.                 'copyHtml5',
  17.                 'excelHtml5',
  18.                 'csvHtml5',
  19.                 'pdfHtml5',
  20.             ],
  21.  
  22.             // Order settings
  23.             order: [[1, 'desc']],
  24.  
  25.             headerCallback: function(thead, data, start, end, display) {
  26.                 thead.getElementsByTagName('th')[0].innerHTML = `
  27.                     <label class="checkbox checkbox-single">
  28.                         <input type="checkbox" value="" class="group-checkable"/>
  29.                         <span></span>
  30.                     </label>`;
  31.             },
  32.  
  33.             columnDefs: [
  34.                 {
  35.                     targets: 0,
  36.                     width: '30px',
  37.                     className: 'dt-left',
  38.                     orderable: false,
  39.                     render: function (data, type, full, meta) {
  40.                         return '<label class="checkbox checkbox-single"> <input type="checkbox" value="' + data +'" class="checkable"/><span></span></label>';
  41.                     },
  42.                 },
  43.                 {
  44.                     targets: 3,
  45.                     width: '75px',
  46.                     render: function(data, type, full, meta) {
  47.                         var status = {
  48.                             0: {'title': 'Inactive', 'class': ' label-light-danger'},
  49.                             1: {'title': 'Active', 'class': ' label-light-primary'},
  50.                         };
  51.                         if (typeof status[data] === 'undefined') {
  52.                             return data;
  53.                         }
  54.                         return '<span class="label label-lg font-weight-bold' + status[data].class + ' label-inline">' + status[data].title + '</span>';
  55.                     },
  56.                 },
  57.                 {
  58.                     targets: -1,
  59.                     title: 'Actions',
  60.                     orderable: false,
  61.                     width: '125px',
  62.                     render: function(data, type, full, meta) {
  63.                         return '\
  64.                             <div class="dropdown dropdown-inline">\
  65.                                 <a href="javascript:;" class="btn btn-sm btn-clean btn-icon mr-2" data-toggle="dropdown">\
  66.                                     <span class="svg-icon svg-icon-md">\
  67.                                         <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24px" height="24px" viewBox="0 0 24 24" version="1.1">\
  68.                                             <g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">\
  69.                                                 <rect x="0" y="0" width="24" height="24"/>\
  70.                                                 <path d="M5,8.6862915 L5,5 L8.6862915,5 L11.5857864,2.10050506 L14.4852814,5 L19,5 L19,9.51471863 L21.4852814,12 L19,14.4852814 L19,19 L14.4852814,19 L11.5857864,21.8994949 L8.6862915,19 L5,19 L5,15.3137085 L1.6862915,12 L5,8.6862915 Z M12,15 C13.6568542,15 15,13.6568542 15,12 C15,10.3431458 13.6568542,9 12,9 C10.3431458,9 9,10.3431458 9,12 C9,13.6568542 10.3431458,15 12,15 Z" fill="#000000"/>\
  71.                                             </g>\
  72.                                         </svg>\
  73.                                     </span>\
  74.                                 </a>\
  75.                                 <div class="dropdown-menu dropdown-menu-sm dropdown-menu-right">\
  76.                                     <ul class="navi flex-column navi-hover py-2">\
  77.                                         <li class="navi-header font-weight-bolder text-uppercase font-size-xs text-primary pb-2">\
  78.                                             Choose an action:\
  79.                                         </li>\
  80.                                         <li class="navi-item">\
  81.                                             <a href="#" class="navi-link">\
  82.                                                 <span class="navi-icon"><i class="la la-print"></i></span>\
  83.                                                 <span class="navi-text">Print</span>\
  84.                                             </a>\
  85.                                         </li>\
  86.                                         <li class="navi-item">\
  87.                                             <a href="#" class="navi-link">\
  88.                                                 <span class="navi-icon"><i class="la la-copy"></i></span>\
  89.                                                 <span class="navi-text">Copy</span>\
  90.                                             </a>\
  91.                                         </li>\
  92.                                         <li class="navi-item">\
  93.                                             <a href="#" class="navi-link">\
  94.                                                 <span class="navi-icon"><i class="la la-file-excel-o"></i></span>\
  95.                                                 <span class="navi-text">Excel</span>\
  96.                                             </a>\
  97.                                         </li>\
  98.                                         <li class="navi-item">\
  99.                                             <a href="#" class="navi-link">\
  100.                                                 <span class="navi-icon"><i class="la la-file-text-o"></i></span>\
  101.                                                 <span class="navi-text">CSV</span>\
  102.                                             </a>\
  103.                                         </li>\
  104.                                         <li class="navi-item">\
  105.                                             <a href="#" class="navi-link">\
  106.                                                 <span class="navi-icon"><i class="la la-file-pdf-o"></i></span>\
  107.                                                 <span class="navi-text">PDF</span>\
  108.                                             </a>\
  109.                                         </li>\
  110.                                     </ul>\
  111.                                 </div>\
  112.                             </div>\
  113.                             <a href="javascript:;" class="btn btn-sm btn-clean btn-icon mr-2 edit-record" title="Edit details">\
  114.                                 <span class="svg-icon svg-icon-md">\
  115.                                     <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24px" height="24px" viewBox="0 0 24 24" version="1.1">\
  116.                                         <g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">\
  117.                                             <rect x="0" y="0" width="24" height="24"/>\
  118.                                             <path d="M8,17.9148182 L8,5.96685884 C8,5.56391781 8.16211443,5.17792052 8.44982609,4.89581508 L10.965708,2.42895648 C11.5426798,1.86322723 12.4640974,1.85620921 13.0496196,2.41308426 L15.5337377,4.77566479 C15.8314604,5.0588212 16,5.45170806 16,5.86258077 L16,17.9148182 C16,18.7432453 15.3284271,19.4148182 14.5,19.4148182 L9.5,19.4148182 C8.67157288,19.4148182 8,18.7432453 8,17.9148182 Z" fill="#000000" fill-rule="nonzero"\ transform="translate(12.000000, 10.707409) rotate(-135.000000) translate(-12.000000, -10.707409) "/>\
  119.                                             <rect fill="#000000" opacity="0.3" x="5" y="20" width="15" height="2" rx="1"/>\
  120.                                         </g>\
  121.                                     </svg>\
  122.                                 </span>\
  123.                             </a>\
  124.                             <a href="javascript:;" class="btn btn-sm btn-clean btn-icon" title="Delete">\
  125.                                 <span class="svg-icon svg-icon-md">\
  126.                                     <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24px" height="24px" viewBox="0 0 24 24" version="1.1">\
  127.                                         <g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">\
  128.                                             <rect x="0" y="0" width="24" height="24"/>\
  129.                                             <path d="M6,8 L6,20.5 C6,21.3284271 6.67157288,22 7.5,22 L16.5,22 C17.3284271,22 18,21.3284271 18,20.5 L18,8 L6,8 Z" fill="#000000" fill-rule="nonzero"/>\
  130.                                             <path d="M14,4.5 L14,4 C14,3.44771525 13.5522847,3 13,3 L11,3 C10.4477153,3 10,3.44771525 10,4 L10,4.5 L5.5,4.5 C5.22385763,4.5 5,4.72385763 5,5 L5,5.5 C5,5.77614237 5.22385763,6 5.5,6 L18.5,6 C18.7761424,6 19,5.77614237 19,5.5 L19,5 C19,4.72385763 18.7761424,4.5 18.5,4.5 L14,4.5 Z" fill="#000000" opacity="0.3"/>\
  131.                                         </g>\
  132.                                     </svg>\
  133.                                 </span>\
  134.                             </a>\
  135.                         ';
  136.                     },
  137.                 },
  138.             ],
  139.         });
  140.  
  141.         table.on('change', '.group-checkable', function() {
  142.             var set = $(this).closest('table').find('td:first-child .checkable');
  143.             var checked = $(this).is(':checked');
  144.  
  145.             $(set).each(function() {
  146.                 if (checked) {
  147.                     $(this).prop('checked', true);
  148.                     $(this).closest('tr').addClass('active');
  149.                 }
  150.                 else {
  151.                     $(this).prop('checked', false);
  152.                     $(this).closest('tr').removeClass('active');
  153.                 }
  154.             });
  155.         });
  156.  
  157.         table.on('change', 'tbody tr .checkbox', function() {
  158.             $(this).parents('tr').toggleClass('active');
  159.         });
  160.  
  161.         $('.edit-record').click(function (e) {
  162.             e.preventDefault();
  163.             let $id = $(this).closest('tr').data('id');
  164.             alert($id)
  165.         })
  166.     };
  167.  
  168.     let crud = function () {
  169.         const form = document.getElementById('add_form');
  170.         const fv = FormValidation.formValidation(
  171.             document.getElementById('add_form'),
  172.  
  173.             {
  174.                 fields: {
  175.                     name: {
  176.                         validators: {
  177.                             notEmpty: {
  178.                                 message: 'Website URL is required'
  179.                             },
  180.                         }
  181.                     },
  182.                     email: {
  183.                         validators: {
  184.                             notEmpty: {
  185.                                 message: 'Email is required'
  186.                             },
  187.                             emailAddress: {
  188.                                 message: 'The value is not a valid email address'
  189.                             },
  190.                             remote: {
  191.                                 data: function() {
  192.                                     return {
  193.                                         user_id: form.querySelector('[name="id"]').value,
  194.                                     };
  195.                                 },
  196.                                 message: 'Email already registered',
  197.                                 method: 'POST',
  198.                                 url: '/admin/users/check-email',
  199.                             }
  200.                         }
  201.                     },
  202.  
  203.                     role: {
  204.                         validators: {
  205.                             notEmpty: {
  206.                                 message: 'Please select an option'
  207.                             }
  208.                         }
  209.                     },
  210.  
  211.                     password: {
  212.                         validators: {
  213.                             stringLength: {
  214.                                 min:12,
  215.                                 message: 'Password must have at least 12 character'
  216.                             },
  217.                             callback: {
  218.                                 message: 'Please specific the framework',
  219.                                 callback: function(input) {
  220.                                     const id = form.querySelector('[name="id"]');
  221.                                     const user_id = id.value ? id.value : '';
  222.  
  223.                                     return (user_id !== '')
  224.                                         // The field is valid if user picks
  225.                                         // a given framework from the list
  226.                                         ? true
  227.                                         // Otherwise, the field value is required
  228.                                         : (input.value !== '');
  229.                                 }
  230.                             }
  231.                         }
  232.                     },
  233.  
  234.                     password_confirmation: {
  235.                         validators: {
  236.                             identical: {
  237.                                 compare: function() {
  238.                                     return form.querySelector('[name="password"]').value;
  239.                                 },
  240.                                 message: 'The password and its confirm are not the same'
  241.                             }
  242.                         }
  243.                     },
  244.                 },
  245.  
  246.                 plugins: {
  247.                     trigger: new FormValidation.plugins.Trigger(),
  248.                     bootstrap: new FormValidation.plugins.Bootstrap(),
  249.                     submitButton: new FormValidation.plugins.SubmitButton(),
  250.                     excluded: new FormValidation.plugins.Excluded({
  251.                         excluded: function(field, ele, eles) {
  252.                             const id = form.querySelector('[name="id"]');
  253.                             const password = form.querySelector('[name="password"]');
  254.                             const password_value = password.value ? password.value : '';
  255.                             const user_id = id.value ? id.value : '';
  256.                             return (field === 'password' && user_id !== '') ;
  257.                         },
  258.                     }),
  259.                 }
  260.             }
  261.         )
  262.  
  263.  
  264.         // Revalidate the confirmation password when changing the password
  265.         form.querySelector('[name="password"]').addEventListener('input', function() {
  266.             fv.revalidateField('password_confirmation');
  267.         });
  268.     }
  269.  
  270.     return {
  271.  
  272.         //main function to initiate the module
  273.         init: function() {
  274.             datatables();
  275.             crud();
  276.         },
  277.     };
  278.  
  279. }();
  280.  
  281. jQuery(document).ready(function() {
  282.     KTDatatablesExtensionButtons.init();
  283. });
Add Comment
Please, Sign In to add comment