3

То, что я до сих пор:Codeigniter + DataTable пользовательские удалить с начальной загрузки модели

$ (документ) .ready (функция() {

 table = $('#users').DataTable({ 
      "processing": true, 
      "ajax": "<?php echo site_url('main/ajax_request'); ?>", 
      "deferRender": true, 
      "columns": [ 
          { "data": "id", "width": "6%", }, 
          { "data": "description" }, 
          { "data": "name" }, 
          { "data": "relation2" }, 

          { 
           "data": null, 
           "width": "6%", 
           "className": "center", 
           "defaultContent": '<a href="<?php echo site_url("model/delete/"); ?>" class="editor_remove" data-toggle="modal" data-target="#myModal"><i class="fa fa-trash"></i></a>' 
          }, 

         ], 

      "dom": 'Tlfrtip', 
      "aaSorting": [], 
      "iDisplayLength": 25, 
      "bStateSave": false, 

      // table tools 
      "tableTools": { 
       sSwfPath: "<?php echo base_url(); ?>assets/plugins/datatable/TableTools/swf/copy_csv_xls_pdf.swf", 
       aButtons: [ 
       { sExtends :'pdf', 
        oSelectorOpts: { filter: 'applied', 
            order: 'current', 
           }, 
        sPdfOrientation: "landscape", 
        sPdfMessage: "Export Aplicatie", 
        bFooter: false, 
       }, 

       { sExtends :'xls', 
        oSelectorOpts: { filter: 'applied', 
            order: 'current', 
           }, 
        bFooter: false, 
       }, 

       { sExtends :'print', 
        oSelectorOpts: { filter: 'applied', 
            order: 'current', 
           }, 
        bFooter: false, 
       }, 
       ], 

       //"sRowSelect": "single", 
      }, 
     }); 

     // Setup - add a text input to each footer cell 
     $('#users tfoot th').each(function() { 
      var title = $('#users thead th').eq($(this).index()).text(); 
      $(this).html('<input type="text" style="width:100%" class="form-control" />'); 
     }); 

     // Apply the search 
     table.columns().eq(0).each(function (colIdx) { 
      $('input', table.column(colIdx).footer()).on('keyup change', function() { 
       table 
        .column(colIdx) 
        .search(this.value) 
        .draw(); 
      }); 
     }); 
    }); 

Контроллер:

// ajax request 
public function ajax_request() { 
    $response = json_encode(array("data" => $this->Misc_model->getRecords())); 

    echo $response; 
} 

Модель:

public function getRecords() { 
    $data = array(); 

    $this->db->select("records.id, records.description, relation_1.name, records.relation2") 
      ->from('records') 
      ->join('relation_1', "relation_1.id = records.relation", 'LEFT') 
      ->where_not_in("deleted", '1'); 

    $query = $this->db->get(); 
    if($query->num_rows() > 0) { 
     foreach ($query->result() as $row) { 
      $data[] = $row; 
     } 
    } 

    return $data; 
} 

HTML :

 <table id="users" class="table table-bordered" cellspacing="0" width="100%"> 
      <thead> 
       <tr> 
        <th>ID</th> 
        <th>Description</th> 
        <th>Single 1</th> 
        <th>Single 2</th> 
        <th>Delete</th> 
       </tr> 
      </thead> 

      <tfoot> 
       <tr> 
        <th>ID</th> 
        <th>Description</th> 
        <th>Single 1</th> 
        <th>Single 2</th> 
        <th>Delete</th> 
       </tr> 
      </tfoot> 

     </table> 

Теперь мой вопрос заключается в следующем:

Как я могу получить значение ID, так что я могу использовать его в:

<?php echo site_url("model/delete/$id"); ?> 

и если это невозможно, есть другой способ сделать это?

ответ

2

Это в вашей модели getRecords, что вы определяете данные:

public function getRecords() { 

    $data = $this->db->select("records.id, records.description, relation_1.name, records.relation2") 
      ->from('records') 
      ->join('relation_1', "relation_1.id = records.relation", 'LEFT') 
      ->where_not_in("deleted", '1') 
      ->get()->result(); 

    foreach($data as $d) { 
     $d->href = '<a href="' . site_url("model/delete/" . $d->id) . '" class="editor_remove" data-toggle="modal" data-target="#myModal"><i class="fa fa-trash"></i></a>'; 
    } 

    return $data; 
} 

И в вашем JQuery DataTable заменить это:

{ 
"data": null, 
"width": "6%", 
"className": "center", 
"defaultContent": '<a href="<?php echo site_url("model/delete/"); ?>" class="editor_remove" data-toggle="modal" data-target="#myModal"><i class="fa fa-trash"></i></a>' 
}, 

Как это:

{ "data": "href" }, 
+0

Это не может work ... – George

+0

Можете ли вы показать свою функцию ajax_request? Именно в этой функции вы определяете свой '" class="editor_remove" data-toggle="modal" data-target="#myModal">' – maX

+0

Готово. Если вам нужно больше рассказать мне. – George

Смежные вопросы