JS : In your view include following files. Please make sure that you include the locale-en.js (or some other locale file), or else your grid would not work.
Model Code : In the model class, lets put this simple code which reads few parameters from the request and fetches the data from the DB.
public function get_grid_data($request_data){
try{
$page = $request_data['page']; // get the requested page
$limit = $request_data['rows']; // get how many rows we want to have into the grid
$totalrows = isset($request_data['totalrows']) ? $request_data['totalrows']: false;
if($totalrows) {
$limit = $totalrows;
}
$this->db->select("*",false);
$this->db->from('test t');
$total_rows = $this->db->get()->result();
$count = count($total_rows);
$response = new stdClass();
if( $count >0 ) {
$total_pages = ceil($count/$limit);
} else {
$total_pages = 0;
}
if ($page > $total_pages){
$page = $total_pages;
}
if ($limit <= 0){
$limit = 0;
}
$start = $limit * $page - $limit;
if ($start<=0){
$start = 0;
}
$response->page = $page;
$response->total = $total_pages;
$response->records = $count;
$response->start = $start;
$response->limit = $limit;
$eligible_rows = array_slice($total_rows, $start, $limit);
$i = 0;
foreach($eligible_rows as $row) {
$response->rows[$i]['id'] = $row->id;
$response->rows[$i++]['cell'] = array($row->id, $row->name, $row->note, $row->amount);
}
return $response;
}catch (Exception $e){
$this->handle_exception($e);
}
}
Controller Code : In the controller, add following code. All this code would do is, invoke model method mentioned above and return the response as JSON.
public function jqgrid_show(){
try{
$response = $this->model_name->get_grid_data($_REQUEST);
header('Content-type: application/json');
echo json_encode($response);
}catch (Exception $e){
$this->handle_controller_exception($e);
}
}
View code: In your view, you need to add following 2 placeholders (1 for grid and 1 for pager).
All content provided on this blog is for informational purposes only. Tudip Technologies provides no endorsement and makes no representations as to accuracy, reliability, completeness, suitability or validity of any information or content on, distributed through or linked, downloaded or accessed from this site. Tudip Technologies will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use of the information on this site. All information is provided on an as-is basis without any obligation to make improvements or to correct errors or omissions. This site may contain links to other websites. Tudip Technologies makes no guarantees or promises regarding these websites and does not necessarily endorse or approve of their content. You may not modify any part of the blog. The inclusion of any part of this blog in another work, whether in printed or electronic or other form, or inclusion of any part of the blog in another website by linking, framing or otherwise without the express permission of Tudip Technologies is prohibited. This site may not be used for any illegal or illicit purpose and Tudip Technologies reserves the right, at its sole discretion and without notice of any kind, to remove anything posted to this site. By using this site, you hereby acknowledge that any reliance upon any materials shall be at your sole risk.