skeeks/yii2-widget-dual-listbox

为 Yii 2 的双列表框小部件

dev-master 2017-11-09 06:04 UTC

This package is auto-updated.

Last update: 2024-08-29 04:31:31 UTC


README

为 Yii 框架 2.0 提供的双列表框。

描述

skeeks\widget\duallistbox\WidgetDualListbox 小部件是 Bootstrap Dual Listbox 的 Yii 2 封装。

要求

  • Yii 版本 2.0.0 或更高

使用方法

  1. skeeks/yii2-widget-dual-listbox 添加到您的项目 composer.json 文件中,并让 Composer 配置您的项目。

    "require": {
        "php": ">=5.4.0",
        "yiisoft/yii2": "*",
        "yiisoft/yii2-bootstrap": "*",
        "yiisoft/yii2-swiftmailer": "*",
        "skeeks/yii2-widget-dual-listbox": "dev-master"
    },
  2. 在您的视图中使用 skeeks\widget\duallistbox\WidgetDualListbox::widget() 代替 yii\helpers\Html::listBox()yii\helpers\Html::activeListBox()yii\widgets\ActiveField::listBox()

    1. 使用 name 和 selection 替换 Html::listBox()

      use skeeks\widget\duallistbox\WidgetDualListbox;
      ...
      <?php
          $options = [
              'multiple' => true,
              'size' => 20,
          ];
          // echo Html::listBox($name, $selection, $items, $options);
          echo DualListbox::widget([
              'name' => $name,
              'selection' => $selection,
              'items' => $items,
              'options' => $options,
              'clientOptions' => [
                  'moveOnSelect' => false,
                  'selectedListLabel' => 'Selected Items',
                  'nonSelectedListLabel' => 'Available Items',
              ],
          ]);
      ?>
    2. 使用 model 和 attribute 替换 Html::activeListBox()

      use skeeks\widget\duallistbox\WidgetDualListbox;
      ...
      <?php
          $options = [
              'multiple' => true,
              'size' => 20,
          ];
          // echo Html::activeListBox($model, $attribute, $items, $options);
          echo WidgetDualListbox::widget([
              'model' => $model,
              'attribute' => $attribute,
              'items' => $items,
              'options' => $options,
              'clientOptions' => [
                  'moveOnSelect' => false,
                  'selectedListLabel' => 'Selected Items',
                  'nonSelectedListLabel' => 'Available Items',
              ],
          ]);
      ?>
    3. 使用 model 和 attribute 替换 ActiveField::listBox()

      use skeeks\widget\duallistbox\WidgetDualListbox;
      ...
      <?php
          $options = [
              'multiple' => true,
              'size' => 20,
          ];
          // echo $form->field($model, $attribute)->listBox($items, $options);
          echo $form->field($model, $attribute)->widget(WidgetDualListbox::className(),[
              'items' => $items,
              'options' => $options,
              'clientOptions' => [
                  'moveOnSelect' => false,
                  'selectedListLabel' => 'Selected Items',
                  'nonSelectedListLabel' => 'Available Items',
              ],
          ]);
      ?>
  3. 在服务器端收集用户输入,就像使用具有多选的单个列表框一样。请注意,输入值将是一个数组。

    如果您在处理用户输入时遇到困难,请阅读 示例代码,该代码演示了如何使用双列表框处理数组格式中的数据。

skeeks\widget\duallistbox\WidgetDualListbox 的属性

  1. name @var string

    输入名称。

  2. selection @var array

    选定的值。

  3. model @var yii\base\Model

    模型对象。

  4. attribute @var string

    属性名称。

  5. items @var array

    选项数据项。数组键是选项值,数组值是对应的选项标签。

  6. options @var array

    列表框的名称-值对标签选项。

  7. clientOptions @var array

    Bootstrap Dual Listbox 的名称-值对选项。有关详细信息,请参阅 初始化参数对象 部分,该部分位于 Bootstrap Dual Listbox 的官方文档中。

前 6 个属性对应于 Html::listBox()Html::activeListBox()ActiveField::listBox() 中使用的参数。

请注意,您必须使用 name-selection 对或 model-attribute 对。前者用于替换 Html::listBox(),后者用于 Html::activeListBox()ActiveField::listBox()

skeeks!
SkeekS CMS (Yii2) — 快速、简单、高效!
skeeks.com | cms.skeeks.com