0

情况:

带有“国家”下拉菜单、“邮政编码”文本字段、“城市”文本字段的表单。

国家下拉菜单有两项:比利时和法国(以国家代码为有效值,分别为 BE 和 FR)

“PostalCode”字段是一个 CJuiAutoComplete 字段,根据从国家下拉列表中选择的值,它必须从“BeCity/getBelgianPostalCodes”或“FrCity/getFrenchPostalCodes”获取自动完成数据。

这两个动作,彼此独立,完美地工作,它们产生正确的自动完成数据。但是,我无法弄清楚如何根据国家下拉列表的选定值使其动态工作。我尝试将所选国家/地区置于会话状态,并从那里动态生成 sourceUrl。当我查看生成的 html 的来源时,根据所选国家/地区,生成的 sourceUrl 是正确的,但我仍然从加载的第一个国家/地区获得值。所以大概有一个缓存问题或什么?

这是代码:

形式:

<div class="row">
        <?php echo $form->labelEx($model,'Country'); ?>
        <?php echo $form->dropDownList($model,
                        'CountryCode',
                        CHtml::listData(Country::model()->findAllByAttributes(array('Visible'=>1)),'Code','Name'),
                        array('ajax'=>array(
                              'type'=>'POST',
                              'url'=>CController::createUrl('request/setRequestCountryCode'),
                              'data'=>'js:{CountryCode: $(this).val()}'))
                        ); ?>
        <?php echo $form->error($model,'CountryCode'); ?>
    </div>

        <div class="row">
        <?php echo $form->labelEx($model,'PostalCode'); ?>
        <?php 

                $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
                'name'=>'Request_PostalCode',
                'value'=>$model->PostalCode,
//                'source'=>$this->createUrl('BeCity/GetBelgianPostalCodes'),
                'sourceUrl'=>$this->createUrl(RequestController::actionGetPostalCodeAction()),
                'options'=>array(
                        'minLength'=>'1',
                        'showAnim'=>'fold',
                        'cache'=>'false',
                        'select'=> 'js:function(event, ui)
                            {
                                $("#Request_PostalCode").val(ui.item.value);
                                $("#Request_City").val(ui.item.city);
                                return false;
                            }'

                )
                ));

                ?>
        <?php echo $form->error($model,'PostalCode'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'City'); ?>
        <?php echo $form->textField($model,'City',array('size'=>50,'maxlength'=>250)); ?>
        <?php echo $form->error($model,'City'); ?>
    </div>

请求控制器:

    public static function actionSetRequestCountryCode()
        {
            $countryCode = $_POST['CountryCode'];
            Yii::app()->user->setState('RequestCountryCode',$countryCode);
        }

        public static function actionGetPostalCodeAction()
        {
            $countryCode = Yii::app()->user->getState('RequestCountryCode');

            if($countryCode == 'BE')
                return 'BeCity/GetBelgianPostalCodes';
            elseif($countryCode == 'FR')
                return 'FrCity/GetFrenchPostalCodes';
            else
                return '';
        }

BeCity 控制器:

public function actionGetBelgianPostalCodes()
        {                     
            $res =array();

            if (isset($_GET['term'])) {
                    // http://www.yiiframework.com/doc/guide/database.dao
                    $qtxt ="SELECT
                            DISTINCT
                            CONCAT(bc.PostalCode, ' - ', bc.NameNL) as label,
                            bc.PostalCode as value,
                            bc.NameNL as city
                            FROM be_city bc
                            WHERE bc.PostalCode LIKE :qterm
                            ORDER BY bc.PostalCode, bc.NameNL ASC";
                    $command =Yii::app()->db->createCommand($qtxt);
                    $command->bindValue(":qterm", $_GET['term'].'%', PDO::PARAM_STR);
                    $res = $command->queryAll();
            }

            echo CJSON::encode($res);
            Yii::app()->end();
        }

FrCity 控制器:

