select2 选择框
1、多选:
PHP Code复制内容到剪贴板
- use kartik\select2\Select2;
- <?= $form->field($model, 'depot_ids')->widget(Select2::classname(), [
- 'data' => \backend\modules\material\models\Depot::getAllDropDownList(),
- 'options' => [
- 'multiple' => true,
- 'placeholder' => '选择仓库'
- ],
- 'pluginOptions' => [
- 'allowClear' => true
- ],
- ]);
- ?>
树列表:
PHP Code复制内容到剪贴板
- use common\helpers\Tree;
- /**
- * 获取选择仓库列表
- * @param array $tree
- * @param array $result
- * @param int $deep
- * @param string $separator
- * @return array
- */
- public static function getDropDownList($tree = [], &$result = [], $deep = 0, $separator = '----')
- {
- $deep++;
- foreach ($tree as $list) {
- $result[$list['id']] = str_repeat($separator, $deep - 1) . $list['name'];
- if (isset($list['children'])) {
- self::getDropDownList($list['children'], $result, $deep);
- }
- }
- return $result;
- }
- /**
- * 获取全部仓库的下拉列表
- * @return array
- */
- public static function getAllDropDownList(){
- $res = self::getDropDownList(Tree::build(self::find()->orderBy("myorder asc")->asArray()->all(), 'id', 'parent_id', 'children', 0));
- return $res;
- }
PHP Code复制内容到剪贴板
- <?= $form->field($model, 'apply_user_id', [
- 'inputOptions' => [
- 'autocomplete' => 'off',
- ],
- 'options' => [
- 'class' => 'form-group',
- 'style' => 'width:200px',
- ]
- ])->widget(\kartik\select2\Select2::classname(), [
- 'data' => \backend\modules\user\models\User::getDropDownListByProjectId(),
- 'options' => [
- 'placeholder' => '选择申请人',
- ],
- 'pluginOptions' => [
- 'allowClear' => true
- ],
- ])->label(false); ?>
下一篇 常用form选择