user7961310
user7961310

Reputation:

Show Select2 option as selected

 <?=
$form->field($model, 'contributors')->widget(Select2::classname(), [
    'initValueText' => '',
    'options' => ['placeholder' => 'Search for a Contributor ...'],
    'pluginOptions' => [
        'allowClear' => true,
        'minimumInputLength' => 3,
        'ajax' => [
            'url' => \yii\helpers\Url::to(['data2']),
            'dataType' => 'json',
            'data' => new JsExpression('function(params) { return {q:params.term}; }')
        ],
        'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
        'templateResult' => new JsExpression('function(contributor_id) { return contributor_id.text; }'),
        'templateSelection' => new JsExpression('function (contributor_id) { return contributor_id.text; }'),
    ],
])->label('Contributors');
?>

How to show selected value, now it shows id as a selected. please anyone help me.

Upvotes: 1

Views: 845

Answers (1)

Muhammad Omer Aslam
Muhammad Omer Aslam

Reputation: 23740

UPDATE

Sorry for late reply didn't notice you need to set the initial text for the select2 as you are using the ajax option so you should use the option initValueText. According to the docs

initValueText : The text to displayed in Select2 widget for the initial value. This is useful and applicable when you are using the widget with ajax loaded data AND/OR you are not providing the data. Check the ajax usage section for an example.

So you need to use it like below add this line inside your controller action and pass it to the view or add it inside the view before the select2

$contributorName = empty($model->contributors) ? '' : Contributors::findOne($model->contributors)->name;

<?=
$form->field($model, 'contributors')->widget(Select2::classname(), [
    'initValueText' => $contributorName, // set the initial display text
    'options' => ['placeholder' => 'Search for a Contributor ...'],
     'data'=>yii\helpers\ArrayHelper::map(Contributors::find()->all(),'id','name')
    'pluginOptions' => [
        'allowClear' => true,
        'minimumInputLength' => 3,
        'ajax' => [
            'url' => \yii\helpers\Url::to(['data2']),
            'dataType' => 'json',
            'data' => new JsExpression('function(params) { return {q:params.term}; }')
        ],
        'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
        'templateResult' => new JsExpression('function(contributor_id) { return contributor_id.text; }'),
        'templateSelection' => new JsExpression('function (contributor_id) { return contributor_id.text; }'),
    ],
])->label('Contributors');
?>

You need to provide the data option to the set of values from which the selected value would be shown you want the text to be shown instead of the id, provide the data options in form of an array with key=>value pairs.

Looking at your code you want to show the contributor name against the id in database table so you should use ArrayHelper:map() along with querying your Contributors model, see below and update your field names for the Contributors model/table inside the ArrayHelper::map()

<?=
$form->field($model, 'contributors')->widget(Select2::classname(), [
    'options' => ['placeholder' => 'Search for a Contributor ...'],
     'data'=>yii\helpers\ArrayHelper::map(Contributors::find()->all(),'id','name')
    'pluginOptions' => [
        'allowClear' => true,
        'minimumInputLength' => 3,
        'ajax' => [
            'url' => \yii\helpers\Url::to(['data2']),
            'dataType' => 'json',
            'data' => new JsExpression('function(params) { return {q:params.term}; }')
        ],
        'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
        'templateResult' => new JsExpression('function(contributor_id) { return contributor_id.text; }'),
        'templateSelection' => new JsExpression('function (contributor_id) { return contributor_id.text; }'),
    ],
])->label('Contributors');
?>

Upvotes: 3

Related Questions