2016-09-29 115 views
0

我有一個從控制器在索引方法發送的數據中加載的選項卡中的數據表。重新加載數據表而無需使用ajax刷新

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

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

我有裝載數據表

<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> 

的觀點,然後我加載數據表中的JavaScript時,頁面加載

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 
    ] 
    }); 
} 

我有一個刪除功能了。如何重新加載數據(使用ajax從控制器再次獲取數據)而不重新加載頁面?

+0

使用ajax請求 – madalinivascu

回答

2
 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'); 
      } 
     }); 

     }); 
+0

感謝它適用於我。 – Noark

+0

@Noark有趣的是,我剛剛實施了這個代碼15分鐘之前,我回答了這個問題....很高興它來到更多的使用,然後只是我的應用程序:) –

+0

什麼運氣!謝謝 – Noark

相關問題