yii2-dual-listbox ¶
- Description
- Requirements
- Installation
- Usage
- Properties of softark\duallistbox\DualListbox
- Source Code & Download
- History
Dual Listboxt for Yii framework 2.0.
Description ¶
softark\duallistbox\DualListbox widget is a Yii 2 wrapper for Bootstrap Dual Listbox.
Requirements ¶
- Yii Version 2.0.0 or later
Installation ¶
Add softark/yii2-dual-listbox
in your project's composer.json
, and let Composer configure your project.
"require": {
"php": ">=5.4.0",
"yiisoft/yii2": "*",
"yiisoft/yii2-bootstrap": "*",
"yiisoft/yii2-swiftmailer": "*",
"softark/yii2-dual-listbox": "dev-master"
},
Usage ¶
Use softark\duallistbox\DualListbox::widget()
in place of yii\helpers\Html::listBox()
, yii\helpers\Html::activeListBox()
, or yii\widgets\ActiveField::listBox()
in your view.
- Replacing Html::listBox() using name and selection
use softark\duallistbox\DualListbox;
...
<?php
$options = [
'multiple' => true,
'size' => 20,
];
// echo Html::listBox($name, $selection, $items, $options);
echo DualListbox::widget([
'name' => $name,
'selection' => $selection,
'items' => $items,
'options' => $options,
'clientOptions' => [
'moveOnSelect' => false,
'selectedListLabel' => 'Selected Items',
'nonSelectedListLabel' => 'Available Items',
],
]);
?>
- Replacing Html::activeListBox() using model and attribute
use softark\duallistbox\DualListbox;
...
<?php
$options = [
'multiple' => true,
'size' => 20,
];
// echo Html::activeListBox($model, $attribute, $items, $options);
echo DualListbox::widget([
'model' => $model,
'attribute' => $attribute,
'items' => $items,
'options' => $options,
'clientOptions' => [
'moveOnSelect' => false,
'selectedListLabel' => 'Selected Items',
'nonSelectedListLabel' => 'Available Items',
],
]);
?>
- Replacing ActiveField::listBox() using model and attribute
use softark\duallistbox\DualListbox;
...
<?php
$options = [
'multiple' => true,
'size' => 20,
];
// echo $form->field($model, $attribute)->listBox($items, $options);
echo $form->field($model, $attribute)->widget(DualListbox::className(),[
'items' => $items,
'options' => $options,
'clientOptions' => [
'moveOnSelect' => false,
'selectedListLabel' => 'Selected Items',
'nonSelectedListLabel' => 'Available Items',
],
]);
?>
And collect the user input in the server side, just as you do with a single Listbox with multiple selection. Note that the input value will be an array.
Properties of softark\duallistbox\DualListbox ¶
name @var string
The input name.
selection @var array
The selected values.
model @var yii\base\Model
The model object.
attribute @var string
The attribute name.
items @var array
The option data items. The array keys are option values, and the array values are the corresponding option labels.
options @var array
The tag options for the listbox in terms of name-value pairs.
clientOptions @var array
The options for the Bootstrap Dual Listbox in terms of name-value pairs. See Initialzation parameters object section of the official documentation of Bootstrap Dual Listbox for details.
The first 6 properties correspond to the parameters used in Html::listBox()
, Html::activeListBox()
and ActiveField::listBox()
.
Note that you have to use either name-selection pair or model-attribute pair. The former is for replacing Html::listBox()
and the latter is for Html::activeListBox()
and ActiveField::listBox()
.
When you use model-attribute pair, the target attribute should be an array. Take a look at the following wiki for detailed information.
How to use Listbox and CheckboxList
Source Code & Download ¶
History ¶
- Version 1.0.0 (2016-01-12)
- Tested on Yii 2.0.6
Pre-Selection on ActiveField and ActiveListBox?
Hi Softark,
First of all:
Thank you for this little piece of gold!
Just one question:
Is itsomehow possible to pre-select items when using the widget as replacement for ActiveListBox or ActiveField? ... Pre-selecting items with Html::listBox() replacement works without any problems...
But when I try for example:
$query = User::find()->all(); $items = ArrayHelper::map($query, 'id', 'username'); $selection = [ 2, 4 ]; $options = [ 'multiple' => true, 'size' => 20, ]; echo $form->field($model, 'username')->widget( DualListbox::className(),[ 'selection' => $selection, 'items' => $items, 'options' => $options, 'clientOptions' => [ 'moveOnSelect' => false, 'selectedListLabel' => 'Selected Items', 'nonSelectedListLabel' => 'Available Items', ], ] );
The "Selected Items" List is just empty.
If I'm too stupid and it should actually work,
could you point me in the right direction? Thanks. :)
Best Regards
name-selection / model-attribute
As I wrote in the article, you have to use name-selection pair when you want to replace Html::listBox(), and model-attribute pair when you want to replace Html::activeListBox() or ActiveField::listBox(). This is because the underlying methods require such parameters.
In Html::activeListBox() and ActiveField::listBox(), pre-selecton must be automatically constructed from the model's attribute, which is an array.
$model->users = [2, 4]; echo $form->field($model, 'users')->widget(DualListbox::className(),[ 'items' => $items, 'options' => $options, 'clientOptions' => [ ... ], ]);
Please take a look at the following wiki, too.
How to use Listbox and CheckboxList
Save the selected to database
Hi, I've successfully show the dual listbox in my view. But I don't know how save the selected value (right side box) to database.
Anybody know how to solve this?
Thanks before:)
How to save the selected items
Please take a look at another wiki article
How to Use ListBox and CheckboxList
And just replace the ListBox (or CheckboxList) with the dual list box.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.