Getting $Data->Id Value In Cgridview On Custom Buttons

Hello everyone,

I’m trying to pass a model id to a js function I trigger when the user clicks on a customized button in CgridView. My TbButtonColumn is the following in my view file:


                array(

                   'class'=>'bootstrap.widgets.TbButtonColumn',

                   'template'=>'{associaScadenza}',

                   'buttons'=>array(

                       'associaScadenza' => array(

                        'label'=>'Associa Scadenza',

                        'icon'=>'file',

                        'url'=>'',

                        'options'=>array(

                            'onclick'=>"{addScadenza(//This is where I need to add the ID); $('#dialogScadenza').dialog('open');}",

                            'style'=>'cursor: pointer;text-decoration:none',

                            ),

                        ),

                       ),

                   ),

Later in the code I have the function I need to call, using the specific model id retrieved


<script type="text/javascript">

function addScadenza()

{

    <?php echo CHtml::ajax(array(

            'url'=>array('appalto/aggiungiScadenza'),

            'data'=> "js:$(this).serialize()",

            'type'=>'post',

            'dataType'=>'json',

            'success'=>"function(data)

            {

                if (data.status == 'failure')

                {

                    $('#dialogScadenza div.divForForm').html(data.div);

                    $('#dialogScadenza div.divForForm form').submit(addScadenza);

                }

                else

                {

                    $('#dialogScadenza div.divForForm').html(data.div);

                    setTimeout(\"$('#dialogScadenza').dialog('close') \",3000);

                }

 

            } ",

            ))?>;

    return false; 

 

}

 

</script>

However, if I simply write ‘$data->id’ inside the “onClick” attribute, I get and “undefined variable: data” error. I need the “onClick” attribute to be evaluated before rendering, something like the default buttons do when generating their row-specific url. Anyone can help?

In the JS callback for the click action you can get the cell that has been clicked, access its parent - table row, get the index of this row in whole table and using this index number read an id from a hidden div that is rendered after the table. I know it’s complex, but you could create a helper js function to do that.

I eventually tried extending the TbButtonColumn widget


<?php 


Yii::import('bootstrap.widgets.TbButtonColumn');


class MyButtonColumn extends TbButtonColumn

{

    protected function renderButton($id, $button, $row, $data)

    {

        if (isset($button['visible']) && !$this->evaluateExpression(

            $button['visible'],

            array('row' => $row, 'data' => $data)

        )

        ) {

            return;

        }


        $label = isset($button['label']) ? $button['label'] : $id;

        $url = isset($button['url']) ? $this->evaluateExpression($button['url'], array('data' => $data, 'row' => $row))

            : '#';

        $options = isset($button['options']) ? $button['options'] : array();

        

        //my add starts here

        if(isset($options['id'])){

            $id = $this->evaluateExpression($options['id'],array('data' => $data, 'row' => $row));

            if($options['function']='addScadenza'){

            $options['onclick']="{addScadenza(".$id."); $('#dialogScadenza').dialog('open');}";

            }

        }

        //my add ends here


        if (!isset($options['title'])) {

            $options['title'] = $label;

        }


        if (!isset($options['rel'])) {

            $options['rel'] = 'tooltip';

        }


        if (isset($button['icon'])) {

            if (strpos($button['icon'], 'icon') === false) {

                $button['icon'] = 'icon-' . implode(' icon-', explode(' ', $button['icon']));

            }


            echo CHtml::link('<i class="' . $button['icon'] . '"></i>', $url, $options);

        } else if (isset($button['imageUrl']) && is_string($button['imageUrl'])) {

            echo CHtml::link(CHtml::image($button['imageUrl'], $label), $url, $options);

        } else {

            echo CHtml::link($label, $url, $options);

        }

    }

}

So my view file column became:


                array(

                   'class'=>'MyButtonColumn',

                   'template'=>$template.'{associaScadenza}',

                   'buttons'=>array(

                       'associaScadenza' => array(

                        'label'=>'Associa Scadenza',

                        'icon'=>'file',

                        'url'=>'',

                        'options'=>array(

                            'onclick'=>"",

                            'style'=>'cursor: pointer;text-decoration:none',

'id'='$data->id',

'function'=>'addScadenza',


                            ),

                        ),

                       ),

                   ),

if it can be of someone use.

thank you Master Q, it was very helpful!