Yii Framework Forum: Selected Option For Drop Down - Yii Framework Forum

Jump to content

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

Selected Option For Drop Down Rate Topic: -----

#1 User is offline   Aadhira 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 28
  • Joined: 06-February 13

Posted 27 April 2013 - 12:02 AM

hi all,

I am getting options for a dropdown from controller ,its working fine .I need those options to be seleced.I tried

echo CHtml::tag('option',array('value' => $id,'selected'=>'selected'),CHtml::encode($value),true);

But its not working ,can any one suggest me how to do this?????


This is my code in controller's action
            foreach($data as $id => $value)
            {
                echo CHtml::tag('option',array('value' => $id),CHtml::encode($value),true);
            }
            




Thanks in advance
:)
0

#2 User is offline   Artificial 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 64
  • Joined: 11-December 10
  • Location:Sydney, Australia

Posted 27 April 2013 - 12:08 AM

If you're trying to have a specific option selected, you need to make use of the selected html attribute. i.e:

foreach($data as $id => $value) {
    if (condition)
        echo CHtml::tag('option',array('value' => $id, 'selected' => 'selected'),CHtml::encode($value),true);
    else
        echo CHtml::tag('option',array('value' => $id),CHtml::encode($value),true);
}

1

#3 User is offline   Aadhira 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 28
  • Joined: 06-February 13

Posted 27 April 2013 - 12:12 AM

View PostArtificial, on 27 April 2013 - 12:08 AM, said:

If you're trying to have a specific option selected, you need to make use of the selected html attribute. i.e:

foreach($data as $id => $value) {
    if (condition)
        echo CHtml::tag('option',array('value' => $id, 'selected' => 'selected'),CHtml::encode($value),true);
    else
        echo CHtml::tag('option',array('value' => $id),CHtml::encode($value),true);
}





I need all the options to be selected,and i already tried your code too ...
1

#4 User is offline   Artificial 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 64
  • Joined: 11-December 10
  • Location:Sydney, Australia

Posted 27 April 2013 - 12:29 AM

Oh, so it's a multi select dropdown?

foreach($data as $id => $value) {
    echo CHtml::tag('option',array('value' => $id, 'selected' => 'selected'),CHtml::encode($value),true);
}

0

#5 User is offline   Aadhira 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 28
  • Joined: 06-February 13

Posted 27 April 2013 - 12:34 AM

View PostArtificial, on 27 April 2013 - 12:29 AM, said:

Oh, so it's a multi select dropdown?

foreach($data as $id => $value) {
    echo CHtml::tag('option',array('value' => $id, 'selected' => 'selected'),CHtml::encode($value),true);
}



yes ,and one more thing i have to say ,i used Tbselect2 extension for the drop down in javascipt,is the problem due to this????
0

#6 User is offline   Aadhira 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 28
  • Joined: 06-February 13

Posted 27 April 2013 - 12:40 AM

View PostAadhira, on 27 April 2013 - 12:34 AM, said:

yes ,and one more thing i have to say ,i used Tbselect2 extension for the drop down in javascipt,is the problem due to this????


this is my code in view
          <?php  echo         form->listBox($model,'hobbies',CHtml::listData(hobbies::model()->findAllByAttributes(array('ID'=>$model->cat_id)),'hob_ID','hob_name'),
           array( 'multiple'=>'multiple',
               ));
            ?>



and i used this in javascript



$('#hobbies').select2({
     height:'150px'
    });

0

#7 User is offline   Artificial 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 64
  • Joined: 11-December 10
  • Location:Sydney, Australia

Posted 27 April 2013 - 12:41 AM

See this: http://yiitest.dev.bezz.com.au/

That's just a simple select box with all items selected, with:

$data = array('john' => 'doe', 'alex' => 'bergman', 'josh' => 'smyth', 'sean' => 'norrish', 'adam' => 'kerr', 'sam' => 'gummer');
echo CHtml::openTag('select', array('multiple' => 'multiple', 'style' => 'width: 300px;'));

foreach($data as $id => $value) {
    echo CHtml::tag('option',array('value' => $id, 'selected' => 'selected'),CHtml::encode($value));
}

echo CHtml::closeTag('select');


And you can see it works. So if you're using select2 and it's not working, there's your problem. I'd suggest you read the select2 documentation to see if you can resolve the issue.
0

#8 User is offline   Aadhira 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 28
  • Joined: 06-February 13

Posted 27 April 2013 - 12:45 AM

View PostArtificial, on 27 April 2013 - 12:41 AM, said:

See this: http://yiitest.dev.bezz.com.au/

That's just a simple select box with all items selected, with:

$data = array('john' => 'doe', 'alex' => 'bergman', 'josh' => 'smyth', 'sean' => 'norrish', 'adam' => 'kerr', 'sam' => 'gummer');
echo CHtml::openTag('select', array('multiple' => 'multiple', 'style' => 'width: 300px;'));

foreach($data as $id => $value) {
    echo CHtml::tag('option',array('value' => $id, 'selected' => 'selected'),CHtml::encode($value));
}

echo CHtml::closeTag('select');


And you can see it works. So if you're using select2 and it's not working, there's your problem. I'd suggest you read the select2 documentation to see if you can resolve the issue.




Thanks a lot ,i will try to fix the issue
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