【问题标题】:Is it possible to have 2 different CGridView in one admin.php in Yii?Yii 的一个 admin.php 中是否可以有 2 个不同的 CGridView?
【发布时间】:2023-03-07 00:47:01
【问题描述】:

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

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

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

所以,我的问题是,我可以让服务的 CGridView 显示在 Package/admin.php 页面中吗?一个单独的 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
));

【问题讨论】:

    标签: yii cgridview


    【解决方案1】:

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

    这是我显示两个网格视图的示例,即管理主题 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 个模型,请参阅下面的代码,创建了 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);
    }
    

    【讨论】:

    • 对不起,我不太明白。但是你所做的不是来自同一张桌子,而是来自分开的桌子吗?我希望 2 个不同的表格位于单独表格的同一页面中。如果我错了,请纠正我。
    • 好的,现在我已经成功地将 2 个表集成到 1 个管理页面中。但是对于搜索,它不适用于第二张桌子。在原始表中的搜索非常好,但在第二个表中却没有。我可以知道我可以添加什么以使其正常工作吗?谢谢
    • @Yeen 我希望你发布了 _grid.php 文件,请制作两个文件并尝试使用 _grid.php 和 _grid2.php 并用于两种模型。如果它不起作用,请在堆栈溢出中创建另一个问题,
    • 我没有任何名为“_grid.php”的文件。我可以从哪里得到它?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 2012-08-22
    • 1970-01-01
    相关资源
    最近更新 更多