open-ecommerce.org
open-ecommerce.org

Reputation: 1814

yii2 create view to display model

I'am trying to implement the demos.krajee.com/grid-demo in a project but i am getting stuck creating the grid for the detail view.

It is a grid and when use the expandRow functionality of the extention I need to get all the related records from another table base in the id (master - detail scenario)

In the controller i have this function that it is working fine to get all records for an specific 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>';
    }
}

The problem now is how to create the view (_attendance-details) from the $model and get all the records display in a grid.

if i use DetailView with the first element of $model works fine:

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

But when i try to use grid i need the dataprovider and i can't make it work with the $model i am passing.

I am very new to yii so any guide will be much appreciated.


I tried vitalik and work perfect

Upvotes: 0

Views: 2554

Answers (1)

vitalik_74
vitalik_74

Reputation: 4611

In controller set:

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>';
    }
}

In view:

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

Upvotes: 2

Related Questions