这是下拉列表:
$cities = array('' => '-- All --');
$cities += CHtml::listData(Cities::model()->findAll(), 'id', 'name');
echo $form->dropDownList($model, 'city', $cities, array('class' => 'edit', 'style' => 'width:auto', 'onchange'=>'$(document).ready(function(){
$("#pay").trigger("click");});' ) );
我想默认选择--All--选项。我怎么能这样做?
更新:我试过这样:
echo $form->dropDownList($model, 'city', $cities, array('options' => array(''=>array('selected'=>true))), array('class' => 'edit', 'style' => 'width:auto', 'onchange'=>'$(document).ready(function(){
$("#pay").trigger("click");});'));
但没有工作。 这是生成的HTML:
<select name="FitnessSubscriber[city]" id="FitnessSubscriber_city">
<option value="" selected="selected">-- All --</option>
<option value="1" selected="selected">Cluj-Napoca</option>
<option value="2">Baia Mare</option>
<option value="3">Iași</option>
<option value="5">Timisoara</option>
<option value="6">Bucuresti</option>
</select>
更新型号: 这是城市模型:
class Cities extends ActiveRecord
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 'cities';
}
public function rules()
{
return array(
array('name', 'length', 'max'=>45),
array('id, name', 'safe', 'on'=>'search'),
);
}
public function relations()
{
return array(
'city' => array(self::BELONGS_TO, 'UserLocations', 'city_id'),
'locations' => array(self::HAS_MANY, 'Locations', 'city_id'),
'users' => array(self::HAS_MANY, 'Users', 'location_id'),
'gyms'=>array(self::HAS_MANY, 'FitnessGym', 'city_id'),
'pools'=>array(self::HAS_MANY, 'SwimPool', 'city_id'),
);
}
public function attributeLabels()
{
return array(
'id' => 'ID',
'name' => 'Name',
);
}
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
答案 0 :(得分:0)
尝试在"prompt"=> "---All---"
内使用$htmlOptions
:
echo $form->dropDownList($model, 'city', $cities, array('class' => 'edit', 'style' => 'width:auto', 'onchange'=>'$(document).ready(function(){
$("#pay").trigger("click");});', "prompt"=> "--All--" ) );
如果您之前添加过,请从数据列表中删除“--All--”。因为它不是真正的数据!
答案 1 :(得分:0)
这样做..
echo $form->field($model, 'city')->dropDownList(
ArrayHelper::map(Cities::find()->all(),'city_id', 'city_name'),
['prompt' => '--Select City--'],
['active' => $model->city], //Here you add the city you want to set selected
);