Datatable是jQuery一個強大的插件,詳見https://www.datatables.net/,下面本人略總結在CodeIghiter框架的前端使用datatable的方法。
常用的配置選項:
根據數據源、性能要求可以配置各種配置項,下面列舉幾個常用的選項:
列定義選項:
回調函數選項:
1.首先需要進行Datatables的一些基礎配置:
“bServerSide”: true, “bProcessing”: true, //顯示提示正在工作中 “bPaginage”: true, “sPaginationType”: “two_buttons”,
2.“sAjaxSource” :用來向DataTable指定加載的外部數據源(如果想使用現有的數據,請使用aoData)可以簡單的提供一個可以用來獲得資料url或者JSON對象,該對象必須包含aaData,作為表格的數據源。
<?PHP echo "\"sAjaxSource\": \"/xxxx/xxx/","; ?>
3.“fnServerData” :此參數可以修改默認功能,從服務器獲取數據($.getJSON),這樣更適合應用程序。
"fnServerData": function (sSource, aoData, fnCallback) { $.ajax ({ "dataType": "json", "type": "POST", "url": sSource, "data": aoData, "success": fnCallback }); },
4. 新版本Datatables與舊版本的不同:
1). “fnRender”: function (setting) { } "mRender": function (data, type, row) { } ;
2). "sPaginationType“: “two_buttons” or “full_numbers”
5. "fnPreDrawCallback": 在每一個表格draw事件發生前調用該函數,通過返回false來取消draw事件其它任何的返回值,包括undefined都會導致draw事件的發生。
Controller:
Controller負責將把view傳遞過來的paging,sorting,searching的信息判斷整理,通過model獲取data list傳送給view顯示。
//Paging: $iDisplayStart = $this->input->get_post('iDisplayStart', true); $iDisplayLength = $this->input->get_post('iDisplayLength', true); if(isset($iDisplayStart) && $iDisplayLength != '-1') { $this->db->limit($this->db->escape_str($iDisplayLength), $this->db->escape_str($iDisplayStart)); } //Ordering: $sTable = 'table_logs_event'; $aColumns = array('id', 'ack', 'datetime', 'category', 'level', 'dev_mac', 'log_evt'); $iSortCol_0 = $this->input->get_post('iSortCol_0', true); $iSortingCols = $this->input->get_post('iSortingCols', true); if(isset($iSortCol_0)) { for($j=0; $j<intval($iSortingCols); $j++) { $iSortCol = $this->input->get_post('iSortCol_'.$j, true); $bSortable = $this->input->get_post('bSortable_'.intval($iSortCol), true); $sSortDir = $this->input->get_post('sSortDir_'.$j, true); if($bSortable == 'true') { $this->db->order_by($aColumns[intval($this->db->escape_str($iSortCol))] , $this->db- >escape_str($sSortDir)); } } } //Filtering: $sSearch = $this->input->get_post('sSearch', true); if(isset($sSearch) && !empty($sSearch)) { for($i=0; $i<count($aColumns); $i++) { $bSearchable = $this->input->get_post('bSearchable_'.$i, true); //單行排序 if(isset($bSearchable) && $bSearchable == 'true') { if ( $criteria == "unread" ) $this->db->or_like($aColumns[$i], $this->db->having('ack', 0)->escape_like_str($sSearch)); else $this->db->or_like($aColumns[$i], $this->db->escape_like_str($sSearch)); } } }
Model:
Model負責從數據表獲取Data list,以及所有需要顯示的信息。
$this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false); if ( $criteria == "unread" ) { $this->db->where('ack', 0); } $query = $this->db->get($sTable); $this->db->select('FOUND_ROWS() AS found_rows'); $iFilteredTotal = $this->db->get()->row()->found_rows; $iTotal = $this->db->count_all($sTable); $output = array( "sEcho" => intval($sEcho), //頁面傳遞用以標記的參數 “iTotalRecords” => $iTotal, //總的數據條數 “iTotalDisplayRecords” => $iFilteredTotal, //獲取顯示在頁面上的數據條數 “aaData” => array() //獲取data list );
