$url = \yii\helpers\Url::to(['/agitator/grs-data/personalnos']);
<?php
$grs = empty($model->personal_no) ? '' : GrsData::findOne($model->personal_no)->personal_no;
echo $form->field($model, 'personal_no')->widget(Select2::classname(), [
'initValueText' => $grs, // set the initial display text
'options' => ['placeholder' => 'Search for a voter ...'],
'pluginOptions' => [
'allowClear' => true,
'minimumInputLength' => 3,
'language' => [
'errorLoading' => new JsExpression("function () { return 'Waiting for results...'; }"),
],
'ajax' => [
'url' => $url,
'dataType' => 'json',
'data' => new JsExpression('function(params) { return {q:params.term}; }')
],
'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
'templateResult' => new JsExpression('function(grs_data) { return grs_data.text; }'),
'templateSelection' => new JsExpression('function (grs_data) { return grs_data.text; }'),
],
]);
?>
这是kartik select2小部件。我用它来搜索数据库中的个人数据,并将结果显示为可以选择的列表。当我选择并提交时,属性&#39; personal_no&#39;没有保存到$ model,它是空的。 这是视图的控制器:
public function actionPersonalnos($q = null, $id = null) {
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$out = ['results' => ['id' => '', 'text' => '']];
if (!is_null($q)) {
$query = new Query;
$query->select('id, personal_no AS text')
->from('grs_data')
->where(['like', 'personal_no', $q])
->limit(20);
$command = $query->createCommand();
$data = $command->queryAll();
$out['results'] = array_values($data);
}
elseif ($id > 0) {
$out['results'] = ['id' => $id, 'text' => GrsData::find($id)->personal_no];
}
return $out;
}
一切正常,只是属性没有保存到模型中,这就是问题所在。感谢。