0
我試圖在表單中實現2amigos SelectizeDropDownList小部件,以直接在下拉列表中向表中添加新值。在Yii2中使用2amigos SelectizeDropDownList創建新記錄
我正在使用模型書籍和模型作者,所以基本上希望能夠以書籍形式添加新的作者。
這是在更新功能的書控制器:
public function actionUpdate($id) {
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['index']);
} else {
return $this->render('update', [
'model' => $model,
'categories' => BookCategory::find()->active()->all(),
'publishers' => Publisher::find()->all(),
'copirights' => Copiright::find()->all(),
'authors' => Author::find()->all(),
]);
}
}
這是形式:
<?=
$form->field($model, 'author_id')->widget(SelectizeDropDownList::className(), [
// calls an action that returns a JSON object with matched
// tags
'loadUrl' => ['author/list'],
'value' => $authors,
'items' => \yii\helpers\ArrayHelper::map(\common\models\author::find()->orderBy('name')->asArray()->all(), 'id', 'name'),
'options' => [
'class' => 'form-control',
'id' => 'id'
],
'clientOptions' => [
'valueField' => 'id',
'labelField' => 'name',
'searchField' => ['name'],
'autosearch' => ['on'],
'create' => true,
'maxItems' => 1,
],
])
?>
這是函數的作者控制器:
public function actionList($query) {
$models = Author::findAllByName($query);
$items = [];
foreach ($models as $model) {
$items[] = ['id' => $model->id, 'name' => $model->name];
}
Yii::$app->response->format = \Yii::$app->response->format = 'json';
return $items;
}
形式可以很好地加載,過濾,搜索和添加新項目。
但它沒有在作者表中插入新的類型屬性。
我是否需要在書籍控制器中添加某些內容?
如何檢查它是新值還是現有作者的更改?
非常感謝