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. Unity. Navigation和寻路

    Navigation Static:不会移动.可以用于计算可行走区域.例如:地板.墙.静态障碍物. 将一个物体选为Navigation Static:Navigation窗口-> 勾选项

  2. LU分解和求解线性方程组

    # coding:utf8 import numpy as np def lu(mat): r,c=np.shape(mat) s=min(r,c) for k in range(s): x=1.0/ ...

  3. JNI学习2:android 调用C语言方法与C语言调用android方法

    #include <jni.h> #include <stdio.h> #include <stdlib.h> #include <jni.h> #in ...

  4. jQuery return false

    在jQuery代码中,我们常见用return false来阻止浏览器的默认行为.例如点击链接,浏览器默认打开一个新窗口/标签,为了阻止浏览器的默认行为,我们往往这样操作: $("a.togg ...

  5. Iaas-cloudstack2

    流程是下面的模板安装脚本,将对应的hypervisor模板下载下来并另存为临时文件,并解压缩,并将其移动到二级存储相应目录下,仅完成此工作. [root@manage nfs]# /usr/share ...

  6. 可选的Web Components类库

    首先需要说明的是这不是一篇 Web Components 的科普文章,如果对此了解不多推荐先读<A Guide to Web Components>. 有句古话-“授人以鱼,不如授人以渔” ...

  7. VB的if和elseif

    VB中if和elseif的用法是: if...then...elseif...then...else...endif 切记在then的后面不要加冒号,加了冒号出现else没有if的错误,因为加了冒号表 ...

  8. 反向代理代理百度、google

    <VirtualHost _default_:443> # ServerAdmin mail@localhost # DocumentRoot "/var/www/html&qu ...

  9. JdbcUtils

    JdbcUtils 项目结构   db.properties driverClass=com.mysql.jdbc.Driver url=jdbc:mysql:///myTest username=r ...

  10. SharePoint 2010 最佳实践学习总结------第1章 SharePoint Foundation开发基础

    ----前言 这段时间项目出在验收阶段,不是很忙,就潜心把SharePoint学一下,不求有多深刻,初衷只是先入门再说.后续会发布一系列的学习总结.主要学习的书籍为<SharePoint2010 ...