H
H
hollanditkzn2017-07-24 16:55:17
Yii
hollanditkzn, 2017-07-24 16:55:17

How to display get request in select2?

How to make it so that when they go to the get request, then in select2 it is displayed in value the value that came from the get request. Tried like this
Link is crm/frontend/web/create?phone=81111111111

<?= $form->field($client, 'phone')->widget(Select2::className(), [
                    'data' => ArrayHelper::map(Client::find()->all(), 'id', 'phone', 'fio'),
                    'value' => $_GET['phone'],//здесь должна отобразиться значение 81111111111
                    'options' => ['placeholder' => 'Введите номер телефона'],
                    'pluginOptions' => [
                        'allowClear' => true,
                        'language' => [
                            'noResults' => new JsExpression('function () { return "<a href=\"'. Url::to(['client/create']).'\">Добавить клиента</button>"; }'),
                        ],
                        'escapeMarkup' => new JsExpression('function (markup) {
        return markup;
    }')
                    ],
            ])?>

Maybe I'm doing something wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2017-07-25
@hollanditkzn

'value' => $_GET['phone'],//здесь должна отобразиться значение 81111111111

you have data in the list in the format - ID -> phone number, i.e. in value you must substitute the identifier of this phone and not its number. And use Yii::$app->request->get('phone') to get the parameter value

M
Maxim Timofeev, 2017-07-25
@webinar

What do you think happens here?
And it turns out there:

[
'fio1'=>['id1'=>'phone1','id2'=>'phone2'],
'fio2'=>['id3'=>'phone3','id4'=>'phone4'],
]

That is, the keys of the array are the name and it will work only if you have the name that matches the phone number passed through get. And a multidimensional array for select is not exactly what you need.
As far as I understand it should be like this:
ArrayHelper::map(Client::find()->all(), 'phone', 'phone'),

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question