Yii Framework Forum: gridview buttonOptions problem - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

gridview buttonOptions problem Rate Topic: -----

#1 User is offline   JMC 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 16-December 09
  • Location:Nederlands, Den Haag

Posted 17 February 2010 - 09:30 AM

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.
I bring the sunshine baby... wherever I go =)
0

#2 User is offline   yiimann 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 102
  • Joined: 19-December 09

Posted 17 February 2010 - 02:45 PM

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))'
0

#3 User is offline   JMC 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 16-December 09
  • Location:Nederlands, Den Haag

Posted 17 February 2010 - 03:58 PM

View Postyiimann, on 17 February 2010 - 02:45 PM, said:

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?
I bring the sunshine baby... wherever I go =)
0

#4 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,892
  • Joined: 04-October 08
  • Location:DC, USA

Posted 17 February 2010 - 05:46 PM

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
    });
});

0

#5 User is offline   JMC 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 16-December 09
  • Location:Nederlands, Den Haag

Posted 17 February 2010 - 06:06 PM

View Postqiang, on 17 February 2010 - 05:46 PM, said:

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)
I bring the sunshine baby... wherever I go =)
0

#6 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,892
  • Joined: 04-October 08
  • Location:DC, USA

Posted 17 February 2010 - 07:51 PM

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

#7 User is offline   JMC 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 16-December 09
  • Location:Nederlands, Den Haag

Posted 18 February 2010 - 05:13 AM

View Postqiang, on 17 February 2010 - 07:51 PM, said:

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
I bring the sunshine baby... wherever I go =)
0

#8 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 18 February 2010 - 06:10 AM

View PostJMC, on 18 February 2010 - 05:13 AM, said:

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
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#9 User is offline   JMC 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 16-December 09
  • Location:Nederlands, Den Haag

Posted 18 February 2010 - 08:51 AM

View Posttri, on 18 February 2010 - 06:10 AM, said:

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 =)
I bring the sunshine baby... wherever I go =)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users