magicsoft/yii2-select

dev-master 2019-06-10 19:31 UTC

This package is auto-updated.

Last update: 2024-09-11 14:18:29 UTC


README

魔法选择器充分利用了 https://github.com/kartik-v/yii2-widget-select2 的功能,但扩展了其功能,使其无需配置即可动态运行。

安装

安装此扩展的首选方式是通过 composer

运行以下命令之一:

php composer.phar require --prefer-dist magicsoft/yii2-select "*"

"magicsoft/yii2-select": "*"

将以下内容添加到您的 composer.json 文件的 require 部分。

用法

您可以直接从表单中直接使用,小部件将使用动态查询动态构建选择器。

echo $form->field($model, 'attribute_id')->widget(\magicsoft\select\MagicSelect::className(), []);

//With this configuration, the widget assumes that its fields of search and return of data are: 'name' or 'description'

但您可以配置自己的搜索和数据返回字段

echo $form->field($model, 'attribute_id')->widget(\magicsoft\select\MagicSelect::className(), [
     'searchData' => 'code,name,...',
     'returnData' => 'join:code,description' 
])?>

searchData 一个或多个字段,用逗号分隔。
returnData 这可以采取三个选项: join:连接几个字段或属性,attr 模型中的属性,field 数据库中的一个字段。

####配置多个选择器与父选择器

//This is a parent select
echo $form->field($model, 'country_id')->widget(\magicsoft\select\MagicSelect::className(), []);

//This is a second select
echo $form->field($model, 'state_id')->widget(\magicsoft\select\MagicSelect::className(), [
     'parent' => 'country'
]);

//This is a tree select
echo $form->field($model, 'province_id')->widget(\magicsoft\select\MagicSelect::className(), [
     'parent' => 'state'
]);

//... More select

####在 gridView 中使用魔法选择器(适用于 Krajee 的 gridView)

$gridColumns = [
    ['attribute' => 'id', 'visible' => false],
    'name',
    \magicsoft\select\MagicSelect::getDataForGrid([
        'model' => $searchModel,
        'attribute' => 'country_id'
    ]),
    \magicsoft\select\MagicSelect::getDataForGrid([
        'model' => $searchModel,
        'attribute' => 'state_id',
        'parent' => 'cuntry'
    ]),
    ... more columns
]

第二个选择器与第一个选择器相关联,第三个与第二个相关联...

模块

如下所示,在您的 Yii 配置文件中设置模块,命名为 magicsoft。

'modules'=>[
   'magicsoft'=>[
        'class' => \magicsoft\select\Module::className(),
        'encryptOptions' => [
            'secretKey' => '205bdf05272043d',
            'secretIv' => '205bdf0512ea37e',
        ],
        'modelsOptions' => [
            'default' => [
                'icon' => 'fa fa-list',
                'modal' => [true, 'free' => ['index', 'create']]
            ],
            'location' => [
                'icon' => 'fa fa-map-marker',
                'title' => ['singular' => 'Location', 'plural' => 'Locations'],
                'group' => ['parent' => 'location', 'shield' => ''],
                'modal' => [true, 'free' => ['index']]
            ],
        ]
    ]
],
//The encryptOptions, you can use your own values
//The modelsOptions, set the particular values for the views

您的控制器

魔法选择器自动管理编辑和插入表单,因此在控制器中必须使用配置。

use magicsoft\select\controllers\MagicController;

class TestController extends Controller
{
    use MagicController;
    
    public function actionCreate(){
        return $this->save(new Test());
    }
}

在您的视图(创建或更新数据的表单)中

<?php
$form = \magicsoft\form\MagicForm::begin([
    'model' => $model,
]);

echo $form->field($model, 'name')->textInput(['maxlength' => true, 'placeholder' => 'Name']);

$form::end();
?>

请参阅 https://github.com/aramirezarg/yii2-modal 的文档

许可证

MagicSelect 在 BSD 3-Clause 许可证下发布。有关详细信息,请参阅捆绑的 LICENSE.md 文件。