-1

I was trying to implement this guide from this page: How to update an HTML table content without refreshing the page?

I somehow managed to implement it, unfortunately the Client-Side dataTable is destroyed when refreshing it.

By destroyed, all the data in the dataTable is in a single page.

This is the table.

<table id="earnings_amendment_account" class="table table-bordered" style="table-layout: fixed; display: none">
                <thead>
                <th></th>
                  <th>Account Code</th>
                  <th>Account Title</th>
                  <th>Account Type</th>
                  <th>Tools</th>
                </thead>
<tbody id="table-body">
 <?php include 'tableBody.php';?>
                </tbody>
              </table>

This is the tableBody.php

<?php include 'backend/conn.php'; ?>                 
                 <?php
                    $params=array();
                    $sql = "SELECT accountcode,accounttype,accounttitle,accounttype,postedby,dateposted,approvedby,dateapproved FROM earningsamendmentaccount";
$query = sqlsrv_query($conn, $sql, $params, array("Scrollable" => SQLSRV_CURSOR_KEYSET));
if ($query === false ) { echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true); exit; } 

                    while($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)){

                      echo "
                      <tr data-key-1='".$row['postedby']."' data-key-2='".$row['dateposted']."' data-key-3='".$row['approvedby']."' data-key-4='".$row['dateapproved']."'>
                        <td class='details-control'></td>
                        <td>".$row['accountcode']."</td>
                          <td>".$row['accounttitle']."</td>
                          <td>".$row['accounttype']."</td>
                          <td>
                          <button class='btn btn-default btn-sm view btn-flat' data-id='".$row['accountcode']."'><i class='fa fa-eye'></i> View</button>
                            <button class='btn btn-success btn-sm edit btn-flat' data-id='".$row['accountcode']."'><i class='fa fa-edit'></i> Edit</button>
                            <button class='btn btn-danger btn-sm delete btn-flat' data-id='".$row['accountcode']."'><i class='fa fa-trash'></i> Delete</button>
                          " ?>
                            <?php if (empty($row['approvedby'])) { echo " <button class='btn btn-warning btn-sm approve btn-flat' data-id='".$row['accountcode']."'><i class='fa fa-check-square-o'></i> Approve</button> "; } ?>

                            <?php "
                        </tr>
                      ";
                    }
                  ?>

This is my jQuery/AJAX - (earnings_amendment_account.php)

function SubmitFormData() {
    var add = $("#add").val();
    var add_accountcode = $("#add_accountcode").val();
    var accounttitle = $("#accounttitle").val();
    var accounttype = $("#accounttype").val();
    var postedby = $("#postedby").val();
    var dateposted = $("#dateposted").val();
    $.post("earnings_amendment_account_add.php", 
    { 
    add: add,
    add_accountcode: add_accountcode,
    accounttitle: accounttitle,
    accounttype: accounttype,
    postedby: postedby,
    dateposted: dateposted
    },
       function(data) {
         $('#results').html(data);
         $('#myForm')[0].reset();
         $.get("tableBody.php", function(data) {
         $("#table-body").html(data);
         $("#earnings_amendment_account").DataTable().ajax.reload();
        });
    });
}

This is the modal being called.

<form autocomplete='off' id="myForm" class="form-horizontal" method="POST" action="earnings_amendment_account_add.php">

<!-- Table Loader -->
<div class="form-group" id="earnings_amendment_account_modalload">
<div class="col-sm-9" id= "earnings_amendment_account_modalload" style="width:100%">
</div></div>

                <div class="form-group">
                    <label for="accounttitle" class="col-sm-3 control-label">Account Title</label>

                    <div class="col-sm-9">
                        <input type="text" autocomplete="off" class="form-control" id="accounttitle" name="accounttitle" style="text-transform:uppercase;width:90%" required>
                    </div>
                </div>
                <div class="form-group" hidden>
                    <label for="postedby" class="col-sm-3 control-label">Posted By</label>

                    <div class="col-sm-9">
                        <input type="text" autocomplete="off" class="form-control" id="postedby" name="postedby" value="<?php echo $user['firstname'].' '.$user['lastname']; ?>" required>
                    </div>
                </div>
                <div class="form-group" hidden>
                    <label for="dateposted" class="col-sm-3 control-label">Posted By</label>

                    <div class="col-sm-9">
                        <input type="text" autocomplete="off" class="form-control" id="dateposted" name="dateposted" value="<?php echo gmdate('Y-m-d h:i:s'); ?>" required>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default info btn-flat pull-left" data-dismiss="modal"><i class="fa fa-close"></i> Close</button>
                <button type="button" onclick="SubmitFormData();" class="btn btn-primary btn-flat" name="add"><i class="fa fa-save"></i> Save</button>
                <div id="results"></div>
                </form>

This is the dataTable.

function format ( dataSource ) {
    var html = '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;" class="table table-bordered">';
    for (var key in dataSource){
        html += '<tr>'+
                   '<td>' + key             +'</td>'+
                   '<td>' + dataSource[key] +'</td>'+
                '</tr>';
    } return html += '</table>';  }
var earnings_amendment_account_table = $('#earnings_amendment_account').DataTable({});

      // Add event listener for opening and closing details
      $('#earnings_amendment_account').on('click', 'td.details-control', function () {
          var tr = $(this).closest('tr');
          var row = earnings_amendment_account_table.row(tr);

          if (row.child.isShown()) {
              // This row is already open - close it
              row.child.hide();
              tr.removeClass('shown');
          } else {
              // Open this row
              row.child(format({
                  'Posted By : ' : tr.data('key-1'),
                  'Date Posted : ' : tr.data('key-2'),
                  'Approved By : ' :  tr.data('key-3'),
                  'Date Approved : ' :  tr.data('key-4')
              })).show();
              tr.addClass('shown');
          } });

Is there a way for this to be fixed?

pjustindaryll
  • 377
  • 3
  • 14

1 Answers1

1

try use your datatable object var earnings_amendment_account_table = $('#earnings_amendment_account').DataTable({});

so on your jQuery/AJAX - (earnings_amendment_account.php) change $("#earnings_amendment_account").DataTable().ajax.reload(); to earnings_amendment_account_table.ajax.reload();

anon
  • 361
  • 4
  • 8