Hi Everybody
This is my first time, so excuse my presentation.
I kinda need desperate help in reloading my gridview whenever any value is selected from the dropdown.
i have 2 tables containing
CITY
city_id(int), citycode(int), cityname(int)
Labels
labelid(int), langid(int), text(varchar)
the labels table contains the actual text of citynames with its respective translation
e.g.
Labelid | Langid | Text
100-----|—English—| Bombay
100-----|—Thai------| Bombay(thai)
101-----|—English—| Delhi
101-----|—Thai------| Delhi(thai)
the labelid in labels table is referenced in the ‘City’ table
City
Cityid | Citycode | Cityname
1 | BOM | 100
2 | DEL | 101
The admin page for CITY displays the ‘Citycode’ and ‘Cityname’
along with a dropdown for language (English, Thai).
Now when the Page loads it is suppose to display the Citycode with the cityname in text fetched from the ‘Labels’ table with the default language being english
now if the user selects
‘Thai’ from the dropdown then the text in cityname column must get translated to the Thai.
so it is kinda related to Ajax in the normal Php code.
I have tried all the possible links from different website but for last 3 weeks I haven’t been able to figure it out.
I have mentioned the code below.
City Model.
<?php
/**
* This is the model class for table "city".
*
* The followings are the available columns in table 'city':
* @property integer $cityid
* @property string $cityname
* @property string $citycode
* @property integer $seq_no
* @property integer $enable
*/
class City extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return City the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'city';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('citycode, seq_no, enable', 'required'),
array('seq_no, enable', 'numerical', 'integerOnly'=>true),
array('cityname', 'length', 'max'=>50),
array('citycode', 'length', 'max'=>3),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('citycode,cityname, seq_no, enable', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'relFlightsOrg'=>array(self::HAS_MANY, 'Flights', 'org'),
'relFlightsDest'=>array(self::HAS_MANY, 'Flights', 'dest'),
//'relLabelCity' =>array(self::BELONGS_TO, 'Labels', 'cityname'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
//'cityid' => 'Cityid',
'cityname' => 'Cityname',
'citycode' => 'Citycode',
//'seq_no' => 'Seq No',
//'enable' => 'Enable',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search($langid=1)
{
$Citylangid=$langid;
$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="lbl.langid='$Citylangid'";
$criteria->compare('citycode',$this->citycode,true);
$criteria->compare('cityname',$this->cityname,true);
return new CActiveDataProvider($this, array('criteria'=>$criteria,));
}
}
City Controller
<?php
class CityController extends Controller
{
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view','Poplang'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update'),
'users'=>array('@'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete','gridview'),
'users'=>array('admin'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
/**
* Update the Gridview depending on Dropdown selection
*/
public function actionPoplang()
{
$model=new City();
$model->unsetAttributes(); // clear any default values
if (isset($_POST['languageCity']))
{
$tag = $_POST['languageCity'];
}
else
{
$tag=1;
}
$query = "SELECT u.citycode, lbl.text
FROM city u JOIN labels lbl
ON u.cityname = lbl.labelid
WHERE lbl.langid=$tag";
$command = Yii::app()->db->createCommand($query);
$model = $command->query();
// if(isset($_GET['City']))
// $model->attributes=$_GET['City'] and Labels::model()->findAll('text');
//
$this->render('admin',array('model'=>$model,'tag'=>$tag));
}
/**
* Manages all models.
*/
public function actionAdmin()
{
$this->actionPoplang();
}
/**
* Performs the AJAX validation.
* @param CModel the model to be validated
*/
protected function performAjaxValidation($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='city-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
}
City ‘admin’ page
<?php
$this->breadcrumbs=array('Cities'=>array('index'),'Manage',);
$this->menu=array(
array('label'=>'List City', 'url'=>array('index')),
array('label'=>'Create City', 'url'=>array('create')),
);
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
$('.search-form').toggle();
return false;
});
$('.search-form form').submit(function(){
$.fn.yiiGridView.update('city-grid', {
data: $(this).serialize()
});
return false;
});
");
?>
<h1>Manage Cities</h1>
<p>
You may optionally enter a comparison operator (<b><</b>, <b><=</b>, <b>></b>, <b>>=</b>, <b><></b>
or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.
</p>
<?php 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 -->
<?
$Labelcriteria = new CDbCriteria;
$Labelcriteria->condition = ("enabled=1");
$langarray= Language::model()->findAll($Labelcriteria);
if(isset($tag))
{
$Clangid=$tag;
$lab=$tag;
}
else
{
$lab='language';
$Clangid=1;
}
?>
<?php echo CHtml::dropDownList('languageCity','',CHtml::listData($langarray, 'langid', 'langname'),
array(
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('City/Poplang'),
'update' =>'$model',
)));
?>
<?php
$this->widget('zii.widgets.grid.CGridView',
array('id'=>'city-grid','dataProvider'=>$model->search($Clangid),'filter'=>$model,'columns'=>array('citycode','cityname',array('class'=>'CButtonColumn',),),'enablePagination'=>true,
)
);
?>
Hope I was clear as I could be
would really Appreciate if anybody can help me in this.
Thanks and God Bless