在不使用ajax刷新的情况下重新加载数据表

2021-12-28 00:00:00 jquery php ajax codeigniter datatable

我在一个选项卡中有一个数据表,它是从控制器通过 index 方法发送的数据加载的.

$data = 数组('文件' =>$this->getDocuments(),//更多东西...);$this->load->view($this->config->item('groupViews') .'example/example_edit_view', $data);

我有加载数据表的视图

<表id="t_documents"class="table table-striped table-bordered table-hover"细胞间距=0"宽度="100%">

然后我在加载页面时在javascript中加载数据表

var documentos = <?php echo json_encode($documents);?>;如果(文档!== null){var table = $('#t_documents').DataTable( {语: {"url": "<?=trad($this,'LANG_DATATABLES');?>"},数据:文件,分页:真的,订购:真实,页长:10,列: [{ title: "" },//下载按钮{ title: "<?=trad($this,'FILE_NAME');?>"},{ title: "<?=trad($this,'FILE_TYPE');?>"},{ title: "" }//删除按钮]});}

我也有删除功能.如何在不重新加载页面的情况下重新加载数据(使用 ajax 再次从控制器获取数据)?

解决方案

 var table=$('#tableid');$('#tableid').on('click','thedeletebuton_id',function(event) {event.preventDefault();var id=$(this).data('id');//将 id 传递给控制器​​以使用 ajax 删除$.ajax({类型:POST",url: "<?php echo base_url('你的控制器'); ?>",数据:{id:id},成功:功能(数据){table.ajax.reload();///重新加载表警报('已删除');}});});

I have a datatable in a tab that is loaded from data sended by the controller on index method.

$data = array(
    'documents'      => $this->getDocuments(),
     //more stuff...
);

$this->load->view($this->config->item('groupViews') . 'example/example_edit_view', $data);

I have the view for loading the datatable

<div class="form-group col-md-12">
    <table
        id="t_documents"
        class="table table-striped table-bordered table-hover"
        cellspacing="0"
        width="100%">
     </table>
</div> 

then I load the datatable in javascript when the page is loaded

var documentos = <?php echo json_encode($documents); ?>;
    if ( documentos !== null){
        var table = $('#t_documents').DataTable( {
            language: {
                "url": "<?=trad($this,'LANG_DATATABLES');?>"
     },
     data: documents,
     paging: true,
     ordering: true,
     pageLength: 10,
     columns: [
         { title: "" },    //Download button
         { title: "<?=trad($this,'FILE_NAME');?>" },
         { title: "<?=trad($this,'FILE_TYPE');?>" },
         { title: "" }     //Delete button
     ]
   });
}

I have a delete function too. How can I reload the data (using ajax for getting the data from controller again) without reloading the page?

解决方案

      var table=$('#tableid');
$('#tableid').on('click','thedeletebuton_id',function(event) {
      event.preventDefault();
      var id=$(this).data('id'); // pass the id to the controller to delete using ajax
       $.ajax({
           type: "POST",
           url: "<?php echo base_url('your controller'); ?>",
           data:  {id:id}, 
           success: function(data)
           {
            table.ajax.reload();   /// reloads the table
            alert('Deleted');
           }
       });

      });

相关文章