我正在使用ignited datatable library来显示来自服务器端的记录
这是我的控制器
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Tryy extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->config->load('category_rules');
$this->load->library('Datatables');
$this->load->model('category_model');
}
public function index()
{
$this->load->view('admin/add_category1.php');
}
public function datatable()
{
$this->datatables->select('parent_category_id,parent_category_name')
->from('parent_category');
echo $this->datatables->generate();
}
}
控制器中的数据表()生成在数组
之后{
"draw":0,
"recordsTotal":6,
"recordsFiltered":6,
"data":[
{
"parent_category_id":"22",
"parent_category_name":"Live"
},
{
"parent_category_id":"23",
"parent_category_name":"Work"
},
{
"parent_category_id":"24",
"parent_category_name":"Enjoy"
},
{
"parent_category_id":"25",
"parent_category_name":"Move"
},
{
"parent_category_id":"26",
"parent_category_name":"Study"
},
{
"parent_category_id":"27",
"parent_category_name":"Misc"
}
]
}
查看文件(admin / add_category1.php)
<table class="table table-striped table-bordered table-hover" id="big_table">
<thead>
<tr>
<th>parent category id</th>
<th>parent category name</th>
</tr>
</thead>
<tbody>
<?php echo $this->table->generate(); ?>
</tbody>
</table>
<script type="text/javascript">
$(document).ready(function() {
$('#big_table').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": '<?php echo base_url('tryy/datatable'); ?>',
"aoColumns": [
{ "mDataProp": "parent_category_id" },
{ "mDataProp": "parent_category_name" }
]
} );
} );
</script>
数据显示但
搜索,排序,显示没有记录下拉列表无法正常运行
答案 0 :(得分:1)
大多数情况下,错误表示您正在调用和显示的列之间存在不匹配。但我没有使用codeigniter库。
另一方面,你正在构建错误的表头,它应该是:
<table class="table table-striped table-bordered table-hover" id="big_table">
<thead>
<tr>
<th>parent category id</th>
<th>parent category name</th>
</tr>
</thead>
<tbody
我相信,你不需要mDataProp的东西,但如果你调用它,你应该使用正确的密钥名称:
而不是:
{ "mDataProp": "parent category id" },
{ "mDataProp": "parent category name" }
你应该用下划线写下名字:
{ "mDataProp": "parent_category_id" },
{ "mDataProp": "parent_category_name" }
答案 1 :(得分:1)
最后我使用了这段代码..而我的服务器端数据表就像魅力一样
控制器
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Tryy extends CI_Controller {
public function __construct()
{
parent::__construct();
//$this->output->enable_profiler(TRUE);
$this->config->load('category_rules');
$this->load->model('category_model','parent_category');
}
public function index()
{
$this->load->view('admin/add_category1.php');
}
public function ajax_list()
{
error_reporting(0);
$list = $this->parent_category->get_datatables();
$data = array();
$no = $_POST['start'];
foreach ($list as $person) {
$no++;
$row = array();
$row[] = $person->parent_category_id;
$row[] = $person->parent_category_name;
//add html for action
$row[] =
'<i class="glyphicon glyphicon-pencil"></i> Edit</a>
<i class="glyphicon glyphicon-trash"></i> Delete</a>';
$data[] = $row;
}
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->parent_category->count_all(),
"recordsFiltered" => $this->parent_category->count_filtered(),
"data" => $data,
);
//output to json format
echo json_encode($output);
}
}
模型
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Category_model extends CI_Model {
var $table = 'parent_category';
var $column = array('parent_category_id','parent_category_name');
var $order = array('parent_category_id' => 'desc');
public function __construct()
{
parent::__construct();
$this->load->database();
}
private function _get_datatables_query()
{
$this->db->from($this->table);
$i = 0;
foreach ($this->column as $item)
{
if($_POST['search']['value'])
($i===0) ? $this->db->like($item, $_POST['search']['value']) : $this->db->or_like($item, $_POST['search']['value']);
$column[$i] = $item;
$i++;
}
if(isset($_POST['order']))
{
$this->db->order_by($column[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
}
else if(isset($this->order))
{
$order = $this->order;
$this->db->order_by(key($order), $order[key($order)]);
}
}
function get_datatables()
{
$this->_get_datatables_query();
if($_POST['length'] != -1)
$this->db->limit($_POST['length'], $_POST['start']);
$query = $this->db->get();
return $query->result();
}
function count_filtered()
{
$this->_get_datatables_query();
$query = $this->db->get();
return $query->num_rows();
}
public function count_all()
{
$this->db->from($this->table);
return $this->db->count_all_results();
}
public function get_by_id($id)
{
$this->db->from($this->table);
$this->db->where('id',$id);
$query = $this->db->get();
return $query->row();
}
public function save($data)
{
$this->db->insert($this->table, $data);
return $this->db->insert_id();
}
public function update($where, $data)
{
$this->db->update($this->table, $data, $where);
return $this->db->affected_rows();
}
public function delete_by_id($id)
{
$this->db->where('id', $id);
$this->db->delete($this->table);
}
}
查看
<!-- BEGIN PAGE LEVEL STYLES -->
<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>assets1/global/plugins/datatables/extensions/ColReorder/css/dataTables.colReorder.min.css"/>
<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>assets1/global/plugins/datatables/plugins/bootstrap/dataTables.bootstrap.css"/>
<!-- END PAGE LEVEL STYLES -->
<!-- BEGIN PAGE CONTAINER -->
<div class="page-container">
<table id="table" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th style="width:125px;">Action</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<?php //echo $this->table->generate(); ?>
</div>
</div>
<!-- END EXAMPLE TABLE PORTLET-->
</div>
<script type="text/javascript" src="<?php echo base_url();?>assets1/global/plugins/datatables/media/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="<?php echo base_url();?>assets1/global/plugins/datatables/plugins/bootstrap/dataTables.bootstrap.js"></script>
<!-- END PAGE LEVEL PLUGINS -->
<script type="text/javascript">
$(document).ready(function() {
table = $('#table').DataTable({
"processing": true, //Feature control the processing indicator.
"serverSide": true, //Feature control DataTables' server-side processing mode.
// Load data for the table's content from an Ajax source
"ajax": {
"url": "<?php echo site_url('admin/tryy/ajax_list')?>",
"type": "POST"
},
//Set column definition initialisation properties.
"columnDefs": [
{
"targets": [ -1 ], //last column
"orderable": false, //set not orderable
},
],
});
});
</script>
答案 2 :(得分:0)
您可以使用此代码,而不是使用ignited datatable library
function datatable()
{
$aColumns = array('parent_category_id','parent_category_name');
$sTable = 'parent_category';
$iDisplayStart = $this->input->get_post('iDisplayStart', true);
$iDisplayLength = $this->input->get_post('iDisplayLength', true);
$iSortCol_0 = $this->input->get_post('iSortCol_0', true);
$iSortingCols = $this->input->get_post('iSortingCols', true);
$sSearch = $this->input->get_post('sSearch', true);
$sEcho = $this->input->get_post('sEcho', true);
// Paging
if(isset($iDisplayStart) && $iDisplayLength != '-1')
{
$this->db->limit($this->db->escape_str($iDisplayLength), $this->db->escape_str($iDisplayStart));
}
// Ordering
if(isset($iSortCol_0))
{
for($i=0; $i<intval($iSortingCols); $i++)
{
$iSortCol = $this->input->get_post('iSortCol_'.$i, true);
$bSortable = $this->input->get_post('bSortable_'.intval($iSortCol), true);
$sSortDir = $this->input->get_post('sSortDir_'.$i, true);
if($bSortable == 'true')
{
$this->db->order_by($aColumns[intval($this->db->escape_str($iSortCol))], $this->db->escape_str($sSortDir));
}
}
}
/*
* Filtering
* NOTE this does not match the built-in DataTables filtering which does it
* word by word on any field. It's possible to do here, but concerned about efficiency
* on very large tables, and MySQL's regex functionality is very limited
*/
if(isset($sSearch) && !empty($sSearch))
{
for($i=0; $i<count($aColumns); $i++)
{
$bSearchable = $this->input->get_post('bSearchable_'.$i, true);
// Individual column filtering
if(isset($bSearchable) && $bSearchable == 'true')
{
$this->db->or_like($aColumns[$i], $this->db->escape_like_str($sSearch));
}
}
}
// Select Data
$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
$rResult = $this->db->get($sTable);
// Data set length after filtering
$this->db->select('FOUND_ROWS() AS found_rows');
$iFilteredTotal = $this->db->get()->row()->found_rows;
// Total data set length
$iTotal = $this->db->count_all($sTable);
// Output
$output = array(
'sEcho' => intval($sEcho),
'iTotalRecords' => $iTotal,
'iTotalDisplayRecords' => $iFilteredTotal,
'aaData' => array()
);
foreach($rResult->result_array() as $aRow)
{
$row = array();
foreach($aColumns as $col)
{
$row[] = $aRow['parent_category_id'];
$row[] = $aRow['parent_category_name'];
}
$output['aaData'][] = $row;
//print_r($output['aaData']);
}
echo json_encode($output);
}
在您的视图文件中。
$('#big_table').dataTable({
"infoEmpty": "No records available",
"sProcessing": "DataTables is currently busy",
"processing": true,
"bSort": false,
"bFilter": false,
"bAutoWidth": true,
"bLengthChange": true,
"serverSide": true,
"sAjaxSource": "<?php echo base_url('tryy/datatable'); ?>",
"aLengthMenu": [[10, 25, 50,100], [10, 25, 50,100]],
"sSortAsc": [[1, 'desc']],
"iDisplayLength": 10,
"dom": 'Zlfrtip',
"bDeferRender": true
});
你的表格html就像。
<table id="big_table" class="table table-bordered table-striped">
<thead>
<tr>
<th>parent_category_id</th>
<th>parent_category_name</th>
</tr>
</thead>
</table>