Yii Framework Forum: Yii Booster And Tbselect2 Within Tbactiveform - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Yii Booster And Tbselect2 Within Tbactiveform How to get the data selected into the Form? Rate Topic: -----

#1 User is offline   gb5256 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 40
  • Joined: 22-April 11

Posted 28 March 2013 - 11:11 AM

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
0

#2 User is offline   gb5256 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 40
  • Joined: 22-April 11

Posted 29 March 2013 - 07:22 AM

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
1

#3 User is offline   ramasurf 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 06-February 12

Posted 31 March 2013 - 03:07 AM

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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users