public function actionGetFrenchPostalCodes()
        {                     
            $res =array();

            if (isset($_GET['term'])) {
                    // http://www.yiiframework.com/doc/guide/database.dao
                    $qtxt ="SELECT
                            DISTINCT
                            CONCAT(fc.PostalCode, ' - ' , fc.NameFR) AS label,
                            fc.PostalCode as value,
                            fc.NameFR as city
                            FROM fr_city fc
                            WHERE fc.PostalCode LIKE :qterm
                            ORDER BY fc.PostalCode ASC, fc.NameFR ASC";
                    $command =Yii::app()->db->createCommand($qtxt);
                    $command->bindValue(":qterm", $_GET['term'].'%', PDO::PARAM_STR);
                    $res =$command->queryAll();
            }

            echo CJSON::encode($res);
            Yii::app()->end();
        }

这是使用会话变量的正确方法,还是有更好的方法?

那么缓存问题呢?

谢谢你的帮助

4

1 回答 1

1

好的,能够通过让一个动作(函数)获取国家参数并执行该动作中的所有逻辑来使其工作,而不是尝试重新路由到两个单独的动作:

形式:

    <div class="row">
        <?php echo $form->labelEx($model,'Country'); ?>
        <?php echo $form->dropDownList($model,
                        'CountryCode',
                        CHtml::listData(Country::model()->findAllByAttributes(array('Visible'=>1)),'Code','Name')

                        ); ?>
        <?php echo $form->error($model,'CountryCode'); ?>
    </div>

        <div class="row">
        <?php echo $form->labelEx($model,'PostalCode'); ?>
        <?php 

                $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
                'name'=>'Request_PostalCode',
                'value'=>$model->PostalCode,
                'source'=>'js: function(request, response) {
                                $.ajax({
                                    url: "'.$this->createUrl('Request/GetPostalCodes').'",
                                    dataType: "json",
                                    data: {
                                        term: request.term,
                                        countryCode: $("#Request_CountryCode").val()
                                    },
                                    success: function (data) {
                                            response(data);
                                    }
                                })
                             }',
                'options'=>array(
                        'minLength'=>'1',
                        'showAnim'=>'fold',
                        'select'=> 'js:function(event, ui)
                            {
                                $("#Request_PostalCode").val(ui.item.value);
                                $("#Request_City").val(ui.item.city);
                                return false;
                            }'

                )
                ));

                ?>
        <?php echo $form->error($model,'PostalCode'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'City'); ?>
        <?php echo $form->textField($model,'City',array('size'=>50,'maxlength'=>250)); ?>
        <?php echo $form->error($model,'City'); ?>
    </div>

请求控制器:

公共函数 actionGetPostalCodes() {

    $countryCode = $_GET['countryCode'];

    $res =array();

    if (isset($_GET['term'])) 
    {

        $qtxt = '';

        if($countryCode == 'BE')
            $qtxt ="SELECT
                    DISTINCT
                    CONCAT(bc.PostalCode, ' - ', bc.NameNL) as label,
                    bc.PostalCode as value,
                    bc.NameNL as city
                    FROM be_city bc
                    WHERE bc.PostalCode LIKE :qterm
                    ORDER BY bc.PostalCode, bc.NameNL ASC";

        elseif($countryCode == 'FR')
            $qtxt ="SELECT
                    DISTINCT
                    CONCAT(fc.PostalCode, ' - ' , fc.NameFR) AS label,
                    fc.PostalCode as value,
                    fc.NameFR as city
                    FROM fr_city fc
                    WHERE fc.PostalCode LIKE :qterm
                    ORDER BY fc.PostalCode ASC, fc.NameFR ASC";

        if($qtxt != '')
        {
            $command =Yii::app()->db->createCommand($qtxt);
            $command->bindValue(":qterm", $_GET['term'].'%', PDO::PARAM_STR);
            $res = $command->queryAll();
        }
    }

    echo CJSON::encode($res);
    Yii::app()->end();

}
于 2012-09-19T08:14:56.737 回答