Reputation: 337
I use these data to fill the select2 kartik combobox, this is my yii2 code,
echo \kartik\widgets\Select2::widget([
'attribute' => 'pembuatSoal_id',
'model' => $model,
'data' => array_merge(["" => ""], \yii\helpers\ArrayHelper::map(\app\models\ViewUsernameGuru::find()->all(), "uname", "nama")),
'options' => ['placeholder' => 'Pilih Guru...', 'id' => 'guru-id', 'class' => "form-control"],
'pluginOptions' => [
'allowClear' => true,
'theme' => \kartik\widgets\Select2::THEME_BOOTSTRAP
],
]);
uname field as the value of the select2 and nama as the display value. but the result is like this:
But when The value of uname field is number, select2 automaticly change with the array index of select2 items.
Hope some body give me the solve.
Thanks.
Upvotes: 2
Views: 3055
Reputation: 1162
As it was mentioned the problem truly is in array_merge
which reindexes numeric arrays, numeric strings too, following results in same in this case
[9 => 'foo']
['9' => 'foo']
I believe desired functionality was to have appearance of "normal" select tag with ability to select "empty" first option which sets empty value on select.
I kept searching and found simple solution which is to do addition of arrays
['' => 'Select value...'] + [0 => false, 1 => true]
With empty first option element, setting should not contain placeholder
, because it does not display it in option list, rather us it as option name.
allowClear
may be set to false, or leave it out as it is default state
resulting in:
echo \kartik\widgets\Select2::widget([
'attribute' => 'pembuatSoal_id',
'model' => $model,
'data' => (["" => "Pilih Guru..."] + \yii\helpers\ArrayHelper::map(\app\models\ViewUsernameGuru::find()->all(), "uname", "nama")),
'options' => ['id' => 'guru-id', 'class' => "form-control"],
'pluginOptions' => [
'allowClear' => false, // false is default so could be left out
'theme' => \kartik\widgets\Select2::THEME_BOOTSTRAP,
'dropdownAutoWidth' => 'true', // to autocalculate width of selection list
],
]);
source: PHP: merge two arrays while keeping keys instead of reindexing?
Upvotes: 0
Reputation: 9358
try this:
echo \kartik\widgets\Select2::widget([
'attribute' => 'pembuatSoal_id',
'model' => $model,
'data' => \yii\helpers\ArrayHelper::map(\app\models\ViewUsernameGuru::find()->all(), "uname", "nama")),
'options' => ['placeholder' => 'Pilih Guru...', 'id' => 'guru-id', 'class' => "form-control"],
'pluginOptions' => [
'allowClear' => true,
'theme' => \kartik\widgets\Select2::THEME_BOOTSTRAP
],
]);
find it here:
Upvotes: 1
Reputation: 1663
Its because of the array_merge. Why do you use it because you have the option "allowClear" = true to allow an empty selection?
If you remove the array_merge the indexes won't be changed.
Just add this to allow an empty selection and give it a name.
'filterWidgetOptions'=>[
'pluginOptions' => ['allowClear' => true],
],
'filterInputOptions' => ['placeholder' => \Yii::t('app', 'Any Entry')],
i tested the settings on a filter but the settings for the normal widget are similar:
'options' => ['placeholder' => 'Any entry'],
'pluginOptions' => [
'allowClear' => true
],
works perfectly for me... so in your case it is just
echo \kartik\widgets\Select2::widget([
'attribute' => 'pembuatSoal_id',
'model' => $model,
'data' => \yii\helpers\ArrayHelper::map(\app\models\ViewUsernameGuru::find()->all(), "uname", "nama"),
'options' => ['placeholder' => 'Pilih Guru...', 'id' => 'guru-id', 'class' => "form-control"],
'pluginOptions' => [
'allowClear' => true,
'theme' => \kartik\widgets\Select2::THEME_BOOTSTRAP
],
]);
Upvotes: 0