H
H
hollanditkzn2017-07-21 17:00:26
Yii
hollanditkzn, 2017-07-21 17:00:26

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.
Here is my code, but the link is not displayed
like this. I tried another example like this, because I don’t fully understand this widget in order to change the inscription to
the Form button
<?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; }'),
                    ]
            ])?>

In form controller
public function actionCreate()
    {
        $model = new Zakaz();
        $client = new Client();
        ...
        }

        return $this->render('create', [
            'model' => $model,
            'client' => $client,
        ]);
    }

And in the ClientController controller
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;
    }

I guess I spelled correctly

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hollanditkzn, 2017-07-24
@hollanditkzn

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 question

Ask a Question

731 491 924 answers to any question