Java中获取数据库中的数据,获取下拉列表中的数据, 获取省、市、区的数据,根据上一层去控制下一层

$(function() {
    //validateRule();
    getDept();
    $("select[name='deptNamee']").change(function() {
        var deptCode = $("select[name='deptNamee']").val();
        getSys(deptCode);
    });
});

function getDept() {
    var deptCode;
    $.get("/system/sysDept/list", {
        method : "initDept"
    }, function(data) {
        $.each(data, function(i, d) {
            $("select[name='deptNamee']").append(
                "<option value='"+d.deptId+"'>" + d.name
                + "</option>");
        });
        // 获取单位code
        deptCode = data[0].deptId;
        // 根据第一个单位code获取对应系统列表
        getSys(deptCode);
    }, 'json');
}

function getSys(deptCode) {

    // ajax请求所有系统
    $.get("/nsmp/info_sys/list", {
        deptCode : deptCode
    }, function(data) {
        debugger;
        // 先清空系统下拉框
        $("select[name='infoSysId']").empty();
        $.each(data.rows, function(i, d) {
            $("select[name='infoSysId']").append(
                "<option value='"+d.id+"'>" + d.sysName
                + "</option>");
        });
    }, 'json');
}
function save() {
    $.ajax({
        cache : true,
        type : "POST",
        url : "/nsmp/grade/save",
        data : $('#basicInfoForm').serialize(), // 你的formid
        async : false,
        error : function(request) {
            alert("Connection error");
        },
        success : function(data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引

                parent.layer.close(index);

            } else {
                parent.layer.msg(data.msg);
            }
        }
    });
}

/*
function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#basicInfoForm").validate({
        rules : {
            deptName : {
                required : true
            }
        },
        messages : {
            deptName : {
                required : icon + "请输入单位名"
            }
        }
    });
}*/

方法是这样的,首先在xml中配置查询语句,使用的是if添加条件的查询。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.bootdo.nsmp.dao.InfoSysDao">
	<!--list-->
	<select id="list" resultType="com.bootdo.nsmp.domain.InfoSys">
		select * from info_sys
		<where>
			<if test="deptNamee != null and deptNamee != ''"> and dept_namee = #{deptNamee} </if>
		</where>
		<if test="offset != null and limit != null">
			limit ${offset}, ${limit}
		</if>
	</select>
	<!--count-->
	<select id="count" resultType="int">
		select count(*) from info_sys
	</select>
	<!--save-->

然后在前端去解析list,获取个别的字段的内容。

其实在里边还上了上一层控制下一层的操作,就比如像选择省、市、区,在选择了省之后,才可以选择市,更换了省之后,要更换到相应地市下边去。

猜你喜欢

转载自blog.csdn.net/wyqwilliam/article/details/83514013