Search code examples
yiicgridview

Is it possible to have 2 different CGridView in one admin.php in Yii?


Is it possible to have 2 different CGridView Tables in one admin.php ?

For example, I have a page of Services and a page of Packages.

Services are basically individual single services whereas Packages consist of individual services.

So, my question is, can I have the Service's CGridView to be displayed in Package/admin.php page? A seperate CGridView table.

Top part with list of Packages, and at the bottom, a different table, with Individual services.

If so, please guide me through it. Thanks in advance.

Updated

public function actionAdmin() {
    $model = new Package('search');
    $model2 = new Service('search');
    $model->unsetAttributes();
    $model2->unsetAttributes();
    $model->active=1;
    $model2->active=1;

    if (isset($_GET['Package'])){
        $model->setAttributes($_GET['Package']);
    }

    $this->render('admin', array(
        'model' => $model,
        'model2' => $model2,
    ));
}

Under _form.php:

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'service-grid',
    'dataProvider' => $model2->search(),
    'htmlOptions'=>array('class'=>'grid-view grid-size'),
    'filter' => $model2,
    'columns' => array( //the appropriate columns
));

Solution

  • Yes this is possible and you can have any number of grid views in one page/action.

    here is a example where i display two gridviews, namely Manage Subjects 1 & Manage Subjects 2

    <?php
    /* @var $this SubjectController */
    /* @var $model Subject */
    $this->breadcrumbs = array(
        Yii::t('edu', 'Subjects') => array('index'),
        Yii::t('edu', 'Manage'),
    );
    ?>
    <?php echo $this->renderPartial('application.views.layouts._actions', array('model' => $model)); ?>
    <?php
    Yii::app()->clientScript->registerScript('search', "
        $('.search-button').click(function(){
            $('.search-form').toggle();
            return false;
        });
        $('.search-form form').submit(function(){
            $.fn.yiiGridView.update('data-grid', {
                data: $(this).serialize()
            });
            return false;
        });
    ");
    ?>
    <h3><?php echo Yii::t('edu', 'Manage Subjects 1'); ?></h3>
    <!-- search-form -->
    <div class="search-form" style="display:none">
        <p>You may optionally enter a comparison operator (<b>&lt;</b>, <b>&lt;=</b>, <b>&gt;</b>, <b>&gt;=</b>, <b>&lt;&gt;</b> or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.</p>
        <?php $this->renderPartial('_search', array('model' => $model)); ?>
    </div>
    <!-- search-form -->
    <?php echo $this->renderPartial('_grid', array('model' => $model)); ?>
    
        <h3><?php echo Yii::t('edu', 'Manage Subjects 2'); ?></h3>
        <!-- search-form -->
        <div class="search-form" style="display:none">
            <p>You may optionally enter a comparison operator (<b>&lt;</b>, <b>&lt;=</b>, <b>&gt;</b>, <b>&gt;=</b>, <b>&lt;&gt;</b> or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.</p>
            <?php $this->renderPartial('_search', array('model' => $model)); ?>
        </div>
        <!-- search-form -->
    <?php echo $this->renderPartial('_grid', array('model' => $model)); ?>
    

    Update

    you need to create 2 models, see my below code, model2 is created and its a different table. you pass it to admin.php and in the gridview change model to model2 in your second gridview. thats all.

    /**
     * Manages all models.
     */
    public function actionAdmin()
    {
        $model = new Subject('search');
        $model2 = new Institute('search');
        Yii::app()->appLog->writeLog('Manage Subjects.');  // Activity log entry
        $model->unsetAttributes();  // Clear any default values
    
        $data = TK::get('Subject');
        if ($data !== null)
            $model->attributes = $data;
    
        $params = array('model' => $model, 'model2' => $model2);
        if (Yii::app()->request->isAjaxRequest)
            $this->renderPartial('_grid', $params);
        else
            $this->render('admin', $params);
    }