chosen

Anouther chosen extension, provides easy to use InputWidget
22 followers

This extension is a wrapper for Chosen JavaScript plugin which makes long, unwieldy select boxes much more user-friendly.

Requirements

Tested with Yii 1.1.10, but should work with previous versions too

Usage

  1. Checkout source code to your project, for example to ext.chosen
  2. Use it, as any input widget

Example:

$this->widget('ext.chosen.Chosen',array(
   'name' => 'inputName', // input name
   'value' => '2', // selection
   'data' => array( // list of select options
       '1'=>'Option 1',
       '2'=>'Option 2',
       '3'=>'Option 3',
       '4'=>'Option 4',
    ),
));

Also you can use it like CHtml helper

Before this import ext.chosen.Chosen, you can add it to config/main.php or call Yii::import('ext.chosen.Chosen') before usage.

Chosen::dropDownList($name, $select, $data, $htmlOptions);
Chosen::activeDropDownList($model, $attribute, $data, $htmlOptions);
Chosen::multiSelect($name, $select, $data, $htmlOptions);
Chosen::activeMultiSelect($model, $attribute, $data, $htmlOptions);

Changes

  • March 19, 2013 Allow to set placeholder through "data-placeholder" in htmlOptions, when using Chosen::* methods.
  • February 13, 2013 Fixed bug when resolving value by attribute name
  • December 12, 2012 Update chosen to version 0.9.11
  • August 5, 2012 Fixed bug when using as input widget(not like CHtml helper)
  • July 23, 2012 Fixed bug in Chosen::activeMultiSelect

Resources

Total 6 comments

#13084 report it
ineersa at 2013/05/03 06:03am
Adding disable_search function and others

Got some issues when needed to disable search on some selects and found out that you dont have such features. So here is some code :

In file Chosen.php init() function add

if (isset($this->htmlOptions['disable_search']))
            $this->settings['disable_search'] = $this->htmlOptions['disable_search'];

Then in your view :

<?php echo Chosen::activeDropDownList($model, 'gender', array('male' => 'Male', 'female' => 'Female'), array('empty' => "Select gender","disable_search"=>true)); ?>

You can do same for all other features. Hope it will be usefull for someone.

#11897 report it
jneto at 2013/02/11 08:13pm
Problem with Tabular Input ( my solution )

Still don't work with double quotes.

So i find that in all CHtml active widgets, they use

self::resolveNameID($model,$attribute,$htmlOptions);

In the documentation of resolveNameID

 * Generates input name and ID for a model attribute.
 * This method will update the HTML options by setting appropriate 'name' and 'id' attributes.
 * This method may also modify the attribute name if the name
 * contains square brackets (mainly used in tabular input).

So i add the

CHtml::resolveNameID($model,$attribute,$htmlOptions);

in your function and now it works.

public static function activeDropDownList($model, $attribute, $data, $htmlOptions = array())
    {
        CHtml::resolveNameID($model,$attribute,$htmlOptions);
        return self::dropDownList(CHtml::activeName($model, $attribute), CHtml::value($model, $attribute), $data, $htmlOptions);
    }

I'm new in YII, so i don't know if it was the right thing to do.

#11887 report it
Bogdan Savluk at 2013/02/11 10:05am
Re: Problem with Tabular Input

Widget supports tabular input(CHtml::activeName, is used to resolve input name), seems problem is in quotes used for attribute name, try with double quotes: Chosen::activeDropDownList($car, "[$i]state", $list, $htmlOptions);

#11883 report it
jneto at 2013/02/11 08:13am
Problem with Tabular Input

I can't use Chosen::activeDropDownList($car, '[$i]state', $list, $htmlOptions);

Thanks.

#10616 report it
Bogdan Savluk at 2012/11/08 06:09am
Re: Why property $settings is set as private in Chosen.php?

Because there are separate properties in widget, to change this settings: ($placeholderMultiple, $placeholderSingle, $allowSingleDeselect, $noResults). Also some properties have internationalized default values(originally script has no i18n).
If I have missed some configuration options: add issue or make pull request on bitbucket - I will add them.

#10613 report it
letsjump at 2012/11/08 03:40am
Great work!

You saved my life, dude!

Just one question:

Why property $settings is set as private in Chosen.php? Are there other ways to pass the javascript options to .chosen({$settings})?

Thanks a lot!

Leave a comment

Please to leave your comment.

Create extension