diiimonn/yii2-widget-checkbox-multiple

此包的最新版本(v1.0.0)没有可用的许可证信息。

用于渲染多选框的输入小部件

安装: 826

依赖: 0

建议者: 0

安全: 0

星标: 5

关注者: 4

分支: 3

开放问题: 0

语言:JavaScript

类型:yii2-extension

v1.0.0 2015-02-23 12:37 UTC

This package is not auto-updated.

Last update: 2024-09-28 17:17:49 UTC


README

用于渲染多选框的输入小部件。使用jQuery Ajax。

安装

使用composer安装

$ php composer.phar require diiimonn/yii2-widget-checkbox-multiple "dev-master"

或在您的composer.json文件的require部分中添加:

"diiimonn/yii2-widget-checkbox-multiple": "dev-master"

用法

视图

...
use diiimonn\widgets\CheckboxMultiple;
use yii\helpers\Url;
...

<?= $form->field($model, 'books')->widget(CheckboxMultiple::className(), [
    'dataAttribute' => 'name',
    'scriptOptions' => [
        'ajax' => [
            'url' => Url::toRoute(['books']),
        ],
    ],
    'placeholder' => Yii::t('app', 'Select ...'),
]) ?>

或使用ActiveForm

...
use diiimonn\widgets\CheckboxMultiple;
use yii\helpers\Url;
...

<?= CheckboxMultiple::widget([
    'model' => $model,
    'attribute' => 'books',
    'dataAttribute' => 'name',
    'scriptOptions' => [
        'ajax' => [
            'url' => Url::toRoute(['books']),
        ],
    ],
    'placeholder' => Yii::t('app', 'Select ...'),
]) ?>

设置

  • data : 数组,选择选项数据项。数组的键是选项值,数组的值是对应的选项标签。如果没有设置此选项,则'attribute'将是关系名称。
  • dataAttribute : 字符串,关系模型中的属性名称,如果没有设置则为'data'。
  • scriptOptions : 数组,用于自定义脚本设置的选项。
  • placeholder : 字符串
  • options : 数组,用于小部件标签的选项。
  • spinnerOptions : 数组,用于yii2-widget-spinner-canvas

scriptOptions

  • templateItem : 字符串,HTML
  • templateCheckbox : 字符串,HTML
  • templateResultItem : 字符串,HTML
  • templateInput : 字符串,HTML
  • templateResultError : 字符串,HTML
  • templateResultWarning : 字符串,HTML
  • templatePlaceholder : 字符串,HTML
  • warningMessage : 字符串
  • errorMessage : 字符串
  • defaultCheckbox :: 布尔值,如果为'true'且没有选中项,则将选中具有空值的复选框。默认为'true'。
  • limit : 整数,最大选中项数。
  • slimScroll : 数组
  • wait : 整数,在Ajax请求之前的超时时间(毫秒)。

自定义scriptOptions示例

...
'scriptOptions' => [
    'defaultCheckbox' => false,
    'limit' => 10,
    'templateItem' => Html::tag('li', Html::tag('span', '{text}') .
            Html::tag('span', Html::tag('span', '', [
                'class' => 'glyphicon glyphicon-remove',
            ]), [
                'class' => 'checkbox-multiple-remove-item',
            ]), [
                'class' => 'checkbox-multiple-item',
            ]),
    'templateInput' => Html::textInput('checkbox-multiple-input', '', [
            'class' => 'form-control input-sm',
        ]),
    'slimScroll' => [
        'color' => '#333',
        'railOpacity' => 0.5,
        'railColor' => '#666666',
    ],
],

控制器

...
use yii\db\Query;
...

public function actionBooks()
{
    Yii::$app->response->format = 'json';

    $json = new \stdClass();

    $query = new Query();
    $query->select([
        'id' => 'id',
        'text' => 'name'
    ]);
    $query->from(BookModel::tableName());

    if ($search = Yii::$app->request->post('search', '')) {
        $query->where(['like', 'username', $search]);
    }

    $query->orderBy([
        'name' => SORT_ASC
    ]);

    if ($itemsId = Yii::$app->request->post('itemsId', [])) {
        $query->andWhere(['not in', 'id', $itemsId]);
    }

    $query->limit(20);

    $command = $query->createCommand();
    $data = $command->queryAll();
    $json->results = array_values($data);

    return $json;
}