2015-10-14 10:29:01 4158次浏览 5条回答 2 悬赏 10 金钱

按照,这个帖子http://www.yiichina.com/tutorial/468 三级联动,但是只有第一级有数据,后面两个都是没数据,请大家帮我看一下。
1.jpg

我是这样做的:
1.建立了数据表

/*
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;

2.我有一个用到省市联动的表tms_course,我利用gii生成Model生成了2个 一个是Course,一个是城市City,下面是Course Model,在里面加入了getCityList($pid)

<?php

namespace app\models;

use Yii;
use \yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%course}}".
 *
 * @property integer $id
 * @property string $college
 * @property string $profession
 * @property string $squad
 * @property string $teacher
 * @property string $subject
 */
class Course extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%course}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['college', 'profession', 'squad', 'teacher', 'subject'], 'required'],
            [['college', 'profession', 'teacher', 'subject'], 'string', 'max' => 50],
            [['squad'], 'string', 'max' => 6]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'college' => 'College',
            'profession' => 'Profession',
            'squad' => 'Squad',
            'teacher' => 'Teacher',
            'subject' => 'Subject',
        ];
    }

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

3.我将如下代码加入了控制器action

    /**
     * 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));
        }
    }

4.我新建了一个控制器用于调用联动

    public function actionCourse(){
        $model = new Course();
        return $this->render('course',['model' => $model]);
    }

5.我建立了action Course的View,以下是Vies course.php的代码

<?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('site/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('site/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();?>

最后运行的时候有问题,只能调用一级联动,后面的都是空的取不到数据,请大神看一下,多谢!

最佳答案

  • 发布于 2015-10-14 18:24 举报

    只能一级一级测试,谁也不知道你代码怎么写的,不可能凭这个代码就能看出错误来,最重要的还是自己去调试

  • 回答于 2015-10-14 16:36 举报

    代码太长...
    你分两头测试,看选了一级后,处理二级的函数能否正确获取一级信息。
    再手动指定一个一级数据,看二级数据能否正常显示。

  • 回答于 2016-03-09 14:34 举报

    只有这一张表吗,其他表结构是什么样的啊

  • 回答于 2016-08-09 19:22 举报

    我也在学这个

    请问楼主

    $form->field($model,'province')

    这个 province,从哪里来的?

  • 回答于 2017-11-24 11:12 举报

    请问楼主这个问题解决了吗?
    我也出不来。

您需要登录后才可以回答。登录 | 立即注册
leoburn
见习主管

leoburn

注册时间:2015-07-22
最后登录:2022-05-19
在线时长:14小时36分
  • 粉丝2
  • 金钱115
  • 威望0
  • 积分255

热门问题