Ver Mensaje Individual
  #26 (permalink)  
Antiguo 13/11/2012, 08:14
Avatar de HiToGoRoShi
HiToGoRoShi
 
Fecha de Ingreso: abril-2008
Mensajes: 849
Antigüedad: 16 años
Puntos: 31
Respuesta: Clase - Data Grid

Yo también hace tiempo desarrolle un datagrid, pero lo llame gridview.

Una desventaja que yo le veo a tu grilla que es muy dependiente de una consulta SQL, no seria mejor pasarle un datasource? ... así este solo se encargaría de mostrar la grilla sin importar de donde viene (mysql,oracle,mssql,xml,csv,txt,etc)

Adjunto mi grilla

Código PHP:
Ver original
  1. <?php
  2.     class GridView{
  3.         private $_ds;
  4.         private $_name;
  5.         private $_ClassName = '';
  6.        
  7.         private $_columns = array();
  8.         private $_TColumns = array();
  9.         private $_WColumns = array();
  10.        
  11.         private $_fields  = array();
  12.         private $_actions = array();
  13.         private $_idAction = '';
  14.        
  15.         private $_rows = 0;
  16.         private $_pager = 0;
  17.         private $_tPager = 0;
  18.         private $_pRequest = '';
  19.        
  20.         private $_cRow = 1;
  21.         private $_RPage;
  22.        
  23.         public function setName($name){
  24.             $this->_name = $name;
  25.         }
  26.        
  27.         public function setClassName($cn){
  28.             $this->_ClassName = $cn;
  29.         }
  30.  
  31.         public function setDataSource($ds){
  32.             $this->_ds = $ds;
  33.         }
  34.        
  35.         public function setColumns($cols){
  36.             $this->_columns = explode(',',$cols);
  37.         }
  38.        
  39.         public function setTColumns($cols){
  40.             $this->_TColumns = explode(',',$cols);
  41.         }
  42.        
  43.         public function setWColumns($cols){
  44.             $this->_WColumns = explode(',',$cols);
  45.         }
  46.        
  47.         public function setFields($fl){
  48.             $this->_fields = explode(',',$fl);
  49.         }
  50.        
  51.         public function setActions($actions){
  52.             if(count($this->_columns)>0)
  53.                 array_unshift($this->_columns, '&nbsp;');
  54.             array_unshift($this->_fields, 'x');
  55.             $this->_actions = explode(',',$actions);
  56.         }
  57.        
  58.         public function setIdAction($id){
  59.             $this->_idAction = explode(',',$id);
  60.         }
  61.        
  62.         public function setRowsAndPager($rw,$pg){
  63.             $this->_rows = $rw;
  64.             $this->_pager = $pg;
  65.             /*La cantidad de paginas que hay en total*/
  66.             $this->_tPager = (($this->_rows%$this->_pager)==0) ? (int)($this->_rows/$this->_pager) : (int)($this->_rows/$this->_pager) + 1;
  67.         }
  68.        
  69.         public function setPRequest($rq){
  70.             $this->_pRequest = $rq;
  71.         }
  72.        
  73.         public function setCRow($rw){
  74.             $this->_cRow = $rw;
  75.         }
  76.        
  77.         public function setRefreshPage($rp){
  78.             $this->_RPage = base_url('index.php/' . $rp);
  79.         }
  80.        
  81.         public function render(){
  82.             $tb  = '<table id="' . $this->_name . '" class="gridview UserGridView ' . $this->_ClassName . '">';
  83.             $tb .= '{header}{rows}{pager}';
  84.             $tb .= '</table>';
  85.            
  86.             /*Header*/
  87.             $th = ''; $t = ''; $w = 0;
  88.             foreach($this->_columns as $k => $col){
  89.                 /*Ancho de cabezera*/
  90.                 $w = ($this->_WColumns[$k]>0) ? 'style="width: ' . $this->_WColumns[$k] . 'px;" ' : '';
  91.                 /*Dejamos un espacio para la columna de acciones*/
  92.                 $th .= "<th $t $w>$col</th>";
  93.                 if($k == count($this->_columns)){
  94.                     $th .= "<tr>$th</tr>";                 
  95.                 }
  96.             }
  97.            
  98.             /*Filas*/
  99.             $td = '';
  100.            
  101.             foreach($this->_ds as $x => $r){
  102.                 $alter = ($x%2) ? 'class="alter"' : '';
  103.                 $td .= "<tr $alter>";
  104.                 foreach($this->_fields as $k => $col){
  105.                     $t = ($this->_TColumns[$k]<>'x') ? 'class="' . $this->_TColumns[$k] . '" ' : '';
  106.                     /*Ancho de cabezera*/
  107.                     $w = ($this->_WColumns[$k]>0) ? 'style="width: ' . $this->_WColumns[$k] . 'px;" ' : '';
  108.                     if($col=='x'){
  109.                         $td .= '<td ' . $w . ' class="actions">';
  110.                         foreach($this->_actions as $at){
  111.                             $ids = "";
  112.                             foreach($this->_idAction as $k => $id)
  113.                                 ($k < count($this->_idAction)-1) ? $ids .= $r->$id . '|' : $ids .= $r->$id . '';
  114.                             switch($at){
  115.                                 case 'select':
  116.                                     $td .= '<input type="image" src="' . base_url() . 'assets/css/images/_select.png" class="gv_select" value="' . $ids . '" />';
  117.                                     break;
  118.                                 case 'search':
  119.                                     $td .= '<input type="image" src="' . base_url() . 'assets/css/images/_search2.png" class="gv_search" value="' . $ids . '" />';
  120.                                     break;
  121.                                 case 'delete':
  122.                                     $td .= '<input type="image" src="' . base_url() . 'assets/css/images/_delete.png" class="gv_del" value="' . $ids . '" />';
  123.                                     break;
  124.                             }
  125.                         }
  126.                         $td .= '</td>';
  127.                     }else{
  128.                         $td .= "<td $w $t>" . $r->$col . "</td>";
  129.                     }
  130.                 }
  131.                 $td .= '</tr>';
  132.             }
  133.            
  134.             /*Pager*/
  135.             $pg = '';
  136.             if($this->_rows > 0){
  137.                 /*Paginas en total*/
  138.                 $pg .= '<tr class="pager">
  139.                     <td colspan="' . count($this->_columns) . '">';
  140.                 $pg .= img(array('src'=>'assets/css/images/_loading.gif','class'=>'loading','title'=>'Cargando sea paciente, en caso de demorar demasiado consulte con su proveedor.'));
  141.                 $pg .= '<input class="gv_pager" type="hidden" value="' . $this->_pager . '" />
  142.                         <input class="gv_tpager" type="hidden" value="' . $this->_tPager . '" />
  143.                         <input class="gv_current" type="hidden" value="' . $this->_cRow . '" />
  144.                         <input class="gv_refresh" type="hidden" value="' . $this->_RPage . '" />';
  145.                 /*Desabilitamos los botones de las paginas anteriores segun la condicion*/
  146.                 if($this->_cRow > 1){
  147.                     $pg .= '<button value="1">Inicio</button>';
  148.                     $pg .= '<button value="' . ($this->_cRow - 1) . '">&lt;</button>'; 
  149.                 }else{
  150.                     $pg .= '<button value="1" disabled="disabled">Inicio</button>';
  151.                     $pg .= '<button value="1" disabled="disabled">&lt;</button>';
  152.                 }
  153.                 $pg .= '<input class="gv_page" type="text" style="width: 25px; text-align: center;" value="' . $this->_cRow . '" />';
  154.                 if($this->_cRow < $this->_tPager){
  155.                     $pg .= '<button value="' . ($this->_cRow + 1) . '">&gt;</button>';
  156.                     $pg .= '<button value="' . $this->_tPager . '">Fin</button>';          
  157.                 }else{
  158.                     $pg .= '<button value="0" disabled="disabled">&gt;</button>';
  159.                     $pg .= '<button value="0" disabled="disabled">Fin</button>';   
  160.                 }
  161.                 $pg .=  '<span class="count">Se encontraron ' . count($this->_ds) . ' registros de ' . $this->_tPager . ' paginas en total</span>';
  162.                 $pg .= '</td>
  163.                 </tr>';
  164.             }
  165.            
  166.             $tb = str_replace('{header}',$th,$tb);
  167.             $tb = str_replace('{rows}',$td,$tb);
  168.             $tb = str_replace('{pager}',$pg,$tb);
  169.             return $tb;
  170.         }
  171.     }

