Witam.
Używam Select2 oraz Ajax laoding.
Skonfigurowane według: http://demos.krajee.com/widget-details/select2#usage-ajax.
Działa wyszukiwanie ale nie mogę zaznaczyć żadnej z zaznaczonej opcji.
Widok:
<?= $form->field($model, 'top_surname_surname_id')->widget(Select2::classname(), [
'initValueText' => $cityDesc, // set the initial display text
'options' => ['placeholder' => 'Wpisz nazwisko'],
'pluginOptions' => [
'allowClear' => true,
'minimumInputLength' => 3,
'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(top_surname_surname_id) { return top_surname_surname_id.text; }'),
'templateSelection' => new JsExpression('function (top_surname_surname_id) { top_surname_surname_id.text; }'),
],
]);?>
Kontroler:
public function actionSurnamelist($q = null, $id = null) {
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$out = ['results' => ['top_surname_surname_id' => '', 'text' => '']];
if (!is_null($q)) {
$query = new Query;
$query->select('surname_id AS top_surname_surname_id, surname_name AS text')
->from('surname')
->where(['like', 'surname_name', $q])
->limit(20);
$command = $query->createCommand();
$data = $command->queryAll();
$out['results'] = array_values($data);
}
elseif ($id > 0) {
$out['results'] = ['top_surname_surname_id' => $id, 'text' => Surname::find($id)->surname_name];
}
return $out;
}
Nie robię błędów czasami tutaj:
('function (top_surname_surname_id) { top_surname_surname_id.text; }')
Co powinno być tutaj.
Generalnie top_surname_surname_id jest kluczem obcym do tabeli nazwisk. i tworzę inną tabelę bazującą na nazwiskach to korzystam z klucza obcego.