Hi. I’m trying to do a Yii component. I need to extend the CGridView, disable change.yiiGridView keydown.yiiGridView and launch the search throught a button:
The gridview:
<?php
$this->widget('application.components.GridViewExtended',
array(
'id'=>get_class($model).'-grid',
'dataProvider'=>$model->search(),
'ajaxUpdate'=>true,
'filter'=>$model,
'columns'=>array(
array(
'name' => 'Name',
'value' => '$data->Name',
'headerHtmlOptions'=>array('style'=>'width:130px;'),
),
array(
'name'=>'',
'type'=>'raw',
'filter'=>'<button class="updateGridButtonSelector"><span class="icon-magnifier"></span></button>',
'value'=>'',
'htmlOptions' => array('class'=>'searchgridview'),
'headerHtmlOptions'=>array('style'=>'width:25px;')
),
),
)
);
?>
The component:
<?php
Yii::import('zii.widgets.grid.CGridView');
class GridViewExtended extends CGridView {
public $selector;
public function init(){
$this->selector = '#' . $this->getId();
$this->afterAjaxUpdate = 'function(id,data){
$("' . $this->selector . '").gridviewExtended();
}';
parent::init();
$this->registerClientScript();
}
public function run(){
parent::run();
$this->registerScript( $this->selector );
}
public function registerClientScript(){
parent::registerClientScript();
Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/js/widgets/gridview.js?'.Yii::app()->params['versionApp'], CClientScript::POS_END);
}
public function registerScript($selector, $options = array()){
Yii::app()->clientScript->registerScript(uniqid(__CLASS__ . '#', true),
'$("' . $selector . '").gridviewExtended();'
);
}
}
?>
The JS plugin
(function( $ ) {
// Plugin definition.
$.fn.gridviewExtended = function( options ) {
enableButtonSearch( this );
// ...
};
// Private function for debugging.
function enableButtonSearch( obj ) {
$(document).off('change.yiiGridView keydown.yiiGridView');
$('body').on('click','#'+$(obj).attr('id')+' .updateGridButtonSelector', function(e) {
$.fn.yiiGridView.update($(obj).attr('id'));
});
};
})( jQuery );
The controller
public function actionAdmin(){
$model=new Usuarios('search');
$model->unsetAttributes();
if(isset($_GET['Usuarios']))
$model->attributes=$_GET['Usuarios'];
$modelName = get_class( $model );
if( isset( $_GET["ajax"] ) && $_GET["ajax"]== $modelName."-grid" ){
$this->renderPartial('_list',array(
'model'=>$model,false,false
));
}
else{
$this->render('admin',array(
'model'=>$model,
));
}
}
This code works fine, but when I click the search button, the first time actionAdmin is called 1 time.
The second time actionAdmin is called 2 times. The third time actionAdmin is called 4 times…
Then 8 times, 16 times, 32 times…
Why the searchbutton is executing many times de actionAdmin?
Thank you.