Y un ejemplo de como implementarla

Código PHP:
Ver original
  1. /*gridview*/
  2.         $this->gridview->setDataSource($this->objAlmacenDLO->listarStockProductos($p,$w));
  3.         $this->gridview->setColumns('ID,Nombre,stock,Categoria,Marca,UND');
  4.         $this->gridview->setFields('idProducto,nombre,stock,categoria,marca,idUnidadMedida');
  5.         $this->gridview->setActions('select');
  6.         $this->gridview->setIdAction('idProducto');
  7.         $this->gridview->setTColumns('center,x,x,x,x,x,x,x,x,x');
  8.         $this->gridview->setWColumns('55,70,x,65,90,65,100,100,55,100');
  9.         $this->gridview->setRowsAndPager($this->objAlmacenDLO->totalStockProductos($w),15);
  10.         $this->gridview->setRefreshPage('almacen/stock');
  11.         $this->gridview->setCRow($c);

A lo mejor podríamos hacer una mejor grilla entre los GURU, y mas controles como lo haria en webforms ASP.NET jojojojo

EDIT: no usen mi codigo, me acabo de dar cuenta que el mensaje es del 2009 ... yo publique el codigo para darle una idea de como lo implemente yo, pero para que esto funcione hace falta jquery, y usar codeigniter... saludos

Última edición por HiToGoRoShi; 13/11/2012 a las 08:23