2016-03-31 09:46:23 23848次浏览 3条回答 6 悬赏 1000 金钱

表结构

$this->createTable('{{%area}}', [
    'id' => $this->primaryKey(6),
    'name' => $this->string(45),
    'pid' => $this->integer(6),
    'code' => $this->string(5),
    'level' => $this->integer(1),
    'typename' => $this->string(4),
], $tableOptions);

需要在表单页面中地址能显示到默认的位置

已经解决了,有空把解决就把解决方案以视频形式发出来

解决方式:

1.在模型中定义省市区三个字段,并在afterFind方法中给新增字段赋值

class UserInformation extends \yii\db\ActiveRecord
{
    public $province_area_id = 0;
    public $city_area_id = 0;
    public $county_area_id = 0;

    public function afterFind()
    {
        parent::afterFind(); // TODO: Change the autogenerated stub
        $this->setProvinceCityCounty($this->area_id);
    }

    private function setProvinceCityCounty($area_id)
    {
        $Area = new Area();
        $area_info = $Area::findOne($area_id);
        if($area_info){
            switch($area_info->level){
                case 1:
                    $this->province_area_id = $area_info->id;
                    break;
                case 2:
                    $this->province_area_id = $area_info->pid;
                    $this->city_area_id = $area_info->id;
                    break;
                case 3:
                    $this->province_area_id = $Area::getParentId($area_info->pid);
                    $this->city_area_id = $area_info->pid;
                    $this->county_area_id = $area_info->id;
                    break;
            }
        }
    }
    ...
}

2.在Area模型中实现获取子列表方法

/**
 * This is the model class for table "{{%area}}".
 *
 * @property integer $id
 * @property string $name
 * @property integer $pid
 * @property string $code
 * @property integer $level
 * @property string $typename
 */
class Area extends \yii\db\ActiveRecord
{
    public $select_head = [
        ["id"=>0, "name"=>"请选择"],
        ["id"=>0, "name"=>"请选择省"],
        ["id"=>0, "name"=>"请选择市"],
        ["id"=>0, "name"=>"请选择区"],
    ];
    ...
    /**
     * 获取子列表
     * @param $pid
     * @return static[]
     */
    public static function getChildrenList($pid,$level=0)
    {
        $x[] = (new static)->select_head[$level];
        return array_merge($x,static::findAll(['pid'=>$pid]));
    }
}

3.用Html::activeDropDownList组件实现可编辑样式的下拉框,其中用onchange方法来实现ajax获取子列表,组件会根据值来设置默认选中的地区

<?= $form->field($UserInformation, 'area_id', [
                        'template'=>'{label}<div id="area_linkage"><div class="col-sm-3">'.
                            Html::activeDropDownList($UserInformation,'province_area_id', ArrayHelper::map($Area::getChildrenList(0,1), 'id', 'name'), [
                                'class' => 'form-control',
                                'onchange' => '
                                    $("#userinformation-area_id").val($(this).val());
                                    $.ajax({
                                        type:"post",
                                        url:"'.Yii::$app->urlManager->createUrl('/usercenter/area/ajax-post-children-area').'",
                                        data:{pid:$(this).val(),level:2},
                                        success:function(msg){
                                            $("#userinformation-city_area_id").html(msg);
                                            $("#userinformation-county_area_id").html(\'<option value="0">请选择区</option>\');
                                        }
                                    });
                                ',
                            ])
                            .'</div><div class="col-sm-3">'.
                            Html::activeDropDownList($UserInformation,'city_area_id', ArrayHelper::map($Area::getChildrenList($UserInformation->province_area_id,2), 'id', 'name'), [
                                'class' => 'form-control',
                                'onchange' => '
                                    $("#userinformation-area_id").val($(this).val());
                                    $.ajax({
                                        type:"post",
                                        url:"'.Yii::$app->urlManager->createUrl('/usercenter/area/ajax-post-children-area').'",
                                        data:{pid:$(this).val(),level:3},
                                        success:function(msg){
                                            $("#userinformation-county_area_id").html(msg);
                                        }
                                    });
                                ',
                            ])
                            .'</div><div class="col-sm-3">'.
                            Html::activeDropDownList($UserInformation,'county_area_id', ArrayHelper::map($Area::getChildrenList($UserInformation->city_area_id,3), 'id', 'name'), [
                                'class' => 'form-control',
                                'onchange' => '
                                    $("#userinformation-area_id").val($(this).val());
                                ',
                            ])
                            .'</div><div class="hidden" id="parent_div_area_id">{input}</div></div>',
])->hiddenInput(); ?>

/usercenter/area/ajax-post-children-area方法:

class AreaController extends UserCenterController
{
    public function actionAjaxPostChildrenArea()
    {
        if(\Yii::$app->request->isAjax){
            $pid = \Yii::$app->request->post('pid');
            $level = \Yii::$app->request->post('level');
            $area_children = Area::getChildrenList($pid,$level);
            $option = "";
            if(count($area_children)>0){
                foreach($area_children as $k => $v){
                    $option .= Html::tag('option',Html::encode($v['name']),['value'=>$v['id']]);
                }
            }
            echo $option;
        }
    }
}
  • 回答于 2016-03-31 10:23 举报

    http://www.yiichina.com/tutorial/476

    提问前不尝试自己解决下问题?

    觉得很赞
  • 回答于 2016-03-31 13:47 举报

    跟yii2有什么关系啊,都是一些js效果而已

  • 回答于 2016-03-31 14:14 举报

    2楼说的对,跟yii没有多大的关系,js就可以实现,根据你现有的表结构,查询时,先查询顶级的数据,当点击某一个级别,你根据这个级别的id,去查询他的子类别就好,ajax请求就能实现,如果数据不多,可以一次性加载出来也行

您需要登录后才可以回答。登录 | 立即注册
wodrow
副董事长

wodrow China

注册时间:2015-04-09
最后登录:4小时前
在线时长:199小时16分
  • 粉丝34
  • 金钱42695
  • 威望120
  • 积分45885

热门问题