M I
M I

Reputation: 3682

Yii - CGridView rows / column values as link and calling ajax function on click

In a YII based project I have a cgridview. Requirement is to make whole row or every column value a link and clicking on any of link in row will fire an ajax call. I have tried it from here How to display rows of CGridView as a link

but issue it that If i make whole row as clickable it takes me to view action.

If I make individual column values in a row as a link and call ajax function i get following error.

Property "CDataColumn.options" is not defined. 

I need help in making whole row as clickable and call an ajax function or individual row values to call an ajax function on click.

Any help or guidance in right direction is greatly appreciated.

//code for making trading name column in cgridview as clickable and call ajax

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'customer-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'id',
        array(
                    'name'  => 'trading_name',
                    'value' => 'CHtml::link($data->trading_name, Yii::app()
                     ->createUrl("customer/view/",array("id"=>$data->primaryKey)))',
                     'type'  => 'raw',
                     'options' => array('ajax' => array('type' => 'get', 'url'=>'js:$(this).attr("href")',
                     'success' => 'js:function(data) { 
                      $("#tab1").html(data);')
                      ),
                 ),
        'email',    
        'site_code',

        array(
            'class'=>'CButtonColumn',
        ),

Upvotes: 1

Views: 9135

Answers (2)

M I
M I

Reputation: 3682

After some hassle I was able to make the row of the cgridview a link and on clicking on the each row calls an AJAX function. Below is the code. May be it is helpful for someone.

selectionChanged did the trick. On clicking any row calls an ajax function and displays each customer's information a div below grid.

<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'customer-grid',
'dataProvider'=>$model->search(),
'selectionChanged'=>'js:function(id){
 n = $.fn.yiiGridView.getSelection(id);
 if (n>0){
    $.ajax({
            url: "'.Yii::app()->urlManager->createUrl('customer/view/').'",
            type: "GET",
            data: {"id": parseInt(n)},
            dataType: "html",
            success: function(data) {
            $("#customer-div").html(data);
         }
         });',

    'filter'=>$model,
    'columns'=>array(
    'id',
    array(
                'name'  => 'trading_name',
                'value' => 'CHtml::link($data->trading_name, Yii::app()
                 ->createUrl("customer/view/",array("id"=>$data->primaryKey)))',
                 'type'  => 'raw',
                 'options' => array('ajax' => array('type' => 'get', 'url'=>'js:$(this).attr("href")',
                 'success' => 'js:function(data) { 
                  $("#tab1").html(data);')
                  ),
             ),
    'email',    
    'site_code',

    array(
        'class'=>'CButtonColumn',
    ),

Upvotes: 1

Jacko07
Jacko07

Reputation: 119

'options' is not possible for a cgridview column as said in yii documentation : http://www.yiiframework.com/doc/api/1.1/CGridColumn#htmlOptions-detail

You have to use 'htmlOptions' if you wan to set options.

But if you want to use ajax with a link, you have to use Chtml::ajaxLink() : http://www.yiiframework.com/doc/api/1.1/CHtml#ajaxLink-detail

I hope it helps you

Upvotes: 0

Related Questions