php - Can't find value in select2 ajax loading in yii2 -
i use select2 ajax loading. code link: http://demos.krajee.com/widget-details/select2. when enter words field, display data/value, can't automatic select data/value according words enter field. so, select2 select first data/value , display value. what's problem? these codes:
_form.php
$url = url::to(['/paket/jsonlist']); $citydesc = empty($model->no_induk) ? '' : penerima::findone($model->no_induk)->nama; echo $form->field($model, 'no_induk')->widget(select2::classname(), [ 'initvaluetext' => $citydesc, // set initial display text 'options' => ['placeholder' => 'search city ...'], 'pluginoptions' => [ 'allowclear' => true, 'minimuminputlength' => 1, 'language' => [ 'errorloading' => new jsexpression("function () { return 'waiting 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(no_induk) { return no_induk.text; }'), 'templateselection' => new jsexpression('function (no_induk) { return no_induk.id; }'), ], ]);
my controller:
public function actionjsonlist($q = null, $id = null) { \yii::$app->response->format = \yii\web\response::format_json; $out = ['results' => ['id' => '', 'text' => '']]; if(!is_null($q)) { $query = new \yii\db\query; $mainquery = $query->select('no_induk id, nama text') ->from('penerima') ->limit(20); $command = $mainquery->createcommand(); $data = $command->queryall(); $out['results'] = array_values($data); } elseif ($id > null) { $out['results'] = ['id' => $id, 'text' => \frontend\models\penerima::find($id)->nama]; } return $out; }
could use attribute name , not vars
echo $form->field($model, 'no_induk')->widget(select2::classname(), [ 'initvaluetext' =>'citydesc', // set initial display text
or
echo $form->field($model, 'no_induk')->widget(select2::classname(), [ 'initvaluetext' =>$model->citydesc, // set initial display text
Comments
Post a Comment