gridview buttonOptions problem

Hi,

I have this piece of code, in my view, and i have some problem retrieving the primarykey as a paramter.




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

        'id'=>'projecten-grid',

        'dataProvider'=>$model->search(),

        'filter'=>$model,

        'columns'=>array(

                'id',

                'userid',

                'project_code',

                'name',

                'description',

                'visible',

                array(

                        'class'=>'CButtonColumn',

                        'updateButtonOptions'=>array('title'=>'',

                                'ajax'=>array(

                                        'update'=>'dataform',

                                        'url'=>Yii::app()->createUrl("update",array("id"=>$data->id)), // here $data->id is null

                                        'dataType'=>'html',

                                        'type'=>'POST',

                                ),

                        ),

                        'updateButtonUrl'=>'$data->id', // here the id is correct

                ),

        ),

)); ?>



anybody know why is this happening? or i have to use an other approach to retrieve the primarykey?

thanx.

The difference is that in the on that works you have ‘$data->id’ (with quotes), CGrid evals this parameter with the grid data. And in your ajax statement it is the value of the current page.

try to put everything under quotes but im not sure it will work:

‘url’=>‘Yii::app()->createUrl(“update”,array(“id”=>$data->id))’

the problem is if i put everything between quotes, then the generated ajax looks like this:

jQuery(’#yt1’).click(function(){jQuery.ajax({‘url’:‘Yii::app()->createUrl(\“update\”,array(\“id\”=>$data->id))’,‘cache’:false});return false;});

the problem is in the ajax level… $data->id is NULL, and if im using as a string then it would look like $data->id instead of the actual id.

Im trying to solve this problem for a while but i couldnt figure it out so far :S

any other idea?

The value in ‘updateButtonOptions’ is treated as static value, not PHP expression. Therefore, your approaches won’t work.

Because each button (link) already has a ‘href’ attribute which contains the URL you want, you could drop ‘ajax’ option and write the following js code instead to achieve your goal:




$('...selector for buttons..').click(function(){

    var url=$(this).attr('href');

    $.ajax({

         url: url

    });

});



but if i use this aproach, my url will be define, and when there is an onclick event triggered, then it wont be an ajax action anymore, but an actual page reload, isnt it? (of course the aja action will be also executed)

You can "return false;" in the click handler.

what do you mean click handler? for the javascript stuff? But if i make that as a false, then it wont be an ajax action. And i want just ajax action instead of reload the page

My interpretation is




$('...selector for buttons..').click(function(){

    var url=$(this).attr('href');

    $.ajax({

         url: url

    });

    return false;

});



/Tommy

Thanks =) it seems to work. Finally =)