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

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

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

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

服务基本上是单独的单一服务,而套餐由单独的服务组成。

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

顶部是包列表,底部是不同的 table,包含单独的服务。

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

已更新

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

是的,这是可能的,您可以在一个网格视图中包含任意数量的网格视图 page/action。

这是我显示两个网格视图的示例,即管理主题 1 和管理主题 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 个模型,请参阅我下面的代码,模型 2 已创建并且它是一个不同的 table。您将它传递给 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);
}