Answer the question
In order to leave comments, you need to log in
How to make it give a link when there is no result in the Select2 widget?
I have such a problem that while reading the documentation I can't figure out how to make it so that when the widget shows "no matches found",
<?= $form->field($client, 'phone')->widget(Select2::className(), [
'data' => ArrayHelper::map(Client::find()->all(), 'id', 'phone'),
'options' => ['placeholder' => 'Введите номер телефона'],
'pluginOptions' => [
'allowClear' => true,
'language' => [
'noResults' => new JsExpression('function () { return "<button type=\"button\" class=\"btn btn-info btn-xs\">Добавить в справочник</button>"; }'),
],
]
])?>
gave a link to a modal window where you can create an entry. I googled few such examples, but some I don’t understand what and where. <?php $clientPhone = empty($client->id) ? '' : Client::findOne($client->id)->phone; ?>
<?= $form->field($client, 'phone')->widget(Select2::className(), [
// 'data' => ArrayHelper::map(Client::find()->all(), 'id', 'phone'),
'options' => ['placeholder' => 'Введите номер телефона'],
'pluginOptions' => [
'allowClear' => true,
'ajax' => [
'url' => Url::to(['client/client-items']),
'dataType' => 'json',
'data' => new JsExpression('function(params) { return {id:params.term}; }')
],
'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
'templateResult' => new JsExpression('function(client) { return client.phone; }'),
'templateSelection' => new JsExpression('function (client) { return client.phone; }'),
]
])?>
public function actionCreate()
{
$model = new Zakaz();
$client = new Client();
...
}
return $this->render('create', [
'model' => $model,
'client' => $client,
]);
}
public function actionsClientItem($q = null, $id = null)
{
Yii::$app->response->format = Response::FORMAT_JSON;
$out = ['result' => ['id' => '', 'text' => '']];
if (!is_null($q)) {
$query = new Query();
$query->select('id, phone AS tel')
->from('client')
->where(['like', 'phone', $q])
->limit(20);
$command = $query->createCommand();
$data = $command->queryAll();
$out['results'] = array_values($data);
} elseif ($id > 0){
$out['results'] = ['id' => $id, 'text' => Client::find($id)->phone];
} return $out;
}
Answer the question
In order to leave comments, you need to log in
Solved a problem
<?= $form->field($client, 'phone')->widget(Select2::className(), [
'data' => ArrayHelper::map(Client::find()->all(), 'id', 'phone'),
'options' => ['placeholder' => 'Введите номер телефона'],
'pluginOptions' => [
'allowClear' => true,
'language' => [
'noResults' => new JsExpression('function () { return "<button type=\"button\" class=\"btn btn-primary btn-xs\">Добавить клиента</button>"; }'),
],
'escapeMarkup' => new JsExpression('function (markup) {
return markup;
}')
],
])?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question