En este tema les quiero mostrar como realizar búsqueda entre fechas en el GridView, en este caso voy a usar la tabla auditoria y el campo fecha_auditoria va a tener la opción para filtrar entre fechas.
Modelo Auditoria (Las modificaciones que se deben hacer en el modelo):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?php class Auditoria extends CActiveRecord { public $date_first ; //Agregar esta variable public $date_last ; //Agregar esta variable //Agregar date_first, date_last en el search en la función rules() public function rules() { return array ( array ( 'fecha_auditoria' , 'required' ), array ( 'usuario, modelo, accion' , 'length' , 'max' =>50), array ( 'campo, camponuevovalor, campoviejovalor' , 'safe' ), array ( 'date_first, date_last,id_auditoria, usuario, modelo, accion, fecha_auditoria, campo, camponuevovalor,campoviejovalor' , 'safe' , 'on' => 'search' ), ); } //Antes del return agregar lo siguiente en el search(), cabe recalcar que fecha_auditoria es el campo que utilizo en este ejemplo public function search() { //Esto es lo que se debe agregar if ((isset( $this ->date_first) && trim( $this ->date_first) != "" ) && (isset( $this ->date_last) && trim( $this ->date_last) != "" )) $criteria ->addBetweenCondition( 'fecha_auditoria' , '' . $this ->date_first. '' , '' . $this ->date_last. '' ); return new CActiveDataProvider( $this , array ( 'criteria' => $criteria , )); } |
Vista auditoria/admin.php (Las modificaciones que se deben hacer en la vista admin):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | //Esto se debe agregar antes del código del GridView <?php // this is the date picker $dateisOn = $this ->widget( 'zii.widgets.jui.CJuiDatePicker' , array ( /*'model'=>$model, 'attribute' => 'date_first',*/ 'name' => 'Auditoria[date_first]' , 'value' => $model ->date_first, 'language' => 'es' , 'options' => array ( 'showAnim' => 'fold' , 'dateFormat' => 'yy-mm-dd' , 'changeMonth' => 'true' , 'changeYear' => 'true' , 'constrainInput' => 'false' , ), 'htmlOptions' => array ( 'style' => 'height:20px;width:70px;' , ), ),true) . '<br> a <br> ' . $this ->widget( 'zii.widgets.jui.CJuiDatePicker' , array ( /*'model'=>$model, 'attribute' => 'date_last',*/ 'name' => 'Auditoria[date_last]' , 'value' => $model ->date_last, 'language' => 'es' , 'options' => array ( 'showAnim' => 'fold' , 'dateFormat' => 'yy-mm-dd' , 'changeMonth' => 'true' , 'changeYear' => 'true' , 'constrainInput' => 'false' , ), 'htmlOptions' => array ( 'style' => 'height:20px;width:70px' , ), ),true); ?> //Esto se debe agregar en el código del GridView 'id' => 'auditoria-grid' , 'type' => 'striped bordered' , 'dataProvider' => $model ->search(), 'afterAjaxUpdate' =>" function () { //Esto es lo que hay que agregar jQuery( '#Auditoria_date_first' ).datepicker(jQuery.extend({showMonthAfterYear:false}, jQuery.datepicker.regional[ 'es' ], { 'showAnim' : 'fold' , 'dateFormat' : 'yy-mm-dd' , 'changeMonth' : 'true' , 'showButtonPanel' : 'true' , 'changeYear' : 'true' , 'constrainInput' : 'false' })); jQuery( '#Auditoria_date_last' ).datepicker(jQuery.extend({showMonthAfterYear:false}, jQuery.datepicker.regional[ 'es' ], { 'showAnim' : 'fold' , 'dateFormat' : 'yy-mm-dd' , 'changeMonth' : 'true' , 'showButtonPanel' : 'true' , 'changeYear' : 'true' , 'constrainInput' : 'false' })); }", 'filter' => $model , //luego se debe cambiar 'fecha_auditoria', por este codigo: array ( 'name' => 'fecha_auditoria' , 'filter' => $dateisOn , 'value' => '$data->fecha_auditoria' ), |
Haciendo esas modificaciones ya van a tener 100% funcional la búsqueda entre fechas, para que pueden utilizar estos códigos nada más deben de adaptarle al modelo y vista admin que deseen, básicamente lo que deben de cambiar es la fecha_auditoria por el campo que deseen, también deben de cambiar (Auditoria[date_first] , Auditoria[date_last] , #Auditoria_date_first , #Auditoria_date_last) por el nombre de su modelo, eso en la vista admin.