2015-04-07 25 views
2

是否有可能在一個admin.php中有2個不同的CGridView表?在Yii的一個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 
)); 

回答

2

是的,這是可能的,你可以在一個頁面/動作的任何數量的網格視圖。

這裏是我顯示兩個GridView的一個例子,即管理主題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); 
} 
+0

對不起,我不太明白。但是,你所做的不是來自同一張桌子,而是分開的桌子?我想要2個不同的表在同一個頁面中分開的表中。如果我錯了,請糾正我。 – NewbieCoder

+1

謝謝!你救了我的一天!乾杯;) – NewbieCoder

+0

好吧,現在我已經成功地將2個表整合到1個管理頁面中。但是對於搜索,它不適用於第二張桌子。原表中的搜索非常好,但不在第二張表中。我可以知道我可以添加什麼,以便它正常工作嗎?謝謝 – NewbieCoder

相關問題