Fadly Dzil
Fadly Dzil

Reputation: 2206

Adding Select2 data-option in Kartik Select2

Basically, I using Kartik Select2 to create a dropdownlist.

If I have a data like this :

<?php
$mapListNoEstimate = ArrayHelper::map($listNoEstimate, 'id', function ($model, $value) {
    return $model['no_surat'] . ' - ' . $model['level'];
});

In Select2 :

 echo $form->field($model, 'repair_estimate_id')->widget(Select2::className(), [
    'data' => $mapListNoEstimate,
    'theme' => Select2::THEME_CLASSIC,
    'options' => [
        'placeholder' => 'Select an estimate ...',
        'options' => [

        ]
    ],
])

, How can we adding some data-attribute ?

In my case, I want to add data-level in each option.

You know, in legacy select option, we can do like this :

<select>
    <option value='$model["id"]' data-level= '$model["level"]'> 
        $model['no_surat'] . ' - ' . $model['level']
    </option>
</select>

Please advise.

Upvotes: 1

Views: 2145

Answers (1)

Saji
Saji

Reputation: 1384

You can set the options within options. See the below sample.

echo Select2::widget([
'name' => 'kv-type-01',
'data' => [1 => "First", 2 => "Second", 3 => "Third", 4 => "Fourth", 5 => "Fifth"],
'options' => [
    'placeholder' => 'Select a type ...',
    'options' => [
        1 => ['data-level' => 'something 1'],
        2 => ['data-level' => 'something 2'],
        3 => ['data-level' => 'something 3'],
        4 => ['data-level' => 'something 4'],
        5 => ['data-level' => 'something 5'],
    ]
],
]);

Upvotes: 1

Related Questions