开源软件工厂 / yii2-select3
一个基于 jQuery 的 DropdownList 组件,将复选框作为选项显示。
1.0007
2017-10-12 00:30 UTC
README
显示介于 DropdownList 和可多选复选框之间的混合。
平台:Yii 框架 2.0
作者:Cristian Salazar chileshift.cl/freesoftwarefactory
预览
使用方法
- 通过 composer
"require": {
"php": ">=5.4.0",
"freesoftwarefactory/yii2-select3": "(put version here)"
},
- 在您的视图中
<?php
use yii\widgets\ActiveForm;
use freesoftwarefactory\select3\Select3Widget;
$options =
[
'opt1' => "Chevrolet",
'opt2' => "Mazda",
'opt3' => "Audi",
];
?>
<?php $form = ActiveForm::begin( [ 'id'=>'form1', ]); ?>
<div class='row'>
<div class='col-md-3'>
<?=$form->field($model, 'someAttributeInYourModel')
->widget(Select3Widget::className(),
[
'prompt' => 'Select Provider',
'options' => $options,
'allSelectable' => true,
'allSelectableLabel' => "(All)",
'visibleAtStartup' => false,
]) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
接收此控件值
值是 base64+json 编码的,也就是说,当您使用此控件提交表单时,您将得到类似的内容
eyJvcHQxIjp0cnVlLCJvcHQyIjp0cnVlLCJvcHQzIjpmYWxzZX0=
这是上述内容的 base64 编码版本
{"opt1":true,"opt2":true,"opt3":false}
所以,您只需调用
$values = json_decode(base64_decode($model->yourAttribute),true);
或者...只需调用 :)
$values = Select3Widget::getDecodedValueFrom($model, 'yourAttribute');
更多功能
- 通过调用 JavaScript 加载项目
$.fn.select3load($('#widgetid') , { 123 : some , 456 : thing });
- 通过传递来禁用控件
[
'disable'=>true,
]
- 在启动时禁用/选择一些项目,通过调用
[
'disabledOptions' => ['somekeydisabled'],
'autoSelectOptions' => ['somekeySelectedFromTheBeginning'],
]
- 监听变化的 JavaScript 事件
$this->registerJs("
$(document).on('select3-changed', [], function(e, options, widget){
console.log('select3-changed', options, widget);
});
");