CGridView Ajax Problem

Hi all,

I just have started to work with Yii and I like the framework very much.

By now I have finished my Project to 99% but now I figured out a problem. After hours of searching and found some help, I just cannot figure out how to fix the problem.

I hope you could help me finishing my Problem.

Problem:

I have a GridView with a CButtonColumn with Ajax Buttons. They GridView uses Pagination and on the first page everything works fine but on all the other pages the ajax requests won’t work. I looked close at the Ajax-Response and the Javascript for the “new” Buttons is rendered fine but the Buttons won’t work. In many posts I read about the problem but could not figure out how to solve the issue.

I can reproduce the problem in 2 different situations. The 1st: a Gridview loaded via a non-Ajax request and after Pagination the Buttons won’t work. The 2nd: A Gridview is loaded in a Jui-Dialog and the Buttons work only on the first page.

Here is the code for the Gridview in the 1st Situation:




$this->widget('zii.widgets.grid.CGridView', array(

        'id'=>'grid-test',

        'ajaxUpdate'=>'Screenigs',

        'ajaxUrl' => $this->createUrl('/teacher/showScreenings',array('own'=>$own)),

        'dataProvider'=>$data_provider,  //model-searchfunction

        'filter'=>$Screening,

        'template'=>'{summary}{items}{pager}',

        'filterPosition'=>'footer',

        'emptyText'=>'Keine Screenings vorhanden', 

        'selectableRows'=>'0',

        'loadingCssClass'=>'loading',

                'columns'=>array(

                            array(

                                'name' =>'title',

                                'header' =>'Titel',

                            ), 

                            array(

                                'name' =>'grade',

                                'header' =>'Schulstufe',

                            ), 


                            array(

                                'name' =>'AuthorName',

                            ), 

                            array(

                                'name' =>'CreateDate',

                                'header' =>'Erstellt am',

                            ),

                           array

                                (

                                    'class'=>'CButtonColumn',

                                    'template'=>'{show}{deploy}{edit}{drop}',

                                    'deleteButtonUrl'=>"Yii::app()->createUrl('teacher/DeleteScreening',array('ScreeningID'=>\$data->id,'AuthorID'=>Yii::app()->user->id,

                                    'return_to_own'=>$own))",

                                    'buttons'=>array

                                    (

                                        'show' => array

                                        (

                                            'label'=>'Screening anzeigen',

                                            'imageUrl'=>Yii::app()->request->baseUrl.'/images/view.png',

                                            'url'=>"Yii::app()->createUrl('/teacher/viewScreening',array('id'=>\$data->id,'return_to_own'=>$own))",

                                            'visible'=>'true',

                                            'options'=>array('id'=>'view-button-'. uniqid(),

                                                             'title'=>'Anzeigen',

                                                             'ajax'=>array(

                                                                            'type'=>'GET',

                                                                            'url'=>"js:$(this).attr('href')",

                                                                            'update'=>'#ViewDialog',

                                                                            'beforeSend' => 'function(){$("#content").addClass("loading");}',

                                                                            'complete' => 'function(){ $("#content").removeClass("loading");}',   

                                                             ),

                                                ),

                                        ),

                                         'deploy' => array

                                        (

                                            'label'=>'deploy',

                                            'url'=>"Yii::app()->createUrl('/teacher/distributeScreening',array('ScreeningID'=>\$data->id,'return_to_own'=>$own))",

                                            'imageUrl'=>Yii::app()->request->baseUrl.'/images/deploy.png',

                                            'visible'=>'true',

                                            'options'=>array('id'=>'deploy-button-'. uniqid(),

                                                             'title'=>'Bereitstellen',

                                                             'ajax'=>array(

                                                                            'type'=>'GET',

                                                                            'url'=>"js:$(this).attr('href')",

                                                                            'update'=>'#DistDialog',

                                                                            'beforeSend' => 'function(){$("#content").addClass("loading");}',

                                                                            'complete' => 'function(){ $("#content").removeClass("loading");}',   

                                                             ),

                                             ),

                                        ),

                                        'edit' => array

                                        (

                                            'label'=>'edit',

                                            'url'=>"Yii::app()->createUrl('teacher/UpdateScreening',array('id'=>\$data->id,'return_to_own'=>$own))",

                                            'imageUrl'=>Yii::app()->request->baseUrl.'/images/update.png',

                                            'visible'=>'(Yii::app()->user->id == $data->AuthorID)',

                                            'options'=>array('id'=>'edit-button-'.  uniqid(),

                                                            'title'=>'Bearbeiten'),

                                        ),

                                        'drop' => array

                                        (

                                            'label'=>'drop',

                                            'visible'=>'(Yii::app()->user->id == $data->AuthorID)',

                                            'url'=>"Yii::app()->createUrl('teacher/DeleteScreening',array('ScreeningID'=>\$data->id,'AuthorID'=>Yii::app()->user->id,

                                                        'return_to_own'=>$own))",

                                            'click'=>'function(){var confirmed = confirm("Wirklich?"); return confirmed;}', 

                                            'imageUrl'=>Yii::app()->request->baseUrl.'/images/delete.png',

                                             'options'=>array('id'=>'del-button-'.  uniqid(),

                                                            'title'=>'Löschen'),

                                            

                                        )

                                        

                                    ),

                                ),

        ),

    ));



I am using Yii 1.1.12.

Thank you for your help! And sorry I know that this question was asked several times, but i could not figure out how to solve the Problem…

regards, Paul

Hi

Have you tried jquery’s Event Delegation?

Have a look at this wiki:

CGridView Ajax

Check the topic:

The Ajax (extracting data from CGridView and passing it to controller)

I hope it helps.

Hi,

Thanks for the Wiki link.

I will take a look into it but I figured out a solution.

I changed the JS. Now it binds through the class of the links and this works.

This is what I did.




Yii::app()->clientScript->registerScript('show', "

$(document).on('click','#grid-test a.show',function() {

        jQuery.ajax({

                        'type':'GET',

                        'url':$(this).attr('href'),

                        'beforeSend':function(){\$(\"#content\").addClass(\"loading\");},

                        'complete':function(){ \$(\"#content\").removeClass(\"loading\");},

                        'cache':false,'data':jQuery(this).parents(\"form\").serialize(),

                        'success':function(html){jQuery(\"#ViewDialog\").html(html)}});

                        return false;});

");