running girl 2015-10-15 16:30:05 5687次浏览 1条评论 14 8 0

view中的代码

    <?= $form->field($model, 'wx_one')->dropdownList(ArrayHelper::map(WxCategory::find()->where(['parent_id'=>0])->asArray()->all(),'id','name'),
    		[
    			'prompt'=>'请选择一级',
    			'onchange'=>'wx_one(this)',
             ]);?>
    <?= $form->field($model, 'wx_two')->dropdownList(ArrayHelper::map(WxCategory::find()->where(['parent_id'=>$model->wx_one])->asArray()->all(),'id','name'),
    		[
    			'prompt'=>'请选择二级',
    			'onchange'=>'wx_two(this)',	        
      	    ]); ?>
    <?= $form->field($model, 'wx_three')->dropdownList(ArrayHelper::map(WxCategory::find()->where(['parent_id'=>$model->wx_two])->asArray()->all(),'id','name'),
    		[
    			'prompt'=>'请选择三级',	        
       	    ]);?>
<script type="text/javascript">

  function wx_one(e){
	$(".form-group.field-category-wx_three").hide();
	$.post("site?typeid=1&pid="+$(e).val(),function(data){
		$("select#category-wx_two").html(data);
	});
}
  function wx_two(e){
	$(".form-group.field-category-wx_three").show();
		$.post("site?typeid=2&pid="+$(e).val(),function(data){
    $("select#category-wx_three").html(data);
     });
}
</script>

控制器内的代码

public function actionSite($pid, $typeid = 0)
    {
    	if($typeid == 1){$aa="请选择二级";}else if($typeid == 2){$aa="请选择三级";}
    
    	echo Html::tag('option',$aa,['value'=>'empty']);
    	
        $model = new WxCategory();
    	if($pid!=null&&$pid!=''&&$pid!="empty"){
    		$model = ArrayHelper::map(WxCategory::findAll(array('parent_id'=>$pid)),'id','name');
    		foreach($model as $value=>$name)
    		{
    			echo Html::tag('option',Html::encode($name),array('value'=>$value));
    		}
    	
    	}
    }
}

WxCategory数据库结构

SET FOREIGN_KEY_CHECKS=0;

-- Table structure for wx_category
-- ----------------------------
DROP TABLE IF EXISTS wx_category;
CREATE TABLE 361_wx_category (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(20) NOT NULL DEFAULT '0',
parent_id int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (id)
) ENGINE=MyISAM AUTO_INCREMENT=538372816 DEFAULT CHARSET=utf8;

-- Records of wx_category
-- ----------------------------
INSERT INTO wx_category VALUES ('538071212', '数据1', '0');
INSERT INTO wx_category VALUES ('538072052', '数据2', '0');
INSERT INTO wx_category VALUES ('538071215', '数据3', '538071212');
INSERT INTO wx_category VALUES ('538071218', '数据4', '538071212');
INSERT INTO wx_category VALUES ('537071260', '数据5', '538071215');
INSERT INTO wx_category VALUES ('537071265', '数据6', '538071215');
INSERT INTO wx_category VALUES ('537071284', '数据7', '538071215');
觉得很赞
  • 评论于 2015-11-06 11:36 举报
    我在https://github.com/bubifengyun/book-yii2-dev-process,准备借助yiichina里的教程,编辑成一本书,最后用某高校LaTeX论文模板美化一下,每月或者更长时间发布PDF教程总结,求支持。
    

    我想把你的教程录入到这本书里,并记录您的名字及原网址,可否?谢谢啦。

您需要登录后才可以评论。登录 | 立即注册