0

我的示例数据库;

我的示例数据库

模型/房地产.PHP

    public function relations()
{
    return array(
        'neighborhood' => array(self::BELONGS_TO, 'Neighborhood', 'fk_id_neighborhood'),

    );
}

我需要的是在注册房产时,拥有所有选项,看一个例子;查看/房地产/添加.PHP

在此处输入图像描述

我的问题与 ajax 无关,如何为每个 DropDownList 进行这种集成,我有一个特定的模型?像这样?

4

1 回答 1

2

You will need ajax anyway.

$form->dropDownList($model, $state, CHtml::listData(State::model()->findAll(),'id','state'), array(
        'empty'=>"Select state",    
        'ajax' => array(
                        'type' => 'POST',
                        'url'=>$this->createUrl('registration/state'),   
                        'update' => '#YOURcityID',                        
                'data'=>array('state'=>'js:this.value',),   
            'success'=> 'function(data) {$("#YourcityID").empty();
                            $("#YourcityID").append(data);

                                                } ',

        )));

In yourcontroller:

public function actionState()
{

 $data=Cities::model()->findAll('fk_id_state=:state', 
                  array(':state'=> $_POST['state']));

    $data=CHtml::listData($data,'city_id','city');
            foreach($data as $value=>$city)  {
                        echo CHtml::tag
                                ('option', array('value'=>$value),CHtml::encode($city),true);
                    }   

}

Back to form:

$form->dropDownlist($model, 'city', array(),array(
        'empty'=>"Select city",
'ajax' => array(
                            'type' => 'POST',
                            'url'=>$this->createUrl('registration/city'),   
                            'update' => '#YOURneighborhoodID',                        
                    'data'=>array('city'=>'js:this.value',),    
                'success'=> 'function(data) {                                       $("#YourneighborhoodID").empty();
                                            $("#YourneighborhoodID").append(data);

                                                    } ',
        ));

In controller:

public function actionCity()
{

 $data=Neighborhood::model()->findAll('fk_id_city=:city', 
                  array(':city'=> $_POST['city']));

    $data=CHtml::listData($data,'neghborhood_id','neighborhood');
            foreach($data as $value=>$neighborhood)  {
                        echo CHtml::tag
                                ('option', array('value'=>$value),CHtml::encode($neighborhood),true);
                    }   

}

This will work like a charm. Check for typos and your variables cause i was hurry. Regards.

于 2013-06-04T15:18:40.327 回答