neovash23 / yii2-chosen
为 Yii2 优化的 Bootstrap Chosen 小部件
v1.0
2016-06-07 15:49 UTC
Requires
- bower-asset/chosen-bootstrap: 1.1.*
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2024-09-29 20:09:43 UTC
README
鸣谢
Chosen http://harvesthq.github.io/chosen/
Chosen Bootstrap 界面与风格 https://github.com/dbtek/chosen-bootstrap(包含我的样式修复)
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一
php composer.phar require "neovash23/yii2-chosen" "*"
或者在您的应用程序的 composer.json
文件的 require
部分添加
"neovash23/yii2-chosen" : "*"
到
使用方法
与模型一起使用
<?php
use neovash23\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 neovash23\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('neovash23\chosen\Chosen', [
'translateCategory' => 'my-app',
]);
您还可以在调用小部件时覆盖占位符文本(对于宽度较小的选择很有用)
<?php
use neovash23\chosen\Chosen;
?>
<?= Chosen::widget([
'name' => 'ChosenTest',
'value' => 3,
'items' => [1 => 'First item', 2 => 'Second item', 3 => 'Third item'],
'placeholder' => 'Select',
]);?>