如何在 yii gridview 中显示选中的记录
how to display selected records in yii gridview
你好,伟大的程序员,我正在使用 yii gridview 来显示学生记录,但我想将显示的记录限制为仅显示特定学生。请问我该怎么做...这是我的索引
<?= 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'],
],
]); ?>
下面是我的控制器
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,
]);
}
将以下代码替换为控制器的索引操作
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,
]);
}
您似乎在寻找这样的东西:
学生搜索模型
public function search($params, $filterStudentId)
{
$query = Students::find();
$query->andWhere(['student_id' => $filterStudentId]);
// some code
return $dataProvider;
}
控制器
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,
]);
}
最干净的方法是不向搜索函数添加任何额外的参数,因为你的 search()
函数也可以被管理员用于后端,如果你在这里添加额外的参数,你将需要修改它们在它被调用的所有地方,所以将你的 controller/action
更改为以下内容并在调用 StudentsSearch
模型
之前将 student_id
分配给 queryParams
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,
]);
}
只需确保您的 StudentsSearch
模型字段 student_id
在返回 $dataProvider
之前在 search()
函数中被搜索到。
$query->andFilterWhere ( ['student_id' => $this->student_id ]);
你好,伟大的程序员,我正在使用 yii gridview 来显示学生记录,但我想将显示的记录限制为仅显示特定学生。请问我该怎么做...这是我的索引
<?= 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'],
],
]); ?>
下面是我的控制器
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,
]);
}
将以下代码替换为控制器的索引操作
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,
]);
}
您似乎在寻找这样的东西:
学生搜索模型
public function search($params, $filterStudentId)
{
$query = Students::find();
$query->andWhere(['student_id' => $filterStudentId]);
// some code
return $dataProvider;
}
控制器
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,
]);
}
最干净的方法是不向搜索函数添加任何额外的参数,因为你的 search()
函数也可以被管理员用于后端,如果你在这里添加额外的参数,你将需要修改它们在它被调用的所有地方,所以将你的 controller/action
更改为以下内容并在调用 StudentsSearch
模型
student_id
分配给 queryParams
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,
]);
}
只需确保您的 StudentsSearch
模型字段 student_id
在返回 $dataProvider
之前在 search()
函数中被搜索到。
$query->andFilterWhere ( ['student_id' => $this->student_id ]);