ESelect2 is a widget extension for Yii framework. This extension is a wrapper for Select2 Jquery plugin (https://github.com/ivaynberg/select2).
// Working with selector $tags=array('Satu','Dua','Tiga'); echo CHtml::textField('test','',array('id'=>'test')); $this->widget('ext.select2.ESelect2',array( 'selector'=>'#test', 'options'=>array( 'tags'=>$tags, ), )); <?php $this->widget('ext.select2.ESelect2',array( 'name'=>'selectInput', 'data'=>array( 0=>'Nol', 1=>'Satu', 2=>'Dua', ), ); ?> // Working with model <?php $this->widget('ext.select2.ESelect2',array( 'model'=>$model, 'attribute'=>'attrName', 'data'=>array( 0=>'Nol', 1=>'Satu', 2=>'Dua', ), ); ?> // Optgroup $data=array( 'one'=>array( '1'=>'Satu', '2'=>'Dua', '3'=>'Tiga', ), 'two'=>array( '4'=>'Sidji', '5'=>'Loro', '6'=>'Telu', ), 'three'=>array( '7'=>'Hiji', '8'=>'Dua', '9'=>'Tilu', ), ); $this->widget('ext.select2.ESelect2',array( 'name'=>'testing', 'data'=>$data, )); $data=array( '1'=>'Satu', '2'=>'Dua', '3'=>'Tiga', ); // Multiple data $this->widget('ext.select2.ESelect2',array( 'name'=>'ajebajeb', 'data'=>$data, 'htmlOptions'=>array( 'multiple'=>'multiple', ), )); // Placeholder $this->widget('ext.select2.ESelect2',array( 'name'=>'asik2x', 'data'=>$data, 'options'=>array( 'placeholder'=>'Keren ya?', 'allowClear'=>true, ), )); ?>
Total 20 comments
Hi
When attaching an ajax call to the ESelect2 I can't get any dropdownlist on the gui.
View:
Controller:
What am I doing wrong?
Thanks Miguel
The details how one should use this excellent widget together with Multimodelform extension are described here.
Hi,
How could I add new option? I googled and found 'createSearchOptions', but don;t know how to use it.....
I tried by putting on the options array but the result is that select2 is not working ;(
Cheers,
Daniel
I got the updated version through github, and I needed to create a input hidden field to run ajax. I changed the run method, line 66:
Worked for me, hope it helps.
can be found here
Hi, you could try the suggestion in my comment below...
Is there possibility to preselect or save the selection fo the form? Every time the form is validated and the errors are shown, the selection disappear?
select2.min.js : Version: 3.2 Timestamp: Mon Sep 10 10:38:04 PDT 2012
edit: fixed in 20130215 archive, thanks
@Hatem Alimam, @nineinchnick Thank you
New version 3.3 is available.
Hello,
I just want to point out something,
in the description of this extension for using multiple data, the $data elements must have unique ids, that would effect the view and the selection, just to avoid some elements to be removed since some have the same id.
this is the code in the extension description.
and this is how the $data must be defined.
Unique IDs
I'm sorry this might appear as a beginner point, but I had a friend which had a problem in testing this extension as some elements were disappeared from the menu due the duplicate of the ids.
Thanks, nice written extension.
@nk913 Hi, I've read the documents carefully, but still i can't find those variables' sources...(like "state","movie" from the documents). I am not using any model to load the data here, instead, I just use some associative arrays (friends_id=>friends_name) to fill in the drop down. What should I do?
Thanks so much!
Please refer to http://ivaynberg.github.com/select2/, you will find the complete documentation about those functions
@nk913, Thanks for your reply!
I did checked the comment but I don't quite get where is the variable "movie" comes from.
What does this should be , for example, in my case?
Thanks!
@jiaming use formatSelection and formatResult options, please check this comment #c9090
Dear all, I have a dropdown like this:
Is there a way i can format the result so it can show the avatar of the friends and the username ?
Thanks!
In one of my applications I needed to initialize the select2 instance with some preselected values, which I had to load via ajax. I had to tinker a bit with the initSelection option to get it to work.
Here is my code, in case anybody needs something similar...
In the view file we have:
The action that is called via ajax and provides the preselected model-data looks like this:
Thanks for this great extension and best regards...
Hi, How to HTML-encode the displayed data?
As a example, one of the items displayed has the following string stored in the DB:
<b>t</b>extand I'd like to display it that way.If you use the following:
The data won't be HTML-encoded and will be presented as-is. This is, the HTML will be rendered and in this example it will display as: text (with the 't' in bold) Yii documentation suggests to use encodeArray. However, if you do the following:
The data will be presented as if it was double HTML-encoded. This is, it will show
&lt;b&gt;t&lt;/b&gt;extinstead of<b>t</b>extHow to correctly HTML-encode the data? Thank you.
@dkrochmalny
Hey guys I have a dropdown that is dependent on the value input into a select 2 dropdown. I have made dependent dropdowns with a regular select box, but the select 2 widget doesn't have an attribute called AJAX. Can anyone help me with the syntax?
Leave a comment
Please login to leave your comment.