在Yii的一个admin.php中可以有2个不同的CGridView吗?

时间:2015-04-07 04:54:29

标签: yii cgridview

是否可以在一个admin.php中拥有2个不同的CGridView表?

例如,我有一个服务页面和一个包页面。

服务基本上是单独的服务,而包裹则由个人服务组成。

所以,我的问题是,我可以在Package / admin.php页面中显示Service的CGridView吗?一个单独的CGridView表。

包含列表的顶部部分,在底部是另一个表,包含个人服务。

如果是这样,请引导我完成它。提前致谢。

更新

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,
    ));
}

在_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
));

1 个答案:

答案 0 :(得分:2)

是的,这是可能的,您可以在一个页面/操作中拥有任意数量的网格视图。

这里是一个显示两个网格视图的示例,即Manage Subjects 1&amp;管理主题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)); ?>

<强>更新

您需要创建2个模型,请参阅下面的代码,创建model2并使用不同的表。您将它传递给admin.php并在gridview中将模型更改为第二个gridview中的model2。多数民众赞成。

/**
 * 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);
}