根据数据表中其他列的值在值列中添加IF条件

时间:2018-09-11 02:43:43

标签: codeigniter datatable

我想添加一个条件,如果状态列为“打开”则将调出“视图”,而如果状态为“关闭”或“取消”则仅调出“ view1”列。这可能吗?

模型

 function json() {
        $this->datatables->select('id_lowongan_pekerjaan,judul_lowongan_pekerjaan,tanggal_berakhir,status,tgl_buat');
        $this->datatables->from('lowongan_pekerjaan');
        $this->datatables->add_column('view', '<a href="lowongan_pekerjaan/edit_lowongan_pekerjaan/$1"> <i class="fa fa-edit"></i></a>  
                                               <a onclick="return confirm(\'Apa anda yakin untuk hapus Data ini?\')" href="lowongan_pekerjaan/delete_lowongan_pekerjaan/$1"><i class="fa fa-trash"></i></a>', 'id_lowongan_pekerjaan');
        $this->datatables->add_column('view1', '', 'id_lowongan_pekerjaan');
        $this->datatables->where('status !="delete"');
        return $this->datatables->generate();
    }

视图

columns: [
     {
    "data": "id_lowongan_pekerjaan",
    "orderable": false
     },
     {"data": "id_lowongan_pekerjaan"},
     {"data": "judul_lowongan_pekerjaan"},
     {"data": "tanggal_berakhir"},
     {"data": "status"},
     {"data": "status", 
              "render": function(data) { 
              if (data === "Open") {
                  return {"data": "view"} 
                }
                else {
                  return {"data": "view1"} 
                }

              } 
     },
     //{"data": "view"}
    ],

1 个答案:

答案 0 :(得分:1)

点燃的数据表没有那种按列的功能。您可以使用我个人使用的“ fork”,它允许外部回调。只需将库替换为我的库即可:https://pastebin.com/Y2mH72SE

控制器:

$this->load->model('some_model');

然后

->add_column('view', array($this->some_model, 'callback_view'), 'id_lowongan_pekerjaan, status');

echo json_encode($this->datatables->generate());

型号:

class Some_model extends CI_Model {
    public function callback_view($id, $status) {
        return $this->load->view('some_view', array('id' => $id, 'status' => $status),  true);
    }
}

some_view.php

<?php if ($status == 'Open'): ?>
<a href="lowongan_pekerjaan/edit_lowongan_pekerjaan/<?php echo $id; ?>"> <i class="fa fa-edit"></i></a>  
<a onclick="return confirm(\'Apa anda yakin untuk hapus Data ini?\')" href="lowongan_pekerjaan/delete_lowongan_pekerjaan/<?php echo $id; ?>"><i class="fa fa-trash"></i></a>
<?php endif; ?>

使用说明

您仍然可以执行内联操作,例如:$this->datatables->add_column('edit', '<a href="profiles/edit/$1">EDIT</a>', 'id');,但在我的版本中,该格式必须为sprintf形式:$this->datatables->add_column('edit', '<a href="profiles/edit/%s">EDIT</a>', 'id');