php 如何在不刷新页面的情况下重新加载Datatable?

o75abkj4  于 2022-11-21  发布在  PHP
关注(0)|答案(1)|浏览(212)

对不起,我的英语是周。请告诉我的过程,以reload的数据没有刷新页面使用datatable jquery。我的重载表不工作。
我看法是:

<table id="table_id" class="table table-striped table-hover table-bordered" cellspacing="0" width="100%">
        <thead>
            <tr style="background-color:#1c2d3f;color:white;">
                <th>Kategori <span style="padding-top:5px;font-size:12px;" class="hidden-sm hidden-xs pull-right glyphicon glyphicon-sort"></span></th>
                <th>Deskripsi <span style="padding-top:5px;font-size:12px;" class="hidden-sm hidden-xs pull-right glyphicon glyphicon-sort"></span></th>
                <th style="width:200px;">Tipe Kategori <span style="padding-top:5px;font-size:12px;" class="hidden-sm hidden-xs pull-right glyphicon glyphicon-sort"></span></th>
                <th>Slug <span style="padding-top:5px;font-size:12px;" class="hidden-sm hidden-xs pull-right glyphicon glyphicon-sort"></span></th>
                <th>Aktif <span style="padding-top:5px;font-size:12px;" class="hidden-sm hidden-xs pull-right glyphicon glyphicon-sort"></span></th>
                <th>Opsi</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach($data_kategori as $ambil_data){?>
           <tr>
              <td style="background-color:white;"><?php echo $ambil_data->kategori;?></td>
              <td style="background-color:white;"><?php echo $ambil_data->deskripsi;?></td>
              <td style="padding-top:15px;background-color:white;"><?php 
              if($ambil_data->parent_kategori == "0"){
                echo "<label class='label label-primary'>Kategori Utama</label>";
                }else{
                echo "<label class='label label-default'>Anak kategori</label>";
                  }?>
              </td>
              <td style="background-color:white;"><?php echo $ambil_data->slug;?></td>
              <td style="padding-top:15px;background-color:white;"><?php 
              if($ambil_data->aktif == "Y"){
                echo "<label class='label label-success'>Aktif</label>";
                }elseif($ambil_data->aktif == "N"){
                echo "<label class='label label-danger'>Tidak aktif</label>";
                  }?>
              </td>
              <td style="background-color:white;">
              <a href="javascript:void(0)" class="btn btn-warning edit" onclick="edit(<?php echo $ambil_data->kat_id;?>)"><i class="glyphicon glyphicon-pencil"></i></a>
              <a href="javascript:void(0)" class="btn btn-danger hapus" onclick="delete_book(<?php echo $ambil_data->kat_id;?>)"><i class="glyphicon glyphicon-remove"></i></a>
              </td>
          </tr>
         <?php }?>
        </tbody>

在插入数据时,这是我的jquery:

function reload_table(){
  table.ajax.reload(null,false); //reload datatable ajax 
}

function save()
{
  var url;
  if(save_method == 'add')
  {   
      url = "<?php echo site_url('admin/kategori/proses_tambah_kat  ')?>";
  }
  else
  {
    url = "<?php echo site_url('index.php/book/book_update')?>";
  }

   // ajax adding data to database
      $.ajax({
        url : url,
        type: "POST",
        data: $('#form').serialize(),
        dataType: "JSON",
        success: function(data)
        {
           //if success close modal and reload ajax table
           $('#modal_form').modal('hide');

          reload_table();

        },
        error: function (jqXHR, textStatus, errorThrown)
        {
            alert('Error adding / update data');
        }
    });

}
有什么地方我做错了吗?

fcg9iug3

fcg9iug31#

可以按id重新加载Datatable,如下所示

$('#table_id').DataTable().ajax.reload(null, false);

TableVar.ajax.reload();

如果您使用 AJAX 来获取Datatable的数据,它将工作。有关更多信息,请参阅此link

相关问题