【问题标题】:Select2 kartik number value change to index valueselect2 kartik 数值改为索引值
【发布时间】:2015-12-02 21:17:12
【问题描述】:

我有这样的数据:

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

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 字段作为 select2 的值,nama 作为显示值。但结果是这样的:

但是当uname字段的值为number时,select2会自动随着select2项的数组索引而变化。

希望有人能帮我解决。

谢谢。

【问题讨论】:

  • array_merge(["" => ""], 是做什么的?
  • 是 --> 'data' => array_merge(["" => ""], \yii\helpers\ArrayHelper::map(\app\models\ViewUsernameGuru::find()- >所有(),“uname”,“nama”)),。你知道如何解决这个问题吗?
  • 您不需要使用array_merge(["" => ""]。因为arrayHelper创建了一个数组。
  • kd_preg 是主键吗?
  • array_merge 到底是为了什么?

标签: yii2 jquery-select2 yii2-model


【解决方案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:

【讨论】:

    【解决方案2】:

    这是因为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
    ],
    ]);
    

    【讨论】:

      【解决方案3】:

      正如之前提到的,问题确实出在array_merge 中,它重新索引了数字数组和数字字符串,在这种情况下结果相同

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

      我相信所需的功能是具有“正常”选择标签的外观,能够选择“空”第一个选项,该选项在选择时设置空值。

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

      ['' => '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?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-16
        • 1970-01-01
        • 2018-05-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多