Ajax Links In Cgridview Widget Loaded By Ajax Not Working

Hi All,

In my view I have a CGridView with an ajax link that loads another CGridView from a controller action:





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

        'id'=>'chart-list',

	'dataProvider'=>$dataProvider

        ,'columns' => array(

        'fname'

        , 'lname'

        , 'grade'

        , 'age'

       ,array(

        'name'  => 'Charts',

        'value' => 'CHtml::ajaxLink("List Charts", array("student/Ajaxcontent", "student_id"=>$data["id"]),

            array("update" => "#stu_charts"))',

        'type'  => 'raw',

    )

        , array(

            'class' => 'CButtonColumn'

            , 'viewButtonUrl' => 'Yii::app()->createUrl("/Student/view", array("id"=>$data["id"]))'

            , 'updateButtonUrl' => 'Yii::app()->createUrl("/Student/update", array("id"=>$data["id"]))'

            , 'deleteButtonUrl' => 'Yii::app()->createUrl("/Student/delete", array("id"=>$data["id"]))'

        ))

   )

);


<div id="stu_charts"></div>

<div id="show_chart"></div>



Here is the controller code:





    public function actionAjaxcontent($student_id) {

  

    

        echo "<span style='font-size: 2em';>$name $lname:</span>";

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

            'id'=>'chart-grid',

            'dataProvider' => $dataProvider

            , 'columns' => array(

                'chart_title'

                , 'id'

                , 'chart_date'

                , 'student_id'

                ,  array(

        'name'  => 'Show',

        'value' => 'CHtml::ajaxLink("Show Chart", array("student/Stuff", "student_id"=>$data["id"]),

            array("update" => "#show_chart"))',

        'type'  => 'raw',

    ),

         ) )   );

    }

    

    public function actionStuff() {

        echo "stuff";

    }



The CGridView from the controller loads in the view fine, but the links it contains do not work.

I would like it to ultimatley show a renderPartial of another view using these links, right now I’m just trying to get it to echo the content of the actionStuff method in the div <div id=“show_chart”></div>

I have tried adding JQuery code to the view like this




Yii::app()->clientScript->registerScript('ajaxupdate', "

$('#chart-grid a.ajaxupdate').live('click', function() {

        $.fn.yiiGridView.update('chart-grid', {

                type: 'POST',

                url: $(this).attr('href'),

   

             

        });

        return false;

});

");



and adding the ajaxupdate class to the ajax link in the second CGridView, but no luck.

Appreciate any help

Thanks

Dear Friend

I could not test your code in my localhost.

Following are my suggestions.

1.The methods CController::render and CController::renderPartial methods are capable of registering necessary scripts when rendering views.Then try the following in our case.




public function actionAjaxcontent($student_id) 

{   //Put the logic here to define the $dataProvider

    echo $this->renderPartial('ajaxcontent',array('dataProvider'=>$dataProvider),true,true);

}




Then put the rest of the logic in the view "ajaxcontent".




echo "<span style='font-size: 2em';>$name $lname:</span>";

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

            'id'=>'chart-grid',

            'dataProvider' => $dataProvider

            , 'columns' => array(

                'chart_title'

                , 'id'

                , 'chart_date'

                , 'student_id'

                ,  array(

        'name'  => 'Show',

        'value' => 'CHtml::ajaxLink("Show Chart", array("student/Stuff", "student_id"=>$data["id"]),

            array("update" => "#show_chart"))',

        'type'  => 'raw',

    ),

         ) )   );




  1. In both AjaxLinks try to assign unique id.Else both have same id as registered by Yii .It may give undesirable

effects.




'value' => 'CHtml::ajaxLink("Show Chart", array("student/Stuff", "student_id"=>$data["id"]),

            array("update" => "#show_chart"),array("id"=>"grid".$data->id))',






'value' => 'CHtml::ajaxLink("Show Chart", array("student/Stuff", "student_id"=>$data["id"]),

            array("update" => "#show_chart"),array("id"=>"list".$data->id))',



Wishing you good luck.

Regards.

seenivasan, Thank you. I’ve made some progress with your help.

My controller code now looks like this:




    public function actionAjaxcontent($student_id) {

        

    // Refine the search by adding criteria array to dataProvider

        $criteria2 = new CDbCriteria;

        $criteria2->condition = 'student_id=:student_id';

        $criteria2->params = array(':student_id' => $student_id);

        $output = Chart::model()->find($criteria2);


        $dataProvider = new CActiveDataProvider('Chart', array(

                    'criteria' => $criteria2,

                ));

 

        

        echo $this->renderPartial('ajaxcontent',array('dataProvider'=>$dataProvider),true,true);


  

    }

    

    public function actionStuff() {

        $model = new Chart;

         $this->renderPartial('showchart', array(

            'model' => $model,

        ));

    }




And this is the content of ajaxcontent.php




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

    'id' => 'chart-grid',

    'dataProvider' => $dataProvider

    , 'columns' => array(

        'chart_title'

        , 'id'

        , 'chart_date'

        , 'student_id'

                    ,  array(

        'name'  => 'Show',

        'value' => 'CHtml::ajaxLink("Show Chart", array("student/Stuff", "student_id"=>$data["id"],array("id"=>"list".$data->id)),

            array("update" => "#show_chart"))',

        'type'  => 'raw',

    )


 

    )));






The contents of showchart.php now load via ajax in my view. :)

I am now stuck on this:

showchart.php is in the student view and the code that produces the chart I want to display is in the chart controller. I have not been able to make the chart display in showchart.php. The code that will display the chart from the chart view is:




$chart = ChartController::createlinechart($model->id); 


$this->widget(

        'application.extensions.OpenFlashChart2Widget.OpenFlashChart2Widget', array(

    'chart' => $chart, // this is an object created using OpenFlashChart2's

    // PHP language binding.

    'width' => '100%',

    'height' => '400',

        )

);

?>



I’m not sure what is the best way to proceed.

Thanks