Hi all, I'm trying to use jqgrid for my grids, I tested two extensions and they are great, part of my code is from them (I hope the authors don't care), but I like to make it work because I want to learn.
Maybe someone with jquery knowledge (and jqgrid) want to help me with this or give me some examples, here is my code and thanks!
(sorry for me bad English )
<?php $baseUrl = Yii::app()->baseUrl . '/jqGrid'; $files = array(); $subdir = ''; $subfile = ''; $clientScript = Yii::app()->getClientScript(); $clientScript->registerCoreScript('jquery'); $clientScript->registerCssFile($baseUrl.'/css/ui.jqgrid.css'); if (true) { $clientScript->registerScriptFile($baseUrl.'/js/i18n/grid.locale-'.'en'.'.js'); } else { $subdir = 'min/'; $subfile = '-min'; $clientScript->registerScriptFile($baseUrl.'/i18n/min/grid.locale-'.'en'.'js'); } $clientScript->registerScriptFile($baseUrl.'/js/jqModal.js'); $clientScript->registerScriptFile($baseUrl.'/js/jqDnR.js'); $files[] = "{$subdir}grid.base{$subfile}.js"; // jqGrid base $files[] = "{$subdir}grid.common{$subfile}.js"; // jqGrid common for editing $files[] = "{$subdir}grid.formedit{$subfile}.js"; // jqGrid Form editing $files[] = "{$subdir}grid.inlinedit{$subfile}.js"; // jqGrid inline editing $files[] = "{$subdir}grid.celledit{$subfile}.js"; // jqGrid cell editing $files[] = "{$subdir}grid.subgrid{$subfile}.js"; // jqGrid subgrid $files[] = "{$subdir}grid.treegrid{$subfile}.js"; // jqGrid treegrid $files[] = "{$subdir}grid.custom{$subfile}.js"; // jqGrid custom $files[] = "{$subdir}grid.postext{$subfile}.js"; // jqGrid postext $files[] = "{$subdir}grid.setcolumns{$subfile}.js"; // jqGrid setcolumns $files[] = "{$subdir}grid.import{$subfile}.js"; // jqGrid import $files[] = "{$subdir}jquery.fmatter{$subfile}.js"; // jqGrid formater $files[] = "{$subdir}json2{$subfile}.js"; // json utils $files[] = "{$subdir}JsonXml{$subfile}.js"; // xmljson utils $plugins[] = "jquery.contextmenu.js"; // jqGrid table to grid $plugins[] = "jquery.tablednd.js"; // jqGrid table to grid foreach ($files as $file) { $clientScript->registerScriptFile($baseUrl.'/js/'.$file); } foreach ($plugins as $file) { $clientScript->registerScriptFile($baseUrl.'/plugins/'.$file); } ?> <script type="text/javascript"> jQuery(document).ready(function(){ jQuery("#list").jqGrid({ url:'/iae/index.php?r=test/example', dataType:'json', mtype: 'GET', colNames:['id','Nivel', 'Curso','Nombre','Archivo'], colModel :[ {name:'id', index:'t.id', width:65}, {name:'nivel', index:'nivel', width:100}, {name:'curso', index:'curso', width:100}, {name:'nombre', index:'t.nombre', width:100}, {name:'archivo', index:'archivo', width:100} ], pager: jQuery('#pager'), rowNum:10, rowList:[10,20,30], sortname: 't.id', sortorder: "desc", viewrecords: true, imgpath: '/iae/jqGrid/themes/redmond/images/', caption: 'My first grid' }); }); </script> <table id="list" class="scroll" cellpadding="0" cellspacing="0"></table> <div id="pager" class="scroll" style="text-align:center;"></div>
Y acá está la acción que devuelve los datos:
<?php public function actionExample(){ $page = $_REQUEST['page']; $limit = $_REQUEST['rows']; $sidx = $_REQUEST['sidx']; $sord = $_REQUEST['sord']; if(!$sidx) $sidx =1; $command = yii::app()->db->createCommand("SELECT COUNT(*) AS count FROM trabajo"); $count = $command->queryScalar(); 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) if($start <0) $start = 0; $SQL = "SELECT id, nombre, archivo from trabajo t ORDER BY $sidx $sord LIMIT $start , $limit"; $result = yii::app()->db->createCommand($SQL )->query() or die("Couldn t execute query."); // constructing a JSON $responce->page = $page; $responce->total = $total_pages; $responce->records = $count; $i=0; while($row = $result->read()) { $responce->rows[$i]['id']=$row['id']; $responce->rows[$i]['cell']=array($row['id'],$row['nombre'],$row['archivo']); $i++; } echo json_encode($responce); } } ?>