Codeigniter + dataTable 自定义删除与引导模型
Posted
技术标签:
【中文标题】Codeigniter + dataTable 自定义删除与引导模型【英文标题】:Codeigniter + dataTable custom delete with bootstrap model 【发布时间】:2015-05-22 05:58:53 【问题描述】:到目前为止我所拥有的:
$(document).ready(function()
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" >
<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"); ?>
如果这是不可能的,有没有其他方法可以做到这一点?
【问题讨论】:
【参考方案1】:您可以在 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 数据表中替换这个:
"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" ,
【讨论】:
你能展示你的 ajax_request 函数吗?您可以在此函数中定义您的<a href="<?php echo site_url("model/delete/"); ?>" class="editor_remove" data-toggle="modal" data-target="#myModal"><i class="fa fa-trash"></i></a>
完成。如果您需要更多信息,请告诉我。
谢谢。我现在收到了,没想到要从模型发送。
我还有一个问题。这是实现这一目标的唯一方法吗?请求从 400 kb 变为 1 MB ...
我认为,但文件更大并不重要,它只是php中的处理,无论如何它不会影响性能。以上是关于Codeigniter + dataTable 自定义删除与引导模型的主要内容,如果未能解决你的问题,请参考以下文章
DataTables 函数在 Codeigniter 中不起作用
DataTables TableTools 插件 - 如何在 Codeigniter 中配置 sSwfPath 路径?
CodeIgniter 或 Ignite Datatables 中的长尾 where 子句
Codeigniter:this->datatables->select(sample)->from(sample)->where()