view中代码

<?php
use yii\bootstrap\ActiveForm;

/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
?>

<?php $form = ActiveForm::begin(['enableClientValidation' => false]);?>

<?= $form->field($model,'province')->dropDownList($model->getCityList(0),
    [
        'prompt'=>'--请选择省--',
        'onchange'=>'
            $(".form-group.field-member-area").hide();
            $.post("'.yii::$app->urlManager->createUrl('member/site').'?typeid=1&pid="+$(this).val(),function(data){
                $("select#member-city").html(data);
            });',
    ]) ?>

<?= $form->field($model, 'city')->dropDownList($model->getCityList($model->province),
    [
        'prompt'=>'--请选择市--',
        'onchange'=>'
            $(".form-group.field-member-area").show();
            $.post("'.yii::$app->urlManager->createUrl('member/site').'?typeid=2&pid="+$(this).val(),function(data){
                $("select#member-area").html(data);
            });',
    ]) ?>
<?= $form->field($model, 'area')->dropDownList($model->getCityList($model->city),['prompt'=>'--请选择区--',]) ?>
<?php ActiveForm::end();?>

model中添加下面的函数

    /**
     * @param $pid
     * @return array
     */
    public function getCityList($pid)
    {
        $model = City::findAll(array('pid'=>$pid));
        return ArrayHelper::map($model, 'id', 'name');
    }

控制器中添加下面的代码:

    /**
     * Function output the site that you selected.
     * @param int $pid
     * @param int $typeid
     */
    public function actionSite($pid, $typeid = 0)
    {
        $model = new Member();
        $model = $model->getCityList($pid);

        if($typeid == 1){$aa="--请选择市--";}else if($typeid == 2 && $model){$aa="--请选择区--";}

        echo Html::tag('option',$aa, ['value'=>'empty']) ;

        foreach($model as $value=>$name)
        {
            echo Html::tag('option',Html::encode($name),array('value'=>$value));
        }
    }

City数据库结构:

/*
Navicat MySQL Data Transfer

Source Server         : MySQL
Source Server Version : 50624
Source Host           : localhost:3306
Source Database       : yii2advanced

Target Server Type    : MYSQL
Target Server Version : 50624
File Encoding         : 65001

Date: 2015-07-13 14:27:12
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for yii2_city
-- ----------------------------
DROP TABLE IF EXISTS `yii2_city`;
CREATE TABLE `yii2_city` (
  `id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
  `pid` smallint(5) unsigned NOT NULL DEFAULT '0',
  `name` varchar(120) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  KEY `parent_id` (`pid`)
) ENGINE=MyISAM AUTO_INCREMENT=3410 DEFAULT CHARSET=utf8;

Yii2.0 实现三级联动 [ 2.0 版本 ]的更多相关文章

  1. [moka同学转载]Yii2 中国省市区三级联动

    1.获取源码:https://github.com/chenkby/yii2-region 2.安装 添加到你的composer.json文件 "chenkby/yii2-region&qu ...

  2. Yii2 中国省市区三级联动

    1.获取源码:https://github.com/chenkby/yii2-region 2.安装 添加到你的composer.json文件 "chenkby/yii2-region&qu ...

  3. Yii2中省市三级联动(栏目联动)

    先从表的设计开始: 表的结构: CREATE TABLE `global_region` ( `region_id` smallint(5) unsigned NOT NULL AUTO_INCREM ...

  4. 简单的纯js三级联动

    参考这个  日尼禾尔  二级联动 写了三级联动 <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...

  5. 所使用的“EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”版本高于所引用的程序集“EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”的版本

    错误信息:所使用的"EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&qu ...

  6. Mysql下Limit注入方法(此方法仅适用于5.0.0<mysql<5.6.6的版本)

    SQL语句类似下面这样:(此方法仅适用于5.0.0<mysql<5.6.6的版本) SELECT field FROM table WHERE id > 0 ORDER BY id ...

  7. 使用pip命令报You are using pip version 9.0.3, however version 18.0 is available pip版本过期.解决方案

    使用pip命令安装或卸载第三方库时报You are using pip version 9.0.3, however version 18.0 is available.错误,一般情况下是pip版本过 ...

  8. 报错:Unsupported major.minor version 52.0 (jar包对不同JDK版本的兼容性问题:)

    Unsupported major.minor version 52.0 这类错误是因为Java版本不一致造成的,在高版本的JDK(1.8)环境中编译JAR包,然后JAR在低版本的JVM(1.6)中运 ...

  9. 安装 tensorflow 1.1.0;以及安装其他相似版本tensorflow遇到的问题;tensorflow 1.13.2 cuda-10环境变量配置问题;Tensorflow 指定训练时如何指定使用的GPU;

    # 安装 2.7 环境conda create -n python2. python= conda activate python2. # 安装 1.1.0 gpu版本pip # 配置环境变量expo ...

随机推荐

  1. @Resource注解

    原文地址:http://blog.sina.com.cn/s/blog_a795a96f01016if1.html   @Resource 注解被用来激活一个命名资源(named resource)的 ...

  2. 【转】关于PHP的header("P3P: CP=CURa……")

    对于IE来说(默认安全级别下),iframe.img.link等标签都是只发送session cookie(又叫 第一方cookie),拦截本地cookie发送(又叫第三方cookie).当这些标签跨 ...

  3. 【转】Android Drawable Resource学习(十一)、RotateDrawable

    对另一个drawable资源,基于当前的level,进行旋转的drawable. 文件位置: res/drawable/filename.xml文件名即资源名 编译数据类型: 指向 RotateDra ...

  4. 关闭Outlook时最小化

    公司用的是阿里邮箱,foxmail客户端总是把已读邮件又标记成未读,又赶上最近招聘,真是深受其烦.已经无法忍受了,决定换了它,重新用起最强客户端outlook. Outlook解决了foxmail的问 ...

  5. python函数: 内置函数

    forthttp://blog.csdn.net/pipisorry/article/details/44755423 Python内置函数 Python内置(built-in)函数随着python解 ...

  6. js 监听整个页面的回车事件

    JS监听整个页面的回车事件 <script type="text/javascript"> document.onkeydown=keyDownSearch;      ...

  7. Google Play开发者账号注册与失败申诉攻略

    Google Play开发者账号注册与失败申诉攻略 这篇文章我在网上找了好久,是在Google play进行开发者账号注册方法,介绍的很详细.现在分享一下.[原文地址] 为了方便开发者们注册谷歌的官方 ...

  8. JABX简单介绍

    主要引至http://suo.iteye.com/blog/1233458 一.简介 1.概念是什么:(Java Architecture for XML Binding) 是一个业界的标准,是一项可 ...

  9. Android外派(安卓外派) — 长年提供安卓开发工程师外派业务(可签合同)

    北京动点飞扬长年提供安卓工程师外派业务. 平均技术情况如下: 1.2~3年以上Android平台开发经验2.熟练掌握java技术,熟悉面向对象编程设计3.熟悉Android应用开发框架及Activit ...

  10. PowerShell连接中国Azure

    由于China Azure与Global Azure属于不同的服务,适用于Global Azure的命令并不完全适用于China Azure.目前,Add-AzureAccount命令地址指向Glob ...