Vit Kos
Vit Kos

Reputation: 5755

Yii CGridView dynamic dataProvider

Is there a way to re-render the view that contains CGridView with a custom 'dataProvider' option? For example I have such view file

<?php $this->widget('bootstrap.widgets.BootGridView',array(
    'id'=>'operations-grid',
    'type'=>'striped bordered',
    'dataProvider'=>$model->search(),       
    'columns'=>array(
        array('name'=>'operationType','value'=>'$data->operationType->name'),
        array(
            'name'=>'creation_date','type'=>'datetime'
        ),
        'ammount_usd:raw:Ammount',
        'comment:text:Comment',
        array(
            'name'=>'currency',
            'value'=>'$data->currency->short',
        ),
        array(
            'name'=>'client',
            'value'=>'$data->client->fio'
        ),
        array(
            'name'=>'organization',
            'value'=>'$data->organization->name'
        ),

        array(
            'class'=>'bootstrap.widgets.BootButtonColumn',
            'header'=>'Action'
        ),
    ),
)); ?>

As a provider I have $model->search(), but I want for example, if a button pressed this view to be re-rendered via ajax with different dataProvider. Is there a way to achieve this? Thanks.

Upvotes: 1

Views: 6574

Answers (3)

Gerhard Liebenberg
Gerhard Liebenberg

Reputation: 444

This wiki on dynamic CGridView should give you lots of ideas.

Upvotes: 0

Boaz Rymland
Boaz Rymland

Reputation: 1467

You can do this in several ways. Among which, you can let the controller provide the view with the $dataProvider, like this:

$this->widget('bootstrap.widgets.BootGridView',array(
'id'=>'operations-grid',
'type'=>'striped bordered',
'dataProvider'=>$dataProvider,   

The controller could instantiate the needed data provider based on the parameters that arrived in that request. The view then just passes this data provider to CGridView, completely oblivion to its precise type.

As already mentioned, you can extend CDataProvider (the same class that CActiveDataProvider extends) to customize it completely as long as it provides the API required by it. See its documentation

Upvotes: 3

artushin
artushin

Reputation: 63

The point of the dataprovider is to give you the appropriate data of a certain model based on certain inputs. Search() does this well because you just need to instantiate a model class with the appropriate attributes you want to search against, and the dataprovider will give you all of your data that matches that criteria.

If you want to change the output of the dataprovider beyond that, you can extend your model class with a modified search() function. If you're trying to get a completely different model passed into your data provider, I would set up a different view for it because it's going to have different columns anyway.

Upvotes: 1

Related Questions