Rendering two CGridViews in one View fails

Hi!

I try to render two CGridView’s into one view:





<?php $this->renderPartial('/category/admin'); ?>

<?php $this->renderPartial('/products/admin'); ?>








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

  'id'=>'category-grid',

  'dataProvider'=>$model->search(),

  'filter'=>$model,

  'columns'=>array(

    'title',

    array(

      'class'=>'CButtonColumn',

      'template' => '{open}',

      'buttons'=>

      array('open' =>

        array(

          'label' => 'open',

          'url' => 'Yii::app()->createUrl("/YiiShop/category/update", array("id" => $data->category_id))',

          )

        )

    ),

  ),

)); 







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

  'id'=>'products-grid',

  'dataProvider'=>$model->search(),

  'filter'=>$model,

  'columns'=>array(

    'category_id',

    'product_id',

    'title',

    'price',

    'unit',

    array(

      'class'=>'CButtonColumn',

      'buttons'=>array(

        'label' => 'view',

        'url' => array('products/view', 'id' => $data->id),

        )

    ),

  ),

)); 




Sadly, the second CGridView lacks the whole CButtonColumn, no matter if i specify the ‘buttons’ array or doesn’t …

is the CButtonColumn intelligent enough to determine the link’s of {view}{update}{delete} depending on the dataProvider that’s give to the CGridView? If not: shouldn’t it be? :slight_smile:

I do not understand your question, but thank you a lot for your example on how to set up an URL in the buttons options:


'url' => 'Yii::app()->createUrl("/YiiShop/category/update", array("id" => $data->category_id))',



Unfortunately, the documentation does not provide any example and it is not clear on to do it. Without your example I don’t know if I could have succeded.

Your 2nd grid’s button column is not configured correctly.

The problem with the CButtonColumn is, that it just renders links relative to the actual Controller, that renders the view.

So, for example, when i render a View containing a Grid that shows Post Models and another Grid that shows Comments, the links that are being generated by CButtonColumn in both Grid Views doesn’t point to the right view/update/delete-URL.

Maybe i will take a further look on this issue today and suppose a patch…

In CButtonColumn you must change:

$js[]=“jQuery(‘a.{$button[‘options’][‘class’]}’).live(‘click’,$function);”;

in this line:

$js[]=“jQuery(’#{$this->grid->id} a.{$button[‘options’][‘class’]}’).live(‘click’,$function);”;

An so every grid was update separately; in the current version the delete action was linked on first a.delete.

I’ve tried to test Taconite and works fine !!

After delete action you can return the XML fo taconite but you must use

'afterAjaxUpdate' =&gt; 'function(id, data){ &#036;.taconite(data); }',     

in the CGridView parameters.

Maybe is better to create a _grid.php to recreate ONLY the grid to update passing the CActiveDataProvider and the ID of the view to be reloaded. The actual CGridView render all the page an the extract the new content of the choosen div and replace it. The partialrender is faster.

Bye.

@Simonz: I’ve fixed the js problem you found. Thanks!

Ok, i didn’t saw that you can pass the ButtonURL’s. This solved my problem:




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

  'id'=>'category-grid',

  'dataProvider'=>$model->search(),

  'filter'=>$model,

  'columns'=>array(

    'title',

    array(

      'class'=>'CButtonColumn', 

      'viewButtonUrl' => 'Yii::app()->createUrl("/YiiShop/category/view", array("id" => $data->category_id))',

      'updateButtonUrl' => 'Yii::app()->createUrl("/YiiShop/category/update", array("id" => $data->category_id))',

      'deleteButtonUrl' => 'Yii::app()->createUrl("/YiiShop/category/delete", array("id" => $data->category_id))',

    ),

  ),

)); 



Thank you!

But anyway, wouldn’t the CGridView be more intelligent if it would generate his links depending on the MODEL it displays rather than the VIEW that renders it?