Yii Booster And Tbselect2 Within Tbactiveform

Hello out there,

I am on my first steps with YiiBoilerplate and this is just sooo amazing.

The YiiBooster gives so many quick wins, fantastic. :lol:

I am right now trying to have a TbSelect2 inside a TbActiveForm.

The TbSelect2 fetches data via a AJAX/JSON from the controller.

That works fine.

I can also selct something, but how can I put the selcted ID (not the text of course) into the appropiate form field?

Any ideas?

This is my code so far:

(the out-commented echo is the default CRUD created form field for this controller. So I want to use the selected ID from the TBSelect2 as the "genus_id" of the model.)


//echo $form->textFieldRow($model,'genus_id',array('class'=>'span5')); 

            

$this->widget('bootstrap.widgets.TbSelect2', array(

            'asDropDownList' => false,

            'name' => 'genus_id',

            'model'=>$model, 

            'options'=>array(

                'width' => '40%',

                'minimumInputLength' => 2,

                'placeholder'=>'Choose Genus',

                'allowClear'=>true,

                'ajax'=>array(

                    'url'=>Yii::app()->createUrl('CfNames/list'),

                    'dataType'=>'jsonp',

                    'data' => 'js: function(term,page) {

                        return {

                            q: term, 

                        };

                        

                    }',

                    'results' => 'js: 

                        function(data,page){

                        return {results: data.list

                        };

                        

                    }',

                

                ),

                

                

           ),

));	

Thanks for any hints to put me into the right direction… ;D

gb

Soo, after one long night, I found the solution.

Actually it is quite simple.

within the options-array the name of the select2 needs to be of the format:

‘name’ => ‘MODEL[ATTRIBUTE]’

Then it works and also stores the id while displaying the text.

Very cool.

BUT: after further dev with the select2 I now face the problem, that this works very well on CREATE, but not on UPDATE:

On UPDATE I would then need to initialise the select2 which needs to retrieve the text.

Select2 comes with a handy ‘initSelection’ part, where you can call another ajax to retrive the text for the id. But this is only called, if there is a value given to the element by load. And exactly this is not the case. I do see the value on the VIEW, but when I click on the UPDATE, I can see in FireBug that the hidden field does not contain any value. All other non-select2 fields work fine.

Any ideas?

(Yeah, I hear it already: Hey you found out the last question yourself: Repeat it!)

thanks for any hints,

gb

HI

Could you share the code of this action ‘url’=>Yii::app()->createUrl(‘CfNames/list’)’?

I have some problems with Select2 and I hope you can help me.

thanks