-
-
Notifications
You must be signed in to change notification settings - Fork 178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Selected option in Select2 #87
Comments
The widget automatically shows a selected value if you have set a value for the input (or the input has a value). NOTE: The value set must be the |
Actually, this is not what I was asking about... For instance, I have |
Not sure why this is difficult - one of these options for example, could achieve what you want: $data = [1=>'category 1', 2=>'category 2', 3=>'category 3'];
// without model
echo Select2::widget([
'name' => 'category',
'value' => 2, // value to initialize
'data' => $data
]);
// alternatively with model
$model->category = 2; // value to initialize
echo Select2::widget([
'model' => $model,
'attribute' => 'category',
'data' => $data
]); |
Thanks, this is what I was looking for. I couldn't find it in the docs the example without the model. |
Reading the docs is important... Select2 extends from the Yii Input Widget, which is highlighted in docs as well. |
But how can I change selected option in Javascript after created the widget? |
Check the Select2 plugin documentation for details on various javascript options. // set value
$("#select").select2("val", "CA");
// get value
$("#select").select2("val"); |
Hi, thanks for you attention. I try this, but not work. In my script I create the widget like this:
SCRIPT;
But with this what are the ID of select2 field? I try set the ID but nothing work. Can you help me with this? |
Finally I found a solution: $("#agenda-prospect_id").select2("data", {id: results.prospect_id, text: results.prospect_nome}); I have to use the "data" not the "val" method... Thanks. |
How to set default value for the Select2 Widget when plugin option "multiple" => true... |
hello there. |
hi selected option in select2 for ajax not working in edit form it shows blank
|
solved i just initialized the value in view
}); |
if($id!=null): |
@jatin7591: this worked for me <?php
$selection = explode(",", $string_from_database);
$js = "";
foreach ($selection as $val) {
$js .= "$('#w0 option[value=". $val ."]').attr('selected','selected').change();";
}
$this->registerJs($js, yii\web\View::POS_READY);
?> In this case $string_from_database must be the values from the options comma-separated in one string. |
Hiya. I am having an issue setting the selected value within the Select 2 widget
As you can see I have tried the $_GET property and now I have made the value static. The id 50 isn't set as selected, I have tried removing the placeholder and it then just displays the first option. Any ideas Regards Liam |
Hi, thanks for this awesome widget. But I still can't selected default value. it happening when I try to update data:
|
Hi Guys. I have Solved this issue. Please try. nasmy_ayyash@yahoo.com
|
Hi, I would like to know how can I do to display the information of the dropbox but being able to write the value. Just like if the dropdown would be examples of what could be in the text field. Like a history of what the user already wrote there, but he can write what he considers best for the occasion. |
Can anyone please help me.. how can i select a default value on page load in select2 filed used in yii2 dynamic form widget ? |
srikanth15794 Something like this:
You just need to establish a value. |
Hello, I am looking for something that can be done using CJuiAutoComplete extension but here I want to use Eselect2, because I want a dropdown with a search option. But when selecting a value from dropdown, I want to populate 4 other fields. Is there any option in select2? By the way I'm using Yii1 |
I had been suffering from using Select2 with a form + model. The issue at the end is very simple. You can not use an attribute in your model that is assigned to a DB field. You just create another attribute, add it to your rules as safe, then use this field:
I will move setting booksetsArray to afterFind() method in the model, but it does the work. I hope it helps someone. |
Hi.
Is there an opportunity to specify the selected option for the select2 widget? It is very useful for editing field, that already have some value.
The text was updated successfully, but these errors were encountered: