Advertisement
Virajsinh

DataTable Edit Cell On Click inline in JavaScript

Mar 8th, 2024 (edited)
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 5.72 KB | Source Code | 0 0
  1. // https://jsfiddle.net/jdlubrano/r6wq8v7y/13/
  2.  
  3. //<table id="datatable" class="display">
  4. //     <thead>
  5. //         <tr>
  6. //             <th>Root Cause</th>
  7. //             <th>Description</th>
  8. //             <th>&nbsp;</th>
  9. //         </tr>
  10. //     </thead>
  11. //     <tfoot>
  12. //         <tr>
  13. //             <th>Root Cause</th>
  14. //             <th>Description</th>
  15. //         </tr>
  16. //     </tfoot>
  17. // </table>
  18.  
  19. <table id="datatable" class="display">
  20.     <thead>
  21.         <tr>
  22.             <th>Root Cause</th>
  23.             <th>Description</th>
  24.             <th>&nbsp;</th>
  25.         </tr>
  26.     </thead>
  27.     <tfoot>
  28.         <tr>
  29.             <th>Root Cause</th>
  30.             <th>Description</th>
  31.         </tr>
  32.     </tfoot>
  33. </table>
  34.  
  35. $(document).ready(function() {
  36.     var tableData = [
  37.         {
  38.             "id": 1,
  39.             "name": "RC_1",
  40.             "description": "This is the first root cause"
  41.         },
  42.         {
  43.             "id": 2,
  44.             "name": "RC_2",
  45.             "description": "This is the second root cause"
  46.         }
  47.     ];
  48.     var dataTable = $('#datatable').DataTable({
  49.         "data": tableData,
  50.         "columns": [
  51.             { "data": "name" },
  52.             { "data": "description" },
  53.             {
  54.                 "data": null,
  55.                 "defaultContent": '<button>Remove</button>'
  56.             }
  57.         ]
  58.     });
  59.     $('#datatable').on('click', 'tbody > tr', function() {
  60.         if( !$(this).hasClass('selected') ) {
  61.            $('#datatable > tbody > tr.selected').removeClass('selected')
  62.            $(this).addClass('selected');
  63.         }
  64.     });
  65.     $('#datatable').on('dblclick', 'tbody td', function() {
  66.         var text = dataTable.cell($(this)).data();
  67.         var inputElement = document.createElement('input');
  68.         inputElement.type = "text";
  69.         inputElement.value = text;
  70.         inputElement.className = "editable";
  71.         this.innerHTML = '';
  72.         this.appendChild(inputElement);
  73.         $(inputElement).focus();
  74.     });
  75.     $('#datatable').on('change', '.editable', function() {
  76.         var inputVal = this.value;
  77.         var cell = dataTable.cell($(this).parent('td'));
  78.         var row = dataTable.row($(this).parents('tr'));
  79.         var oldData = cell.data();
  80.         cell.data(inputVal);
  81.         console.log(JSON.stringify(row.data()));
  82.         // Make an ajax call to update table.
  83.         // If the ajax call fails, put the old data back
  84.         // and show a warning.
  85.         dataTable.draw();
  86.     });
  87.     $('#datatable').on('blur', '.editable', function() {
  88.         $(this).parent('td').html(this.value);
  89.         dataTable.draw();
  90.     });
  91. });
  92. // --------------------------------------------------------------
  93.  
  94.     $('#ajaxSalarytable').on('click', 'tbody > tr', function() {
  95.       if( !$(this).hasClass('selected') ) {
  96.        $('#ajaxSalarytable > tbody > tr.selected').removeClass('selected')
  97.        $(this).addClass('selected');
  98.      }
  99.    });
  100.  
  101.     $('#ajaxSalarytable').on('dblclick', 'tbody td', function()
  102.     {
  103.       var check_class = $(this).attr('class');
  104.       if(check_class == 'hold_salary_amount' || check_class == 'hold_remark')
  105.       {
  106.         var text = oTable.cell($(this)).data();
  107.         var inputElement = document.createElement('input');
  108.         inputElement.type = "text";
  109.         inputElement.value = text;
  110.         inputElement.className = "editable";
  111.         this.innerHTML = '';
  112.         this.appendChild(inputElement);
  113.         $(inputElement).focus();
  114.       }
  115.     });
  116.  
  117.     $('#ajaxSalarytable').on('change', '.editable', function()
  118.     {
  119.       var check_class = $(this).parent('td').attr('class');
  120.       if(check_class == 'hold_salary_amount' || check_class == 'hold_remark')
  121.       {
  122.         var inputVal = this.value;
  123.         var cell     = oTable.cell($(this).parent('td'));
  124.         var row      = oTable.row($(this).parents('tr'));
  125.         var oldData  = cell.data();
  126.         var employee_id = row.data().id;
  127.         // console.log('===  index.js [166] ===', inputVal);
  128.         var formData = new FormData();
  129.         formData.append('employee_id', employee_id);
  130.         formData.append(check_class, inputVal);
  131.         $.ajax({
  132.           url: backend_url + '/salary/update',
  133.           type: 'POST',
  134.           data: formData,
  135.           cache: false,
  136.           contentType: false,
  137.           processData: false,
  138.           dataType: 'json',
  139.           beforeSend: function() {
  140.             // Add Loader
  141.           },
  142.           success: function (res) {
  143.             //var res = JSON.parse(data);
  144.             if(res.type == 'success'){
  145.               //Message
  146.               alert(res.message);
  147.               // $('#FORM_NAME').trigger("reset");
  148.             }else{
  149.               //Message
  150.               alert(res.message);
  151.             }
  152.           },
  153.           error: function(xhr) {
  154.             // if error occured
  155.             // console.log(xhr);
  156.           },
  157.           complete: function() {
  158.             // Remove Loader
  159.           },
  160.           statusCode: {
  161.             404: function() {
  162.               // alert("page not found");
  163.             },
  164.             403: function() {
  165.               // alert("Forbidden");
  166.             },
  167.             500: function() {
  168.               // alert("Internal Server Error");
  169.             }
  170.           }
  171.         });
  172.         // cell.data(inputVal);
  173.         // console.log(JSON.stringify(row.data()));
  174.         // Make an ajax call to update table.
  175.         // If the ajax call fails, put the old data back
  176.         // and show a warning.
  177.         oTable.draw();
  178.       }
  179.     });
  180.  
  181.     $('#ajaxSalarytable').on('blur', '.editable', function() {
  182.       $(this).parent('td').html(this.value);
  183.       oTable.draw();
  184.     });
  185. // --------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement