连接表时,Ignited Datatables获取id

时间:2016-04-05 08:06:09

标签: php mysql codeigniter datatable

我使用了点火器的点燃数据插件, 问题是当使用此模型连接两个表时:

public $table = 'user';

public function getDataTable(){
$this->load->library('datatables');
$this->datatables->select('user.username, role.name, user.id')
        ->join('role', ''.$this->table.'.role = role.id')            
        ->edit_column('id', '<button data-id="$1" id="edit" class="btn btn-primary"><i class="fa fa-edit"></i> Edit</button> <button data-id="$1" id="delete" class="btn btn-danger"><i class="fa fa-trash"></i> Delete</button>', 'id');
$this->datatables->from($this->table);

return $this->datatables->generate('json', '');
}

它向我展示了这样的结果: enter image description here

但编辑按钮并没有显示user.id的ID 编辑按钮结果(firebug)

{"sEcho":1,"iTotalRecords":2,"iTotalDisplayRecords":2,"aaData":
[
{"username":"admin","name":"admin","id":"<button data-id=\"id\" id=\"edit\" class=\"btn btn-primary\"><i class=\"fa fa-edit\"><\/i> Edit<\/button> <button data-id=\"id\" id=\"delete\" class=\"btn btn-danger\"><i class=\"fa fa-trash\"><\/i> Delete<\/button>"},
{"username":"admin2","name":"staff","id":"<button data-id=\"id\" id=\"edit\" class=\"btn btn-primary\"><i class=\"fa fa-edit\"><\/i> Edit<\/button> <button data-id=\"id\" id=\"delete\" class=\"btn btn-danger\"><i class=\"fa fa-trash\"><\/i>Delete<\/button>"}
],
"sColumns":"user.username,role.name,user.id"}

你可以看到data-id =&#34; id&#34;但我想要data-id =&#34; 1&#34;或data-id =&#34; 2&#34; (来自$ 1的user.id结果)

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

经过几个小时面对这个问题,我现在找到了答案! 这个问题是由FK和PK的歧义引起的,所以我在select查询中添加了别名。

我更改了这一行:

$this->datatables->select('user.username, role.name, user.id')

$this->datatables->select('user.username, role.name, user.id as uid')

然后更改

->edit_column('id', '<button data-id="$1" id="edit" class="btn btn-primary"><i class="fa fa-edit"></i> Edit</button> <button data-id="$1" id="delete" class="btn btn-danger"><i class="fa fa-trash"></i> Delete</button>', 'id');

->edit_column('id', '<button data-id="$1" id="edit" class="btn btn-primary"><i class="fa fa-edit"></i> Edit</button> <button data-id="$1" id="delete" class="btn btn-danger"><i class="fa fa-trash"></i> Delete</button>', 'uid');