选择2 kartik数值更改为索引值

时间:2015-09-07 10:35:14

标签: yii2 jquery-select2 yii2-model

我有这样的数据:
enter image description here

我使用这些数据来填充select2 kartik组合框,这是我的yii2代码,

WITH 
  MEMBER measures.NumberOfSameWeekDays AS 
    Count([Date].[Day of Week].CurrentMember) 
  MEMBER measures.WeekDayCurrentMem AS 
    [Date].[Day of Week].CurrentMember.Member_Caption 
SELECT 
  {
    measures.NumberOfSameWeekDays
   ,measures.WeekDayCurrentMem
  } ON COLUMNS
 ,[Date].[Day of Week].[Day of Week] ON ROWS
FROM [Adventure Works]
WHERE 
    [Date].[Calendar].[Date].&[20050101]
  : 
    [Date].[Calendar].[Date].&[20050116];

uname 字段作为select2和nama的值作为显示值。但结果是这样的:
enter image description here

但是当uname字段的值为number时,select2会自动更改select2项的数组索引。

希望有些人给我解决。

感谢。

3 个答案:

答案 0 :(得分:1)

试试这个:

  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
                            ],
                        ]);

找到它here

答案 1 :(得分:0)

因为array_merge。你为什么要使用它,因为你有一个选项“allowClear”= true来允许空选?

如果删除array_merge,则不会更改索引。

只需添加此项以允许空选择并为其命名。

'filterWidgetOptions'=>[
            'pluginOptions' => ['allowClear' => true],
 ],
'filterInputOptions' => ['placeholder' => \Yii::t('app', 'Any Entry')],

我在过滤器上测试了设置,但普通小部件的设置类似:

'options' => ['placeholder' => 'Any entry'],
'pluginOptions' => [
    'allowClear' => true
],

对我来说很完美...... 所以在你的情况下它只是

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
],
]);

答案 2 :(得分:0)

正如前面提到的,问题确实存在于array_merge中,它重新索引数值数组,数字字符串也是如此,在这种情况下结果相同

[9 => 'foo'] 
['9' => 'foo']

我认为所需的功能是具有“普通”选择标记的外观,能够选择“空”第一个选项,在select上设置空值。

我一直在寻找并找到了添加数组的简单解决方案

['' => 'Select value...'] + [0 => false, 1 => true]

使用空的第一个选项元素,设置不应包含placeholder,因为它不会在选项列表中显示它,而是将它作为选项名称显示。 allowClear可能设置为false,或者将其保留为默认状态

导致:

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
    ],
]);

来源:PHP: merge two arrays while keeping keys instead of reindexing?