Yii2.0 三级联动求助 [ 2.0 版本 ]
按照,这个帖子http://www.yiichina.com/tutorial/468 三级联动,但是只有第一级有数据,后面两个都是没数据,请大家帮我看一下。
我是这样做的:
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();?>
最后运行的时候有问题,只能调用一级联动,后面的都是空的取不到数据,请大神看一下,多谢!
其他 4 个回答
leoburn
注册时间:2015-07-22
最后登录:2022-05-19
在线时长:14小时36分
最后登录:2022-05-19
在线时长:14小时36分
- 粉丝2
- 金钱115
- 威望0
- 积分255