nex/yii2-chosen

Bootstrap Chosen widget for Yii2

安装数: 75,578

依赖者: 2

建议者: 0

安全性: 0

星标: 27

关注者: 4

分支: 12

开放问题: 1

类型:yii2-extension

v1.0 2016-06-07 15:49 UTC

This package is not auto-updated.

Last update: 2024-09-14 15:58:05 UTC


README

致谢

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

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

安装

安装此扩展的最佳方式是通过 composer

运行以下命令之一:

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

或添加以下内容到您的应用 composer.json 文件的 require 部分:

"nex/yii2-chosen" : "*"

require

用法

使用模型

<?php
use nex\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
        'clientOptions' => [
            'search_contains' => true,
            'single_backstroke_delete' => false,
        ],
]);?>

不使用模型

<?php
use nex\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
    'clientOptions' => [
        'search_contains' => true,
        'max_selected_options' => 2,
    ],
]);?>

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

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

您也可以在调用小部件时覆盖占位符文本(这对于宽度较小的选择非常有用)

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

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