jqGrid dataInit doesn't trigger - php
I'm working with a project that uses jqGrid in the most recent version.
The thing is that this project is PHP(5.6) and uses JSON to "translate/talk" to jqGrid framework, and colModel parameters are all inside PHP arrays. It works flawlessly but i'm unable to trigger dataInit of colModel "criacao" using the actual project's code.
public function laudos($section)
{
$table = 'laudos';
$fields = array('id','laudo','nome_fantasia','cliente','cadastro_id','email','senha','exame','descricao','criacao','exclusao','arquivo');
$tabela = array(
'colNames' => array('ID','Laudo','Clínica','Nome','Cadastro','Email','Senha','Exame','Descrição','Criação','Exclusão','Arquivo'),
'colModel' => array(
array('name'=>'id','hidden'=>true,'search'=>true,'key'=>true),
array('name'=>'laudo','index'=>'laudo','width'=>70,'align'=>'center','search'=>true,'editable'=>true,'editrules'=>array('required'=>true),'sorttype'=>'integer','searchoptions'=>array('sopt'=>'[eq,cn]', 'clearSearch'=>false)),
array('name'=>'nome_fantasia','search'=>true,'width'=>170,'align'=>'center','editable'=>false,'sorttype'=>'text','searchoptions'=>array('sopt'=>'[eq,cn]','clearSearch'=>false),'editrules'=>array('required'=>true)),
array('name'=>'cliente','search'=>true,'width'=>170,'align'=>'center','editable'=>false,'sorttype'=>'text','searchoptions'=>array('sopt'=>'[eq,cn]','clearSearch'=>false),'editrules'=>array('required'=>true),),
array('name'=>'cadastro_id','search'=>true,'hidden'=>true,
'editable'=>true,'edittype'=>'text','searchoptions'=>array('sopt'=>'[eq,cn]','clearSearch'=>false),'editrules'=>array('edithidden'=>true,'required'=>true),
'editoptions'=>array('dataInit'=>'[]')),
array('name'=>'email','search'=>true,'hidden'=>true,'editable'=>true,'sorttype'=>'email','searchoptions'=>array('sopt'=>'[eq,cn]','clearSearch'=>false),'editrules'=>array('edithidden'=>true)),
array('name'=>'senha','search'=>true,'hidden'=>true,'editable'=>true,'editrules'=>array('edithidden'=>true)),
array('name'=>'exame','search'=>true,'width'=>50,'align'=>'center','editable'=>true,'sorttype'=>'text','searchoptions'=>array('sopt'=>'[eq,cn]','clearSearch'=>false),'formatter'=>'select','edittype'=>'select',
'editoptions'=>array('value'=>array('Biópsia'=>'Biópsia','Necrópsia'=>'Necrópsia','Citologia'=>'Citologia'))
),
array('name'=>'descricao','search'=>true,'width'=>200,'align'=>'center','editable'=>true,'sorttype'=>'text','searchoptions'=>array('sopt'=>'[eq,cn]','clearSearch'=>false)),
array('name'=>'criacao','search'=>true,'width'=>70,'formatter'=>'date','fixed'=>true,'resizable'=>false,'align'=>'center','sorttype'=>'date','searchoptions'=>array('sopt'=>'[eq,cn]','clearSearch'=>false), 'editoptions'=>array('dataInit'=>'function (elem) { $(elem).datepicker();')),
array('name'=>'exclusao','search'=>true,'width'=>70,'formatter'=>'date','sorttype'=>'date','fixed'=>true,'resizable'=>false,'editable'=>true,'searchoptions'=>array('sopt'=>'[eq,cn]','clearSearch'=>false),'align'=>'center'),
array('name'=>'arquivo','search'=>false,'width'=>60,'formatter'=>'arquivo','classes'=>'tabela_laudo_arquivo','editable'=>true,'searchoptions'=>array('sopt'=>false,'clearSearch'=>false))
),
'sortname' => 'id',
'caption' => 'Registros de Laudos Cadastrados',
);
This is the PHP function that returns a responce to jqgrid framework:
private function tabelas($table, $fields, $where = '1 = 1')
{
$page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1; // get the requested page
$limit = isset($_REQUEST['rows']) ? $_REQUEST['rows'] : 99999; // get how many rows we want to have into the grid
$sidx = isset($_REQUEST['sidx']) ? $_REQUEST['sidx'] : 'id'; // get index row - i.e. user click to sort
$sord = isset($_REQUEST['sord']) ? $_REQUEST['sord'] : 'desc'; // get the direction
if(!$sidx) $sidx =1;
$count = $this->db->get_var("SELECT COUNT(*) AS count FROM $table WHERE $where");
if($count > 0)
{
$total_pages = ceil($count/$limit);
}
else
{
$total_pages = 0;
}
if ($page > $total_pages) $page = $total_pages;
$start = $limit * $page - $limit; // do not put $limit*($page - 1)
$sql = "SELECT " . implode(',',$fields) . " FROM $table WHERE $where ORDER BY $sidx $sord LIMIT $start, $limit";
$result = $this->db->get_results($sql);
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
$i = 0;
foreach($result as $row)
{
$responce->rows[$i]['id'] = $row->id;
foreach($fields as $field)
{
$responce->rows[$i]['cell'][] = $row->$field;
}
$i++;
}
return $responce;
}
Using $.extend in my jQGrid script did the job.
Question already answered here.
Related
Pagination in codeigniter 3 and send to api
I have following function. public function sendstocktobl() { if ($_SERVER['REMOTE_ADDR'] !== $this->localhost) { echo $_SERVER['REMOTE_ADDR']; } else { $this->load->database(); $url = get_integration_url('baselinker'); $this->db->select('user_id'); $this->db->where('bl_send_stock', 1); $this->db->where('is_baselinker', 1); $this->db->where('is_active', 1); $resArr = $this->db->get('ci_users')->result_array(); foreach ($resArr as $row) { $user_id = $row['user_id']; $limit = 1000; $offset = 0; $where = "quantity is NOT NULL"; $this->db->where($where); $this->db->where('is_active', 1); $this->db->where('user_id', $user_id); $total_results = $this->db->count_all('ci_products'); $total_pages = ceil($total_results / $limit); $token = get_bl_token($user_id); $inventoryid = get_bl_inventory($user_id); $blwh = get_bl_warehouse($user_id); for ($page = 1; $page <= $total_pages; $page++) { $arr = [ "inventory_id" => $inventoryid, "products" => [], ]; $this->db->select('*'); $where = "quantity is NOT NULL"; $this->db->where($where); $this->db->where('is_active', 1); $this->db->where('user_id', $user_id); $prodArr = $this->db->get('ci_products')->result_array(); foreach ($prodArr as $prod) { $arr['products'][$prod['id']] = [$blwh => $prod['quantity']]; } $offset += $limit; } $preparebl = json_encode($arr); echo $preparebl; } } } Long story short, i need to grab data from sql and send to an api for each users which meets some requirements. I need to prepare apicall like this it's now $arr , but the point it's that pagination it's not working, for user 1 i have 1077 records and i get just one json array with products (1077 products), instead of 2 with maximum 1k . Could someone help me a bit cause i'm not getting where is the error in my script..
Forgotted to add $limit and offset there: $prodArr = $this->db->get('ci_products')->result_array(); Correct: $prodArr = $this->db->get('ci_products', $limit, $offset)->result_array(); And added also apicall in "for" loop. I let here my function which, now it's working fine, maybel it will help someone to inspire. public function sendstocktobl(){ if ($_SERVER['REMOTE_ADDR'] !== $this->localhost) {echo $_SERVER['REMOTE_ADDR'];} else{ $this->load->database(); $url = get_integration_url('baselinker'); $this->db->select('user_id'); $this->db->where('bl_send_stock',1); $this->db->where('is_baselinker',1); $this->db->where('is_active',1); $resArr = $this->db->get('ci_users')->result_array(); foreach ($resArr as $row){ $user_id = $row['user_id']; $limit = 1000; $offset = 0; $where = "quantity is NOT NULL"; $total_results = $this->db->where('user_id',$user_id)->where('is_active',1)->where($where)->from("ci_products")->count_all_results(); $total_pages = ceil($total_results / $limit); $token = get_bl_token($user_id); $inventoryid = get_bl_inventory($user_id); $blwh = get_bl_warehouse($user_id); for ($page = 1; $page <= $total_pages; $page++) { $arr = [ "inventory_id" => $inventoryid, "products" => [], ]; $this->db->select('*'); $where = "quantity is NOT NULL"; $this->db->where($where); $this->db->where('is_active',1); $this->db->where('user_id',$user_id); $prodArr = $this->db->get('ci_products', $limit, $offset)->result_array(); foreach($prodArr as $prod){ $arr['products'][$prod['id']] = [$blwh => $prod['quantity']]; } $preparebl = json_encode($arr); echo $preparebl; $offset += $limit; } } } }
Pagination for foreach loop
I currently have a method inside my "car class" that display the car: static function getCars(){ $autos = DB::query("SELECT * FROM automoviles"); $retorno = array(); foreach($autos as $a){ $automovil = automovil::fromDB($a->marca, $a->modelo, $a->version, $a->year, $a->usuario_id, $a->kilometraje, $a->info, $a->hits, $a->cilindrada, $a->estado, $a->color, $a->categoria, $a->precio, $a->idAutomovil); array_push($retorno, $automovil); } return $retorno; } In my index.php I call that function foreach(car::getCars() as $a){ That allows me to display the info this way ( of course inside the foreach I have a huge code with the details I'll display. Is there a way to implement a pagination to that thing so I can handle 8 cars per page, instead of showing all of them at the same page?
You can add a $limit and $page parameter on your function so that it will only return a maximum of $limit number of items starting from $limit * $page(or will call it the $offset). You also need to add a function to get the total number of rows you have for automoviles table. static function getCars($page = 0, $limit = 8){ $offset = $limit * max(0, $page - 1); //replace this with prepared statement $autos = DB::query("SELECT * FROM automoviles LIMIT $offset, $limit"); $retorno = array(); foreach($autos as $a){ $automovil = automovil::fromDB($a->marca, $a->modelo, $a->version, $a->year, $a->usuario_id, $a->kilometraje, $a->info, $a->hits, $a->cilindrada, $a->estado, $a->color, $a->categoria, $a->precio, $a->idAutomovil); array_push($retorno, $automovil); } return $retorno; } static function getTotal() { //query to get total number of rows in automoviles table } In your index.php do this: foreach(car::getCars((isset($_GET['page']) ? $_GET['page'] : 1)) as $a){ ... } and add the pagination links. $total = car::getTotal(); if($total > 8) { for($i = 1; $i <= intval(ceil(1.0 * $total / $limit)); $i++) { echo '' . $i . '; } }
Datagrid pagination, serverside easyui doesn't work
I am working with datagrid EasyUI, and I want to do a pagination. In Datagrid displays only 10 row, a shows me 'Displaying 1 to 10 of 10 items'. I don't know if the output array it's ok, to send to Datagrid. Here is my code: public function get_temperatura_humedad_list($page, $rows) { $offset = ($page - 1) * $rows; $result = array(); $rs = $this->db->consulta("select count(*) from dht22"); $row = mysqli_fetch_row($rs); $result['total'] = $row[0]; $rs = $this->db->consulta("select * from dht22 limit $offset, $rows"); $items = array(); while ($row = mysqli_fetch_object($rs)) { array_push($items, $row); } // $result["rows"] = $items; return $result['rows'] = $items; }
You should define your variable $rows to get the right offset. Something like this : $rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10; $offset = ($page-1)*$rows;
Jqgrid Pagination not working due to query
I am trying to set up a jqgrid and I am having difficulty in constructing the controller that generates the json data to populate the grid. I am using codeigniter 2.0+ and I am not sure how to build the query for php in codeigniter. I followed this guid under "Loading Data -> JSON Data" for the jqgrid. I also consulted codeigniter docs on data selection. The thing is I am not sure how to write the second query to sort and limit according to the jqgrid paramiters. Here is my controller. public function applicantdata(){ $page = $this->input->get('page');// get the requested page $limit = $this->input->get('rows');// get how many rows we want to have into the grid $sidx = $this->input->get('sidx');// get index row - i.e. user click to sort $sord = $this->input->get('sord');// get the direction if(!$sidx){ $sidx =1; } $this->db->select('*'); $this->db->from('applicant'); $this->db->join('transaction', 'transaction.applicant_id = applicant.id'); $query = $this->db->get(); $count = $query->num_rows(); $limit = 10; if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } if ($page > $total_pages){ $page=$total_pages; } $start = $limit*$page - $limit; // do not put $limit*($page - 1) //NOT SURE HOW TO DO THIS IN CODEIGNITER ENVIRONMENT //$SQL = "SELECT a.id, a.invdate, b.name, a.amount,a.tax,a.total,a.note FROM invheader a, clients b WHERE a.client_id=b.client_id ORDER BY $sidx $sord LIMIT $start , $limit"; //$result = mysql_query( $SQL ) or die("Couldn t execute query.".mysql_error()); $result = $query->result_array(); $responce->page = $page; $responce->total = $total_pages; $responce->records = $count; $i=0; foreach ($result as $myrow){ $responce->rows[$i]['id']=$myrow['id']; $responce->rows[$i]['cell']=array($myrow['id'],$myrow['firstname'],$myrow['lastname'],$myrow['amount'],$myrow['status']); $i++; } echo json_encode($responce); } With the above code, the grid is populating and its is working to a greater extent. Only thing is the pagination stuff not working properly in that the data on page one shows when I move to page 2, 3, etc.
Her is the final working thing. For anyone who may need it. public function applicantdata(){ $page = $this->input->get('page');// get the requested page $limit = $this->input->get('rows');// get how many rows we want to have into the grid $sidx = $this->input->get('sidx');// get index row - i.e. user click to sort $sord = $this->input->get('sord');// get the direction if(!$sidx){ $sidx =1; } $this->db->select('firstname'); $this->db->from('applicant'); $this->db->join('transaction', 'transaction.applicant_id = applicant.id'); $query = $this->db->get(); $count = $query->num_rows(); if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } if ($page > $total_pages){ $page=$total_pages; } $start = $limit*$page - $limit; // do not put $limit*($page - 1) $this->db->select('*'); $this->db->from('applicant'); $this->db->join('transaction', 'transaction.applicant_id = applicant.id'); $this->db->order_by("applicant.id", $sord); $this->db->limit($limit, $start); $query2 = $this->db->get(); $result = $query2->result_array(); $responce->page = $page; $responce->total = $total_pages; $responce->records = $count; $i=0; foreach ($result as $myrow){ $responce->rows[$i]['id']=$myrow['id']; $responce->rows[$i]['cell']=array($myrow['id'],$myrow['firstname'],$myrow['lastname'],$myrow['amount'],$myrow['status']); $i++; } echo json_encode($responce); }
jQgrid wrong pager structure
Im trying to parse pager data and grid data through a json response into a jqgrid. Here is the php file that creates a json file wich contains all these data (pager+grid data) if(!$sidx) $sidx =1; $result = mysql_query("SELECT COUNT(*) AS count FROM logs"); $row = mysql_fetch_array($result); $count = $row['count']; if( $count >0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } if ($page > $total_pages) $page=$total_pages; $start = $limit*$page - $limit; $pages_array = array("page"=> $page , "total" => $total_pages , "records" => $count); array_push($return_array,$pages_array); // Getting data for jqGrid table $data = mysql_query("SELECT * FROM logs ORDER BY log_id DESC"); if(mysql_num_rows($data)) { while($row = mysql_fetch_array($data)) { $row_array['rows']['log_id'] = $row['log_id']; $row_array['rows']['ip'] = $row['ip']; $row_array['rows']['hostname'] = $row['host']; $row_array['rows']['log'] = $row['input']; $row_array['rows']['date'] = $row['date']; array_push($return_array,$row_array); } } echo json_encode($return_array); With this php code i retrieve a json file like this : [{"page":"1","total":2,"records":"34"}, {"rows":{"log_id":"108","ip":"127.0.0.1","hostname":"","log":"having 1=1--","date":"09-06-2013 22:05:57"}}, {"rows":{"log_id":"107","ip":"127.0.0.1","hostname":"","log":"\/\/","date":"09-06-2013 22:05:57"}}, {"rows":{"log_id":"106","ip":"127.0.0.1","hostname":"","log":"**/","date":"09-06-2013 22:05:55"}}, {"rows":{"log_id":"105","ip":"127.0.0.1","hostname":"","log":"+and+","date":"09-06-2013 22:05:55"}}] But this is wrong structure. In a post here in stackoverflow user Musa said that the structure must be like this : { "page": 2,//current page "total": 2,//number of pages "records": 11,//# of records in total "rows": [//array of data { "id": "101",//id for this row of data "cell": [ "101", "Sundale", "OTTP", "652", "6", "65", "656665", "986346654", "823343454", "554332" ] } ] } Can someone help me fix my code so the structure that json response be right ? Thanks for any help!
So finally i solve this problrm using php's stdClass to load the data and here is the code: init_mysql(); $response = new stdClass(); // Getting pages number (for jqGrid pager) if(!$sidx) $sidx =1; $result = mysql_query("SELECT COUNT(*) AS count FROM logs"); $row = mysql_fetch_array($result); $count = $row['count']; if( $count >0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } if ($page > $total_pages) $page=$total_pages; $start = $limit*$page - $limit; $response->page = $page; $response->total = $total_pages; $response->records = $count; // Getting data for jqGrid table $data = mysql_query("SELECT * FROM logs ORDER BY log_id DESC"); $i = 0; if(mysql_num_rows($data)) { while($row = mysql_fetch_array($data)) { $response->rows[$i]['id']=$i+1; $response->rows[$i]['cell']=array('log_id'=>$row['log_id'],'ip'=>$row['ip'],'hostname'=>$row['host'],'log'=>$row['input'],'date'=>$row['date']); $i++; } } echo json_encode($response);