4

Is the any way, to access model located in $data variable from CButtonColumn? Below code is not working.

array(
'class' => 'CButtonColumn',
'template' => '{test}',
    'buttons' => array(
        'test' => array(
            'label' => 'Select',
            'click' => 'js:function() { <b>alert($data->_id);</b> return false;}',
        ),
    ),
),
Kai
  • 38,985
  • 14
  • 88
  • 103
gryfi
  • 61
  • 1
  • 3
  • So you are outputting the PHP $data->id variable into the onClick JS? I'm not sure that will work. The $data variable is only only available when the 'click' string is eval()'d by Yii, and I think the js: perfix stops it from being eval()'d and just outputs it as a string? Hmmm – thaddeusmt Apr 06 '11 at 16:46
  • Have you got solved this? otherwise look at my answer. – Jigar7521 May 02 '17 at 12:27

5 Answers5

5

The only field where $data is allowed in CButtonColumn class is url, imageUrl and visible. To pass id to the javascript click event you may place such id in the url and grab it from the DOM. This is very rude hack but easy implementation.

array(
    'class' => 'CButtonColumn',
    'template' => '{test}',
    'buttons' => array(
        'test' => array(
            'label' => 'Select',
             /* set id */
            'url' => $data->id, 
             /* retrieve id from this DOM element (jQuery) */
            'click' => 'function() { alert( $(this).attr("href"); return false;}',
        ),
    ),
),

If you are looking for a more clear coding you could work in CDataColumn class

nanocult
  • 104
  • 1
  • 4
5

It is possible to access visible attributes from jquery:

'click'=>'js:function(){alert("first element in cgridview is"+$(this).parent().parent().children(":nth-child(1)").text());}'
animuson
  • 53,861
  • 28
  • 137
  • 147
chris
  • 51
  • 1
  • 2
1

It looks like _id is a private variable (according to Yii's coding "standards"). You can not access private variables (and methods) outside of an object. Create a getter-method like this in your model:

public function getId() {
  return $this->_id;
}

and then change your code to:

array(
    'class' => 'CButtonColumn',
    'template' => '{test}',
        'buttons' => array(
            'test' => array(
                'label' => 'Select',
                'click' => 'js:function() { alert($data->id); return false;}',
            ),
        ),
),
Roman
  • 5,651
  • 1
  • 30
  • 41
  • Yes, but there is still main problem: Undefined variable: data. If I reffering to $data in raw column type - it work. – gryfi Apr 04 '11 at 20:37
  • It's interesting, there is access in additional field 'url'. Bug? – gryfi Apr 04 '11 at 20:54
  • Uhm sry... got it wrong yesterday. Try the code above again (remove "'." from $data->id) – Roman Apr 05 '11 at 10:58
0

You need to customize the CButtonColumn class. Have a look this post:

http://www.yiiframework.com/wiki/714/yii-1-1-cgridview-use-special-variable-data-in-the-options-of-a-button-i-e-evaluate-options-attribute/

Sarvar Nishonboyev
  • 12,262
  • 10
  • 69
  • 70
0

You can do that by custom function, as we can derived a $data variable inside it so that we can utilize better php as well as yii itself.

Try like this :

'test' => array(
          'label' => 'Select',
          'click' => function($data) {
            $id = $data->id;
            return "js:function() { alert($id); return false;}";
           },
     ),
Jigar7521
  • 1,549
  • 14
  • 27
  • 1
    As *StackOverflow* intends to be a site, where you can not only ask questions, to get them answered, but also to find already answered questions. While this might answer the question, further readers might benefit even more, if you'd include some explaination to it. – derM - not here for BOT dreams May 02 '17 at 12:01