yii2创建视图以显示模型

时间:2015-02-20 11:17:04

标签: gridview grid yii2 master-detail

我正在尝试在项目中实现demos.krajee.com/grid-demo,但我很难为详细视图创建网格。

这是一个网格,当使用扩展的expandRow功能时,我需要从id(master - detail场景)中的另一个表基础获取所有相关记录

在控制器中我有这个功能,它可以正常工作以获取特定ID的所有记录:

    public function actionDetail() {
    $searchModel = new AttendanceSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    if (isset($_POST['expandRowKey'])) {
        $IDcustomers = Yii::$app->request->post('expandRowKey');

    $model = Attendance::find()
            ->where(['IDcustomers' => $IDcustomers])
            ->all();            
        return $this->renderPartial('_attendance-details', ['model'=>$model]);

    } else {
        return '<div class="alert alert-danger">No data found</div>';
    }
}

现在的问题是如何从$ model创建视图(_attendance-details)并将所有记录显示在网格中。

如果我使用DetailView与$ model的第一个元素工作正常:

        <?= DetailView::widget([
    'model' => $model[0],
    'attributes' => [
        'IDattendance',
        'IDcustomers',
        'date',
        'doctor',
        'lawyer',
    ],
]) ?>

但是当我尝试使用网格时,我需要数据提供者,而我无法使用我正在传递的$模型。

我是yii的新手所以任何指南都会非常感激。


我尝试了vitalik并且做得很完美

1 个答案:

答案 0 :(得分:2)

controller集合中:

public function actionDetail()
{        
    if (isset($_POST['expandRowKey'])) {
        $IDcustomers = Yii::$app->request->post('expandRowKey');

        $model = Attendance::find()
            ->where(['IDcustomers' => $IDcustomers]);
        $dataProvider = new ActiveDataProvider([
            'query' => $model,
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        return $this->renderPartial('_attendance-details', ['dataProvider' => $dataProvider]);

    } else {
        return '<div class="alert alert-danger">No data found</div>';
    }
}

在视图中:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        'IDattendance',
        'IDcustomers',
        'date',
        'doctor',
        'lawyer',      
    ],
]);?>