Msjqgrid
De KumbiaPHP Framework Wiki
Revisión del 01:02 20 feb 2014 de Caseros2001 (discusión | contribuciones)
Sumario
JQUERY DATAGRID.
Helper
<?php
/**
* Helpers
* PHP version 5
* LICENSE
*
* This source file is subject to the GNU/GPL that is bundled
* with this package in the file docs/LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to emiliano@markisich.com.ar so we can send you a copy immediately.
*
* @author Markisich Emiliano <emiliano@markisich.com.ar>
* (este es mi primer aporte al SoftWare Libre)
*/
/**
* Libreria Mostrar datos en una grilla
* es una adaptacion de jqGrid a kumbia.
* Version 0.1
*/
class msjqGrid{
public $contador = array();
/**
* titulo del datagrid
* @var string
*/
public $title_caption = '';
/**
* Si desea que poder obtener un order by por campo
* para esto debe recibir el field y el order
* default = false
* @var bool
*/
public $formatOption = array();
/**
* Si desea que poder obtener un order by por campo
* para esto debe recibir el field y el order
* default = false
* @var bool
*/
public $key = array();
/**
* Si desea que poder obtener un order by por campo
* para esto debe recibir el field y el order
* default = false
* @var bool
*/
public $order_fields = false;
/**
* Array de datos q se obtienen de un find
* @var array
*/
public $data_source = array();
/**
* url: url para la accion que efectua la paginacion,
* por defecto "module/controller/page/" y se envia por parametro el numero de pagina.
* por defecto busca en el mismo controller y action
* @var string
*/
public $url = '';
public $get_fields_and_headers = array();
/**
* Modelo actual
* @var string
*/
public $model = '';
/**
* campos que se van a mostrar de la base de datos
* @var array
*/
public $fields = array();
/**
* titulos o cabezeras que se mostra en la table <th></th>
* @var array
*/
public $headers = array();
/**
*Cambia el titulo a mostrar según la key del array $data.
*/
public function setFormatoption($field, $formato){
$this->formatOption = array($field => $formato);
}
public function getformatOption($field){
return @$this->formatOption[$field];
}
public function setkey($field, $formato){
$this->key = array($field => $formato);
}
public function getkey($field){
return @$this->key[$field];
}
public function __construct($datos) {
$this->data_source = $datos;
$this->url = Router::get('controller') . '/'; /*. Router::get('action');*/
//$this->headers = "kjgh";
// if($this->existData()){
// $this
// }
}
public function xml(){
$code = '<Listado>';
$code .= '<Request>';
$code .= '<IsValid>True</IsValid>';
$code .= '<ItemSearchRequest>';
$code .= '<SearchIndex>'."Titulo" .'</SearchIndex>';
$code .= '</ItemSearchRequest>';
$code .= '</Request>';
$code .= '<Items>';
foreach ($this->data_source as $item):
$code .= '<Item>';
$code .= '<DetailPageURL></DetailPageURL>';
$code .= '<ItemAttributes>';
foreach ($this->fields as $campo):
$var = $item->$campo;
$code .="<$campo> $var </$campo>";
endforeach;
$code .= '</ItemAttributes>';
$code .= '</Item>';
endforeach;
$code .= '</Items>';
$code .= '</Listado>';
return $code;
}
}
?>
parcial
<?php echo Tag::css('jqGrid-4.1.1/css/ui.jqgrid'); ?>
<?php echo Tag::css('jqGrid-4.1.1/css/ui.multiselect'); ?>
<?php echo Tag::js('jquery/jquery-ui-1.8.1.custom.min'); ?>
<?php echo Tag::js('jqGrid/plugins/jquery.layout'); ?>
<?php echo Tag::js('jqGrid/js/i18n/grid.locale-es'); ?>
<?php echo Tag::js('jqGrid/js/jquery.jqGrid.min'); ?>
<?php echo Tag::js('jqGrid/plugins/jquery.tablednd'); ?>
<?php echo Tag::js('jqGrid/plugins/jquery.contextmenu'); ?>
<?php echo Tag::js('jqGrid/plugins/ui.multiselect'); ?>
<?php echo Tag::js('jqGrid/plugins/xmlmap'); ?>
<?php
$model = '';
$names ='';
foreach ($dataGrid->fields as $campo):
$names .= '"'.$campo.'",' ;
$formato = $dataGrid->getformatOption($campo);
$key = $dataGrid->getkey($campo);
$model .= '{name:"'.$campo.'",index:"'.$campo.'", width:120, xmlmap:"ItemAttributes>'.$campo.'", sorttype:"integer", sorttype:"int"'.$key . $formato.'},';
endforeach; ?>
<?php
$datos ="'".'<?xml version="1.0" encoding="UTF-8" ?>' ;
$datos .= $dataGrid->xml() ."'";
?>
<script type="text/javascript">
var datos = <?php echo "$datos"; ?>
jQuery(document).ready(function(){
var lastsel;
jQuery("#list").jqGrid({
//url: "listado",
datastr:datos,
datatype: "xmlstring",
colNames:[<?php echo substr($names, 0,-1);?>],
colModel:[<?php echo substr($model, 0,-1);?>],
height:250, with: 450,
loadonce: true,
xmlReader: { root : "Items", row: "Item", repeatitems: false, id: "ASIN" },
rowNum:10,
rowList:[5,10,20,30],
pager:jQuery('#pagered'), //'#pagered',
sortname: 'id',
viewrecords: true,
sortorder: "desc",
caption: "Listado",
forceFit : true,
cellEdit: false,
cellsubmit: 'clientArray',
multiselect: true,
onSelectRow: function(rowid){
lastsel = $('#list').getCell(rowid, 'id');
},
ondblClickRow: function(rowid){
lastsel = $('#list').getCell(rowid, 'id');
},
footerrow : true,
userDataOnFooter : true,
altRows : true,
// grouping:true,
// groupingView : {
// groupField : ['debe'],
// groupDataSorted : true
// },
// caption: "Grouping",
groupSummary: true,
// groupDataSorted: true
});
jQuery("#list").jqGrid('navGrid','#pagered',
{edit:false,add:false,del:false},
{},
{},
{},
{multipleSearch:true, multipleGroup:false, showQuery: false} );
jQuery("#list").jqGrid('navButtonAdd',"#pagered",{
caption:"",
buttonicon:'ui-icon-plus',
onClickButton: function(){
location.href='/<?php echo $dataGrid->url ?>create/';
},
position:"last"
});
jQuery("#list").jqGrid('navButtonAdd',"#pagered",{
caption:"",
buttonicon:"ui-icon-pencil",
onClickButton: function(){
// var gsr = jQuery("#list").jqGrid('getGridParam','selrow');
// if(gsr){
// jQuery("#list").jqGrid('GridToForm',gsr,"#order");
// } else {
// alert("Seleccione un registro")
// }
if(lastsel){
location.href="/<?php echo $dataGrid->url ?>edit/"+lastsel;
}else{
alert("Seleccione un registro")
}
},
position:"last"
});
jQuery("#list").jqGrid('navButtonAdd',"#pagered",{
caption:"",
buttonicon:"ui-icon-trash",
onClickButton: function(){
/* var gsr = jQuery("#list").jqGrid('getGridParam','selrow');
if(gsr){
jQuery("#list").jqGrid('GridToForm',gsr,"#order");
} else {
alert("Seleccione un registro")
}*/
if(lastsel){
location.href="/<?php echo $dataGrid->url ?>del/"+lastsel;
}else{
alert("Seleccione un registro")
}
},
position:"last"
});
});
function mysum(val, name, record){
return parseFloat(val||0) + parseFloat((record[name]||0));
}
function currencyFmatter (cellvalue, options, rowObject){
return "$"+cellvalue;
};
// $("#list").jqGrid('bindKeys', {"onEnter":function( rowid ) { alert("You enter a row with id:"+rowid)} } );
function formatOperations(cellvalue, options, rowObject) {
return "<a href='/<?php echo $dataGrid->url ?>edit/" + cellvalue + "' id='ui-icon-trash'><u>Edit</u></a> "+
"<a <a href='/<?php echo $dataGrid->headers ?>del/" + cellvalue + "'><u>Edit</u></a>";
};
</script>
<div id="grilla" class="ui-layout-west ui-widget ui-widget-content">
<div>
<table id="list"></table>
<div id="pagered" style="height: 40px"></div>
</div>
Vista
<?php View::content()?>
<?php $dataGrid = new msjqgrid($listArticulos); ?>
<?php $dataGrid->fields = array('id' => 'id','codigo' => 'codigo', 'denominacion' => 'denominacion', 'stock' => 'stock', 'precio' => 'precio');?>
<?php //$dataGrid->headers = array('id' => 'ID','codigo' => 'CODIGO', 'denominacion' => 'DESCRIPCION', 'stock' => 'STOCK', 'precio' => 'PRECIO');?>
<?php //$dataGrid->typefields = array('id' => 'integer','codigo' => 'string', 'denominacion' => 'string', 'stock' => 'STOCK', 'precio' => 'PRECIO');?>
<?php $dataGrid->setFormatOption('precio',',formatter:"currency",formatoptions:{decimalSeparator:",", thousandsSeparator: ",", decimalPlaces: 2, prefix: "$ "}');?>
<?php $dataGrid->setkey('id',',key:true');?>
<?php echo View::partial('msjqgrid/msjqgrid' ,false,array('dataGrid'=>$dataGrid)); ?>