【问题标题】:how to apply select2 in Yii2 dropdown list如何在 Yii2 下拉列表中应用 select2
【发布时间】:2017-04-19 18:43:28
【问题描述】:

在 yii2 基本 ActiveForm 中,我添加了一个下拉列表,并在其中使用“选项”数组添加我的属性。我想在我的下拉列表中添加 select2,但它不起作用。我在网上搜索过并找到了我在下面使用过的“选项”数组的这种方法,但在视图中仍然没有显示输出

<div class="col-xs-6 col-lg-6">
     <div class="form-group form-material floating" data-plugin="formMaterial">
       <div class="example"> <b role="presentation"></b>
        <?= $form->field($model, 'corporation_status')->dropDownList(Yii::$app->appstatues->status,['options'=>['class'=>'form-control','prompt'=>'abc','data-plugin'=>'select2']])->label('Stat'); ?>
        </div>
       </div>
      </div>

任何帮助将不胜感激。

【问题讨论】:

标签: drop-down-menu yii2 select2


【解决方案1】:

使用扩展名https://github.com/kartik-v/yii2-widget-select2

还有很多不错的扩展: http://demos.krajee.com/

【讨论】:

  • 我们不能通过在选项数组中的任何位置使用“data-plugin”=>'select2' 来做到这一点吗?这不是正确的方法吗?
  • 正确的做法是为 JS 库 Select2 使用 Yii2 包装器(Kartik 的包装器是最常用的)。 data-plugin 不会工作,除非你有我们不知道的进一步逻辑。您可以自己在具有该属性的元素上启动 Select2,但是包装器太好了,不能不使用它,很多人都在使用它,而且著名的​​包装器通常维护得很好。
【解决方案2】:

找到要添加到下拉列表中的“select2”的解决方案,请执行以下步骤: 将以下 css 和 js 文件添加到您的“AppAsset”:

https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css
https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js

并在表单字段中添加'data-plugin'=>'select2',如下所示:

 <?= $form->field($model, 'corporation_status')->dropDownList(Yii::$app->appstatues->status,['prompt' => 'hello','data-plugin'=>'select2','class'=>'form-control'])->label('Status'); ?>

【讨论】:

    【解决方案3】:

    运行此命令为 yii2 安装 kartik\select2 小部件

    php composer.phar require kartik-v/yii2-widget-select2 "@dev"
    

    然后使用小部件。

    //Include kartik\select2 Class
    use kartik\select2\Select2;
    
    // Normal select with ActiveForm & model
    echo $form->field($model, 'corporation_status')->widget(Select2::classname(), [
        'data' => Yii::$app->appstatues->status,
        'options' => ['prompt' => 'Select a state ...'],
        'pluginOptions' => [
            'allowClear' => true
        ],
    ]);
    

    【讨论】:

      猜你喜欢
      • 2020-04-14
      • 1970-01-01
      • 2016-03-12
      • 2014-03-01
      • 1970-01-01
      • 2018-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多