Godwin
Godwin

Reputation: 57

how to display selected records in yii gridview

Good day, great programmers, please I am using yii gridview to display students records but I want to restrict the records displayed to just a particular student. please how do I go about this ...this is my index

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
         // 'id',
        'student.first_name',
        'student.last_name',
        'studentFaculty.faculty_name',
        'studentDept.department_name',
        'level',
        'stateOfOrigin.state_name',
        'image',

        ['class' => 'yii\grid\ActionColumn'],
    ],
]); ?> 

This is my controller below

public function actionIndex()
{
    $searchModel = new StudentsSearch(); 
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams)->ArrayHelper::map([ Students::find()->where('student_id'=> yii::$app->user->identity->id)->all()]);

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}

Upvotes: 0

Views: 101

Answers (3)

Muhammad Omer Aslam
Muhammad Omer Aslam

Reputation: 23788

The cleanest way to do this without adding any extra params to the search function because your search() function could be used for the backend by admin too and if you add extra params here you would need to modify them at all places it is called, so change your controller/action to the following and assign the student_id to the queryParams before calling the StudentsSearch model

public function actionIndex()
{
    $searchModel = new StudentsSearch(); 
    $queryParams=Yii::$app->request->queryParams;
    $queryParams['StudentsSearch']['student_id']=Yii::$app->user->id;
    $dataProvider = $searchModel->search($queryParams);

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}

Just make sure your StudentsSearch model the field student_id is searched in the search() function before returning $dataProvider.

$query->andFilterWhere ( ['student_id' => $this->student_id ]);

Upvotes: 1

ExploitFate
ExploitFate

Reputation: 605

Seems you are looking for something like this:

StudentsSearch model

public function search($params, $filterStudentId)
{
    $query = Students::find();
    $query->andWhere(['student_id' => $filterStudentId]);
    // some code
    return $dataProvider;
}

Controller

public function actionIndex()
{
    $searchModel = new StudentsSearch();
    $params = Yii::$app->request->queryParams;
    $filterStudentId = Yii::$app->user->identity->id;
    $dataProvider = $searchModel->search($params, $filterStudentId);

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}

Upvotes: 0

Mohan
Mohan

Reputation: 616

Replace below code with Controller's Index action

public function actionIndex(){

$searchModel = new StudentsSearch(); 
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->andWhere(['student_id'=>yii::$app->user->identity->id]); 


return $this->render('index', [
    'searchModel' => $searchModel,
    'dataProvider' => $dataProvider,
]);

}

Upvotes: 1

Related Questions