Reputation: 45
I have 3 tables in mysql
city in which I have Cityid Cityname
labels in which I have labelid langid and text
language table in which I have langid, langname and enabled
Now the 3 tables are interrelated as cityname=labelid text will hold the actual name language will hold the language name eg
City Table will be `
cityid=1, cityname=3000
cityid=2, cityname=3001
`
Labels Table will be
labelid =3000, langid=1, text=New York
labelid =3000, langid=23, text=New York in Chinese
labelid= 3001, langid=1, text= Mumbai
Language Table will be
`langid=1, lagname=english, enabled=1
langid=23, langname=chinese, enabled=1`
Now what I have achieved is to display the data of city in grid view and show a dropdown of all the languages whose enabled=1
What I want to do is change the content of the grid according to the language we have selected from the drop down.
So when Chinese is selected in the drop down then all the city names should appear in Chinese.
my view code is
$Labelcriteria = new CDbCriteria;
$Labelcriteria->condition = ("enabled=1");
$langarray= Language::model()->findAll($Labelcriteria);
$i=-1;
foreach ($langarray as $lang)
{
$i=$i+1;
$langName[$i]=$lang->langname;
}
//echo CHtml::dropDownList('select_box_name','select_value',$langName,array('onchange' => 'alert(1)',));
echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">
<?php
$this->renderPartial('_search',array('model'=>$model,));
?>
</div><!-- search-form -->
<?php
echo CHtml::dropDownList('select_box_name','select_value',$langName, array(
'onchange'=>'alert(1)',
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('cityController/dynamiccities'),
)
));
$this->widget('zii.widgets.grid.CGridView',
array('id'=>'city-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array('citycode', 'cityname',array('class'=>'CButtonColumn',),),));
?>
While my model is
public function search()
{
$criteria=new CDbCriteria;
$criteria->select = 't.citycode,lbl.text as cityname ';
$criteria->join = 'inner join labels lbl on t.cityname=lbl.labelid inner join language lng on lbl.langid=lng.langid';
$criteria->order = 'lbl.text ASC';
$criteria->condition=$someway_to_change_dynamically_using_dropdown;
$criteria->compare('citycode',$this->citycode,true);
$criteria->compare('cityname',$this->cityname,true);
// $criteria->compare('cityid',$this->cityid);
// $criteria->compare('seq_no',$this->seq_no);
// $criteria->compare('enable',$this->enable);
return new CActiveDataProvider($this, array('criteria'=>$criteria,));
}
Any help will be really appreciated
This is the controller action which renders the view file
public function Admin()
{
$model=new City();
$model->unsetAttributes(); // clear any default values
if(isset($_GET['City']))
$model->attributes=$_GET['City'];
$this->render('admin',array('model'=>$model));
}
Upvotes: 3
Views: 6275
Reputation: 9979
You can use this for updating the content of a GridView
with respect to the selection on a CHtml::dropDownList
.
1)The js script is for capturing the onchange of the select:
Yii::app()->clientScript->registerScript('sel_status', "
$('#selStatus').change(function() {
//alert(this.value);
$.fn.yiiGridView.update('milestone-category-grid', {
data: $(this).serialize()
});
return false;
});
");
2)Code for the select:
$data = CHtml::listData(Status::model()->findAll('IsProcess=?',array(1)), 'ID', 'Description');
$select = key($data);
echo CHtml::dropDownList(
'dropDownStatus',
$select, // selected item from the $data
$data,
array(
'style'=>'margin-bottom:10px;',
'id'=>'selStatus',
)
);
3)For the gridview widget:
$this->widget('bootstrap.widgets.TbGridView',array(
'id'=>'milestone-category-grid',
'afterAjaxUpdate' => 'installSortable',
'enableSorting' => false,
'dataProvider'=>$model->search($select),
'rowCssClassExpression'=>'"items[]_{$data->ID}"',
'columns'=>array(
'Description',
),
)); ?>
4)In the Search
function of your corresponding model the following code needs to capture the GET variable that is being passed, in this case it is dropDownStatus
, the name given to the select (use Firebug, if necessary, to get the name of the variable being passed):
public function search($status=false)
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
if ($status!==false) {
$criteria->condition='StatusID=:StatusID';
$criteria->params=array('StatusID'=>$status);
}
if (isset($_GET['dropDownStatus'])) {
$criteria->condition='StatusID=:StatusID';
$criteria->params=array('StatusID'=>$_GET['dropDownStatus']);
$criteria->order='Position ASC';
}
...
Upvotes: 0
Reputation: 3242
OK I've tried to wrap my head around it and I think I have it.
You want to show all the city names in English by default, but have a drop down with all the available languages in. When the language is switched, show the city names in that language.
So what you need is to have the grid view filled with all the name pulled out using a parameter, specified by the drop down, but defaulting to English. Your most of the way there.
Your countries are pulled out here:
....dget('zii.widgets.grid.CGridView',
array('id'=>'city-grid',
'dataProvider'=>$model->search(),
....
I'm guessing you want to just show the code and the translated name within the grid view?
So the widget will become:
$this->widget('zii.widgets.grid.CGridView',
array('id'=>'city-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'citycode',
array(
'name'=>'Country',
'value'=>'$data->getLabel("'.$langId.'")',
),
array('class'=>'CButtonColumn')
),
));
On your city model you then need a method to get its langauge variant out based on the ID of the langauge, so place this into your model:
public function getLabel($id){
return $this->labels[$id]->text;
}
You'll also need the proper realationship for the labels for each cityname too. Because your below Yii 1.1.9 and your not pulling them down together (joining) we'll need a manual function to join a FK to a FK.
Add this to your city Model:
public function getLabels(){
return Labels::model()->findAll(array(
'condition'=>'labelid = :labelid',
'params'=>array(':labelid'=>$this->cityname)
));
}
and change its search function to:
public function search()
{
$criteria=new CDbCriteria;
$criteria->order = 'lbl.text ASC';
$criteria->compare('citycode',$this->citycode,true);
$criteria->compare('cityname',$this->cityname,true);
$criteria->compare('cityid',$this->cityid);
$criteria->compare('seq_no',$this->seq_no);
$criteria->compare('enable',$this->enable);
return new CActiveDataProvider($this, array('criteria'=>$criteria,));
}
Now you'll need to modify the action send the right variables into the view:
public function Admin($lang=1)
{
$model=new City();
//populate the filters
$model->attributes = $_GET['City'];
$this->render('admin',array('model'=>$model, 'langId'=>$lang));
}
Then the search form needs to show all the available countries, which you've got already pulled out here:
$Labelcriteria = new CDbCriteria;
$Labelcriteria->condition = ("enabled=1");
$langarray= Language::model()->findAll($Labelcriteria);
$i=-1;
foreach ($langarray as $lang)
{
$i=$i+1;
$langName[$i]=$lang->langname;
}
This could be replaced without any change to the results with:
$langarray = Language::model()->findAll(array('condition'=>'enabled=1','select'=>'langname'));
....I think
Either way, you have the countries there and are already popluating a drop down with them.
So all that should give you a view with the drop down of langauges and a grid view with the city names populated with English names by default, but if you pass the lang parameter with an ID it will show cities with that language.
How you implement the Javascript to actually update the grid view on Ajax will on the rest of your page layout, framework and splitting out some more views. But we can get into that another time.
Upvotes: 2