I am a new programmer in code igniter. I am trying to integrate jqgrid with a code igniter, and seven hours later I came to a successful point where jqgrid and code igniter are fully integrated with the search option.
First write the model in your application / model directory. The code...
class JqgridSample extends CI_Model { function getAllData($start,$limit,$sidx,$sord,$where){ $this->db->select('id,name,email,passport,phone,fax,address'); $this->db->limit($limit); if($where != NULL) $this->db->where($where,NULL,FALSE); $this->db->order_by($sidx,$sord); $query = $this->db->get('info',$limit,$start); return $query->result(); } }
Then write the controller in the directory of your application / controller. The code
class Demo extends CI_Controller { function __construct() { parent::__construct(); $this->load->model('JqgridSample'); } function jqGrid(){ $this->load->view('showGrid'); } function loadData(){ $page = isset($_POST['page'])?$_POST['page']:1; $limit = isset($_POST['rows'])?$_POST['rows']:10; $sidx = isset($_POST['sidx'])?$_POST['sidx']:'name'; $sord = isset($_POST['sord'])?$_POST['sord']:''; $start = $limit*$page - $limit; $start = ($start<0)?0:$start; $where = ""; $searchField = isset($_POST['searchField']) ? $_POST['searchField'] : false; $searchOper = isset($_POST['searchOper']) ? $_POST['searchOper']: false; $searchString = isset($_POST['searchString']) ? $_POST['searchString'] : false; if ($_POST['_search'] == 'true') { $ops = array( 'eq'=>'=', 'ne'=>'<>', 'lt'=>'<', 'le'=>'<=', 'gt'=>'>', 'ge'=>'>=', 'bw'=>'LIKE', 'bn'=>'NOT LIKE', 'in'=>'LIKE', 'ni'=>'NOT LIKE', 'ew'=>'LIKE', 'en'=>'NOT LIKE', 'cn'=>'LIKE', 'nc'=>'NOT LIKE' ); foreach ($ops as $key=>$value){ if ($searchOper==$key) { $ops = $value; } } if($searchOper == 'eq' ) $searchString = $searchString; if($searchOper == 'bw' || $searchOper == 'bn') $searchString .= '%'; if($searchOper == 'ew' || $searchOper == 'en' ) $searchString = '%'.$searchString; if($searchOper == 'cn' || $searchOper == 'nc' || $searchOper == 'in' || $searchOper == 'ni') $searchString = '%'.$searchString.'%'; $where = "$searchField $ops '$searchString' "; } if(!$sidx) $sidx =1; $count = $this->db->count_all_results('info'); if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } if ($page > $total_pages) $page=$total_pages; $query = $this->JqgridSample->getAllData($start,$limit,$sidx,$sord,$where); $responce->page = $page; $responce->total = $total_pages; $responce->records = $count; $i=0; foreach($query as $row) { $responce->rows[$i]['id']=$row->id; $responce->rows[$i]['cell']=array($row->name,$row->email,$row->passport,$row->phone,$row->fax,$row->address); $i++; } echo json_encode($responce); } }
And finally, you write the view in your application / view directory.
<head> <link rel="stylesheet" type="text/css" href="<?php echo base_url()?>application/views/css/custom-theme/jquery-ui-1.8.16.custom.css" /> <link type="text/css" href="<?php echo base_url()?>application/views/css/ui.jqgrid.css" rel="stylesheet" /> <link type="text/css" href="<?php echo base_url()?>application/views/css/plugins/searchFilter.css" rel="stylesheet" /> <style> html, body { margin: 0; padding: 0; font-size: 75%; } </style> <script type="text/javascript" src="<?php echo base_url(); ?>application/views/js/jquery-1.5.2.min.js"></script> <script type="text/javascript" src="<?php echo base_url(); ?>application/views/js/i18n/grid.locale-en.js"></script> <script type="text/javascript" src="<?php echo base_url(); ?>application/views/js/jquery.jqGrid.min.js"></script> <title>Codeigniter With JQGrid</title> </head> <body> <center> <h1>Codeigniter With JQGrid</h1> <?php $ci =& get_instance(); $base_url = base_url(); ?> <table id="list"></table> <div id="pager"></div> </center> </body> <script type="text/javascript"> $(document).ready(function (){ jQuery("#list").jqGrid({ url:'<?=$base_url.'index.php/demo/loadData'?>', </script>
For the view file for myself, I create two folders in the view directory js and css. and in js foder put jquery-1.5.2.min.js, grid.locale-en.js (views / js / i18n /), jquery.jqGrid.min.js, which you will find in the jqgrid package.
In the same way, you need jquery-ui-1.8.16.custom.css, ui.jqgrid.css, which is also available in the jqgrid package.
To start the full process, you need to create a database named jqgrid_sample, and in the database create a table named info that contains the fields ...
ID
name
Email
Passport
telephone
fax machine
address
here it is. enjoy. bye.