How do I use dropDownList with listData()? Can someone provide an example, as I just cannot get this to work.
How do I use dropDownList with listData()? Can someone provide an example, as I just cannot get this to work.
Hi
Just like this
<?php
$fa=Groupe::model()->findAll();
//public static array listData(array $models, string $valueField, string $textField, string $groupField='')
$ld=CHtml::listData($fa,'grp_Groupe','grp_Groupe');
echo CHtml::activeDropDownList($model,'fic_grp_Groupe',$ld); ?>
I don’t use a model for my dropdown because it’s not querying a database, I just want to define a list of options somewhere that I can use for the dropdown.
Width listData(), you get an array when you read a database so if you don’t use database, make an array like this
array('value1'=>'item1', 'value2'=>'item2',...)
Hi Loryck,
Basically what I want to do is display the same drop down list on two seperate pages, however I only want to define the dropdown data in one place.
I thought the listData function is used for this purpose?
you could create a function that generates the drop down and then just call the function where you need the drop down placed. for example this generates my language drop down which uses an ajax call to update a list of articles
static public function generateAjaxLanguageDropDownFormData($form, $htmlOptions)
{
$opts = array(
'ajax'=>array(
'type'=>'POST',
'url'=>('?r=articles/updateFormLanguage'),
'update'=>'#article_form'
)
);
$opts = array_merge($htmlOptions, $opts);
$lang = array(0=>'English', 1=>'French', 2=>'German');
return CHtml::DropDownList(
'ArticlesForm[lang]',
$form->lang, $lang, $opts);
}
Note i currently use a static array to form the options of the drop down eventually I’ll replace it with a function call that generates this data. Also you don’t need the static public declarations unless you put it inside of a class which is where mine lives.
To use the function i then do
echo ArticlesForm::generateAjaxLanguageDropDownFormData($form, $options);
Hello…
I want my dropDownList to be grouped. Can anyone show me how to force listData() to group values? There is the last parameter - string $groupField - which I cant make work. I need it to join 2 tables = to use 2 models?
1st table = Regions
2nd table = Countries
Each region belongs to 1 country. I need my dropDownList to look cca like this:
Germany
Westfall
Bayern
USA
New York
California
etc…
I do it like this:
CHtml::listData( Regions::model()->findAll(),
'id_region', // values
'region_name', // captions
'id_country'), // groups
But this code shows me following dropDown, where numbers are IDs of countries. I wasnt able to joint region table with country table.
1
Westfall
Bayern
2
New York
California
So I tried to do this, but it didnt work:
CHtml::listData( array (Regions::model()->findAll(), (Countries::model()->findAll() )
'id_region', // values
'region_name', // captions
'country_name'), // groups
And this didnt work neither
$criteria = new CDbCriteria;
$criteria->join = 'JOIN countries on (countries.id_country = regions.id_country)';
$RegionsModel = Regions::model()->findAll($criteria);
CHtml::listData( array ($RegionsModel, Countries::model()->findAll() )
'id_region', // values
'region_name', // captions
'country_name'), // groups
I think Im close, but Im lost…
try
CHtml::listData( Regions::model()->with('countries')->findAll(),
'id_region', // values
'region_name', // captions
'country_name'), // groups
It doesn’t work to me. He’s still looking for the “country_name” in the Regions table. He doesn’t see info from the table of countries… Yes, I have relation in Regions table pointing to Countries table. I tried both ways: BELONGS_TO and HAS_ONE.
I’ll keep trying…
Then try this:
CHtml::listData( Regions::model()->findAll(),
'id_region', // values
'region_name', // captions
'countries.country_name'), // groups, here countries is the name of the relation in the relations method, adn county name must be a colum of the country table
Hope this help you!!!
Didn’t work too. I tried a lots of things before I wrote my question, but it’s damn tricky
In this case, he’s looking for column Regions.countries.country_name - but it doesn’t exist, of course
I think it’s not difficult. And once, I’ll figure it out
You have to build an array like this:
array(
'Germany' => array(
'1' => 'Bayern',
'2' => 'Niedersachsen'
),
'USA' => array(
'3' => 'Kalifornien'
)
);
so create a little helper for it
I miss the together()
I did tests on my site and works
<?php
$fa=Localidades::model()->with('provincia')->together()->findAll();
$ld=CHtml::listData($fa,'idLocalidad', // values
'descripcion', // description of the Localidad
'provincia.descripcion'); //provincia is the alias in the relation
echo CHtml::activeDropDownList($model,'idLocalidad',$ld); ?>
in the model of Localidades
public function relations()
{
return array(
'provincia' => array(self::BELONGS_TO, 'Provincias', 'pro_idProvincia','alias'=>'provincia'),
);
}
My Localidades are your Regions
My Provincias are your Countries
[edit]
in your case, you do not need the alias in the relation
country_name is unique
in my db descripcion is not unique
[edit2]
I forget my rule (KISS) ha ha
this is what you have to do
CHtml::listData( Regions::model()->with('countries')->together()->findAll(),
'id_region', // values
'region_name', // captions
'country_name'), // groups
Thanks for help, but still nothing. He (PC) is still looking for column “country_name” in Region table. I don’t understand. I tried everything.
I’ll create a View that will join my tables and dropDownList will (I hope) work with it correctly. He is probably not able to join tables and work with all collumns.
This MUST work. Because CHtml::listData() uses the CHtml::value() method.
From the class reference: Value() Method
Just for sure, can you show us your relations method of Regions model??
I my "Regions" model is following relation
class Regions extends CActiveRecord
{
public function relations()
{
return array(
'country' => array(self::BELONGS_TO,'Countries','id_region_country'
)
);
}
}
PS:
Column “id_region_country” is in Regions table and it’s FK from Countries table.
In "Countries" table is PK "id_country"
1 country has more regions. Each region has always 1 country.
Then you should use:
CHtml::listData( Regions::model()->findAll(),
'id_region', // values
'region_name', // captions
'country.country_name'), // groups, here countries is the name of the relation in the relations method, adn county name must be a colum of the country table
In the dot notation the first part must be the name of the relation (in your case country). The second the attribute (in your case country_name).
bring forgiveness for obvious things
but certainly is nonsense
change countries for country in the with
CHtml::listData( Regions::model()->with('country')->together()->findAll(),
'id_region', // values
'region_name', // captions
'country_name'), // groups
another thing you can try is if the relationship works
in the controller load a region
$region=Regions::model()->find();
in the view
echo Chtml::encode($region->country->country_name);
also check that the query is correct. looks runtime/application.log
before
Enabling trace in config/admin.php
....
// application components
'components'=>array(
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'CFileLogRoute',
'levels'=>'error, warning,trace,info',
),
),
),
........
I have already tried this. It didn’t work as well. It was looking for column: “Regions.country.country_name”, which returned error.
I created the view I mentioned above a it works.
Exactly what version of yii you are using?