您好,我正在尝试在 Yii2 中的每个驱动程序的 gridview 列中添加一个 select2 小部件。网格用于驱动程序模型。一名司机可能有多个区域。如下图所示:
这是网格:
我在驱动程序模型中创建了一个关系:
public function getZones()
{
return $this->hasMany(Zone::className(), ['id' => 'zone_id'])
->viaTable('driver_zone_mapping', ['driver_id' => 'id']);
}
这是 Select2 Grid Column 代码:
[
'label' => 'Zones',
'format' => 'raw',
'value' => function ($model) {
return Select2::widget([
'model' => $model,
'attribute' => 'zones',
'value' => $model->zones,
'data' => ArrayHelper::map(Zone::find()->asArray()->all(), 'id', 'zone'),
'options' => [
'placeholder' => 'Select Zone ...',
'class' => 's2-tog-button',
'data-id' => $model->id,
'multiple' => true,
],
'pluginOptions' => [
'tags' => true,
],
]);
}
],
控制器动作:
public function actionTest()
{
$searchModel = new DriverSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->pagination->pageSize = 50;
return $this->render('test', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
这是模型搜索功能:
public function search($params)
{
$query = Driver::find();
$query->joinWith(['zones']);
// add conditions that should always apply here
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'presence' => $this->presence,
'priority' => $this->priority,
'created_at' => $this->created_at,
]);
$query->andFilterWhere(['like', 'name', $this->name])
->andFilterWhere(['like', 'visible_name', $this->mobile])
->andFilterWhere(['like', 'mobile', $this->mobile])
->andFilterWhere(['like', 'other', $this->other]);
return $dataProvider;
}
问题是,select2 小部件仅针对一个驱动程序显示(第一个具有区域的驱动程序)。其他人没有小部件,即使他们在驱动程序区域映射表中有区域。任何想法?