bogdik / yii2-chosen

Bootstrap Chosen 小部件用于 Yii2

安装: 26

依赖关系: 0

建议: 0

安全性: 0

星标: 0

关注者: 2

分支: 12

类型:yii2-extension

1.1.7 2018-01-22 15:42 UTC

This package is not auto-updated.

Last update: 2024-09-23 06:34:42 UTC


README

致谢

Chosen http://harvesthq.github.io/chosen/

Chosen Bootstrap Look & Feel https://github.com/dbtek/chosen-bootstrap(包含我的样式修正)

安装

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

可以运行

php composer.phar require "bogdik/yii2-chosen" "*"

或者在应用程序的 composer.json 文件的 require 部分添加

"bogdik/yii2-chosen" : "*"

to

使用

使用模型

<?php
use bogdik\chosen\Chosen;
?>

<?= Chosen::widget([
    'model' => $model,
    'attribute' => 'selectable_attr',
    'items' => [1 => 'First item', 2 => 'Second item', 3 => 'Third item'],
    'multiple' => true,
]);?>

<?= $form->field($model, 'selectable_attr')->widget(
    Chosen::className(), [
        'items' => [1 => 'First item', 2 => 'Second item', 3 => 'Third item'],
        'disableSearch' => 5, // Search input will be disabled while there are fewer than 5 items
	'disablesArray' => [1,2],
        'clientOptions' => [
            'search_contains' => true,
            'single_backstroke_delete' => false,
        ],
]);?>

不使用模型

<?php
use bogdik\chosen\Chosen;
?>
<?= Chosen::widget([
    'name' => 'ChosenTest',
    'value' => 3,
    'items' => [1 => 'First item', 2 => 'Second item', 3 => 'Third item'],
    'allowDeselect' => false,
    'disableSearch' => true, // Search input will be disabled
    'disablesArray' => [1,2],
    'clientOptions' => [
        'search_contains' => true,
        'max_selected_options' => 2,
    ],
]);?>

要覆盖默认的占位符字符串,您可以将 Select an option(单选)和 Select some options(多选)的翻译消息添加到您的应用程序消息文件中。默认情况下,小部件将使用 'app' 类别来翻译这些字符串,但您可以通过在控件配置中更改 translateCategory 选项来自定义类别。例如,您可以在应用程序配置文件中写下此内容

<?php
\Yii::$container->set('bogdik\chosen\Chosen', [
    'translateCategory' => 'my-app',
]);

您还可以在调用小部件时覆盖占位符文本(对于宽度较小的选择器可能很有用)

<?php
use bogdik\chosen\Chosen;
?>
<?= Chosen::widget([
    'name' => 'ChosenTest',
    'value' => 3,
    'items' => [1 => 'First item', 2 => 'Second item', 3 => 'Third item'],

    'placeholder' => 'Select',
]);?>