Yii2 Gridview 没有在索引页面中显示数据
Yii2 Gridview didn't show data in Index page
我正在使用模型搜索来过滤和查询所有数据到控制器并将数据发回视图。查询没问题,但在视图中无法显示数据。而且我不知道如何解决?
这是我的模特搜索:
$user_id = Yii::$app->user->id;
$subquery = Workload::find()->select(['p.id', 'p.project_name', 'w.user_id', 'w.commit_time', 'w.comment', 'w.workload_type'])
->from(['project as p', 'workload as w'])
->where(['and','w.user_id = '.$user_id, 'p.id = w.project_id']);
$query = Workload::find()
->select(['workloadTeam.project_id', 'wp.project_name', 'workloadTeam.user_id', 'workloadTeam.commit_time', 'workloadTeam.week','workloadTeam.workload_type', 'workloadTeam.comment'])
->from(['wp' => $subquery]);
$query->join('INNER JOIN', 'workload as workloadTeam', 'wp.id = workloadTeam.project_id')->all();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
在我的控制器中:
public function actionIndex()
{
$searchModel = new WorkloadSearch();
$dataProvider = $searchModel->searchWorkloadofUser(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
并且在我的视图索引页面中:
$gridColumns = [
[
'class' => 'yii\grid\SerialColumn',
'contentOptions' => ['style' => 'width: 40px;']
],
[
'attribute' => 'user_id',
'label' => Yii::t('app', 'Members Alias'),
'value' => 'profile.full_name',
'filter' => \app\modules\admin\models\User::profileDropDown(),
'contentOptions' => ['style' => 'width: 150px;', 'class' => 'select2'],
'filterOptions' => ['data-widget' => 'select2'],
'format' => 'html',
],
[
'attribute' => 'project_id',
'label' => Yii::t('app', 'Assigned Projects'),
'value' => 'project.project_name',
'filter' => \app\modules\admin\models\Project::projectWorkloadMemberDropdown(),
'contentOptions' => ['style' => 'width: 150px;', 'class' => 'select2', 'placeholder' => 'Select your projects'],
'filterOptions' => ['data-widget' => 'select2'],
'format' => 'html',
],
[
'label' => 'Committed (hr.)',
'value' => function ($model) {
return $model->commit_time;
}
],
[
'label' => 'Burned Amount (hr.)',
'value' => function ($model) {
return WorkLog::getTotalBurnedHoursofUser($model->user_id, $model->project_id);
},
],
[
'attribute' => 'week',
'filterInputOptions' => ['class' => 'form-control datepicker'],
'value' => function ($model) {
return $model->week;
},
'label' => Yii::t('app', 'From Date'),
],
[
'label' => 'Comment',
'value' => function ($model) {
return $model->comment;
}
],
[
'label' => 'Workload Type',
// 'attribute' => 'workload_type',
'filter' => \app\modules\admin\models\Workload::WorkloadTypeDropdownList(),
'value' => function ($model) {
return \app\modules\admin\models\Workload::$workload_type_label[$model->workload_type];
}
]
];
?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
]); ?>
我在你的代码中发现你正在使用 all()
方法,而 ActiveDataProvider
不需要,因为 all()
方法 returns 除了你之外的所有行需要将查询对象传递给 ActiveDataProvider
$user_id = Yii::$app->user->id;
$subquery = Workload::find()->select(['p.id', 'p.project_name', 'w.user_id', 'w.commit_time', 'w.comment', 'w.workload_type'])
->from(['project as p', 'workload as w'])
->where(['and','w.user_id = '.$user_id, 'p.id = w.project_id']);
$query = Workload::find()
->select(['workloadTeam.project_id', 'wp.project_name', 'workloadTeam.user_id', 'workloadTeam.commit_time', 'workloadTeam.week','workloadTeam.workload_type', 'workloadTeam.comment'])
->from(['wp' => $subquery]);
$query->join('INNER JOIN', 'workload as workloadTeam', 'wp.id = workloadTeam.project_id');
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
之后您可以打印 $dataProvider->models
用于调试目的,即您正在从数据库中获取行。如果你得到行意味着你现在可以将 $dataProvider
对象传递给 GridView
.
我正在使用模型搜索来过滤和查询所有数据到控制器并将数据发回视图。查询没问题,但在视图中无法显示数据。而且我不知道如何解决?
这是我的模特搜索:
$user_id = Yii::$app->user->id;
$subquery = Workload::find()->select(['p.id', 'p.project_name', 'w.user_id', 'w.commit_time', 'w.comment', 'w.workload_type'])
->from(['project as p', 'workload as w'])
->where(['and','w.user_id = '.$user_id, 'p.id = w.project_id']);
$query = Workload::find()
->select(['workloadTeam.project_id', 'wp.project_name', 'workloadTeam.user_id', 'workloadTeam.commit_time', 'workloadTeam.week','workloadTeam.workload_type', 'workloadTeam.comment'])
->from(['wp' => $subquery]);
$query->join('INNER JOIN', 'workload as workloadTeam', 'wp.id = workloadTeam.project_id')->all();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
在我的控制器中:
public function actionIndex()
{
$searchModel = new WorkloadSearch();
$dataProvider = $searchModel->searchWorkloadofUser(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
并且在我的视图索引页面中:
$gridColumns = [
[
'class' => 'yii\grid\SerialColumn',
'contentOptions' => ['style' => 'width: 40px;']
],
[
'attribute' => 'user_id',
'label' => Yii::t('app', 'Members Alias'),
'value' => 'profile.full_name',
'filter' => \app\modules\admin\models\User::profileDropDown(),
'contentOptions' => ['style' => 'width: 150px;', 'class' => 'select2'],
'filterOptions' => ['data-widget' => 'select2'],
'format' => 'html',
],
[
'attribute' => 'project_id',
'label' => Yii::t('app', 'Assigned Projects'),
'value' => 'project.project_name',
'filter' => \app\modules\admin\models\Project::projectWorkloadMemberDropdown(),
'contentOptions' => ['style' => 'width: 150px;', 'class' => 'select2', 'placeholder' => 'Select your projects'],
'filterOptions' => ['data-widget' => 'select2'],
'format' => 'html',
],
[
'label' => 'Committed (hr.)',
'value' => function ($model) {
return $model->commit_time;
}
],
[
'label' => 'Burned Amount (hr.)',
'value' => function ($model) {
return WorkLog::getTotalBurnedHoursofUser($model->user_id, $model->project_id);
},
],
[
'attribute' => 'week',
'filterInputOptions' => ['class' => 'form-control datepicker'],
'value' => function ($model) {
return $model->week;
},
'label' => Yii::t('app', 'From Date'),
],
[
'label' => 'Comment',
'value' => function ($model) {
return $model->comment;
}
],
[
'label' => 'Workload Type',
// 'attribute' => 'workload_type',
'filter' => \app\modules\admin\models\Workload::WorkloadTypeDropdownList(),
'value' => function ($model) {
return \app\modules\admin\models\Workload::$workload_type_label[$model->workload_type];
}
]
];
?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
]); ?>
我在你的代码中发现你正在使用 all()
方法,而 ActiveDataProvider
不需要,因为 all()
方法 returns 除了你之外的所有行需要将查询对象传递给 ActiveDataProvider
$user_id = Yii::$app->user->id;
$subquery = Workload::find()->select(['p.id', 'p.project_name', 'w.user_id', 'w.commit_time', 'w.comment', 'w.workload_type'])
->from(['project as p', 'workload as w'])
->where(['and','w.user_id = '.$user_id, 'p.id = w.project_id']);
$query = Workload::find()
->select(['workloadTeam.project_id', 'wp.project_name', 'workloadTeam.user_id', 'workloadTeam.commit_time', 'workloadTeam.week','workloadTeam.workload_type', 'workloadTeam.comment'])
->from(['wp' => $subquery]);
$query->join('INNER JOIN', 'workload as workloadTeam', 'wp.id = workloadTeam.project_id');
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
之后您可以打印 $dataProvider->models
用于调试目的,即您正在从数据库中获取行。如果你得到行意味着你现在可以将 $dataProvider
对象传递给 GridView
.