Yii Framework Forum: Need an example of dropDownList() and listData() working together - Yii Framework Forum

Jump to content

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

Need an example of dropDownList() and listData() working together Rate Topic: ***** 2 Votes

#1 User is offline   nettrinity 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 208
  • Joined: 15-March 11

Posted 20 October 2011 - 04:20 PM

Hi,
I've been working for the past 2 hours to try to generate a simple drop down list in my view.

I am a beginner and not capable to get things working based on the document here. I am pretty lost about the data type and coudln't figure out why we need listData() to generate data for dropDownList(). Learning how to use dropDownList() is hard enough for me.
Please help me provide an example, and assume I know nothing.

Thank you!

- Nick
0

#2 User is offline   nettrinity 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 208
  • Joined: 15-March 11

Posted 20 October 2011 - 04:23 PM

this is my code in view
<?php echo $searchForm->dropDownList($searchFormModel,$category, 'option1'); ?>


and having the following error

Quote

Property "CompanySearchForm." is not defined.



0

#3 User is offline   Aruna Attanayake 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 119
  • Joined: 08-September 11
  • Location:Colombo - Sri Lanka

Posted 20 October 2011 - 07:28 PM

// Normal dropdownlist
<?php echo $form->dropDownList($model, 'field_name', array(1=>'test1', 2=>'test2'));?>


// List data need when populating dropdown from database values
<?php echo $form->dropDownList($model,'node_types_id', CHtml::listData(NodeTypes::model()->findAll(array('order' => 'name')),'id','name'));?>

Extensions:
SFTP

PageSize

Dashboard
3

#4 User is offline   Luke Jurgs 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 162
  • Joined: 16-October 11
  • Location:Australia

Posted 20 October 2011 - 07:36 PM

Hi,

This page goes into some detail into how listData works:

http://www.yiiframew...-example-chtml/

I assume based on the syntax you are using that you are using CActiveForm, the second parameter needs to be a valid attribute on your Model, so this is why you are getting an error. The third parameter needs to be an array.

Example:

$model = new MyModel();
$model->category_id = 1;

$categories = array(1 => 'Parts', 2 => 'Services');

<?php echo $searchForm->dropDownList($model, 'category_id', $categories); ?>



Or if you want categories from some model:

$model = new MyModel();
$model->category_id = 1;

<?php echo $searchForm->dropDownList($model, 'category_id', CHtml::listData(Category::model()->findAll(), 'id', 'name')); ?>



So in the second case, listData takes the attributes named 'id' and 'name' on Category to use in the array to return.


Hope this helps you a bit.
2

#5 User is offline   nettrinity 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 208
  • Joined: 15-March 11

Posted 21 October 2011 - 08:27 AM

Thank you very much!
0

#6 User is offline   satyanarayana 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 33
  • Joined: 30-January 14
  • Location:Hyderabad

Posted 17 March 2014 - 03:46 AM

View PostAruna Attanayake, on 20 October 2011 - 07:28 PM, said:

// Normal dropdownlist
<?php echo $form->dropDownList($model, 'field_name', array(1=>'test1', 2=>'test2'));?>


// List data need when populating dropdown from database values
<?php echo $form->dropDownList($model,'node_types_id', CHtml::listData(NodeTypes::model()->findAll(array('order' => 'name')),'id','name'));?>




I got this error,

Undefined variable: form
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