1:DeliveryPersonVO对象

package com.funcanteen.business.entity.delivery.vo;

import java.util.List;
import com.funcanteen.business.entity.delivery.DeliveryPersonCampus;
import com.funcanteen.business.entity.delivery.DeliveryPersonStall; public class DeliveryPersonVO {
/**
* 校区
*/
private DeliveryPersonCampus deliveryPersonCampus;
/**
* 档口集合
*/
private List<DeliveryPersonStall> deliveryPersonStallList;
/**
* 校区集合
*/
private List<DeliveryPersonCampus> campusList;
public DeliveryPersonCampus getDeliveryPersonCampus() {
return deliveryPersonCampus;
}
public void setDeliveryPersonCampus(DeliveryPersonCampus deliveryPersonCampus) {
this.deliveryPersonCampus = deliveryPersonCampus;
}
public List<DeliveryPersonStall> getDeliveryPersonStallList() {
return deliveryPersonStallList;
}
public void setDeliveryPersonStallList(List<DeliveryPersonStall> deliveryPersonStallList) {
this.deliveryPersonStallList = deliveryPersonStallList;
}
public List<DeliveryPersonCampus> getCampusList() {
return campusList;
}
public void setCampusList(List<DeliveryPersonCampus> campusList) {
this.campusList = campusList;
} }

2:DeliveryPersonCampus 对象

package com.funcanteen.business.entity.delivery;
/**
*
* @author ckj
*
*/
public class DeliveryPersonCampus {
private Integer campusId;
private String campusName; public Integer getCampusId() {
return campusId;
}
public void setCampusId(Integer campusId) {
this.campusId = campusId;
}
public String getCampusName() {
return campusName;
}
public void setCampusName(String campusName) {
this.campusName = campusName;
} }

3:DeliveryPersonStall 对象

package com.funcanteen.business.entity.delivery;
/**
*
* @author ckj
*
*/
public class DeliveryPersonStall {
private Integer stallId;
private String stallName; public Integer getStallId() {
return stallId;
}
public void setStallId(Integer stallId) {
this.stallId = stallId;
}
public String getStallName() {
return stallName;
}
public void setStallName(String stallName) {
this.stallName = stallName;
}
}

4:DeliveryPersonCampus

package com.funcanteen.business.entity.delivery;
/**
*
* @author ckj
*
*/
public class DeliveryPersonCampus {
private Integer campusId;
private String campusName; public Integer getCampusId() {
return campusId;
}
public void setCampusId(Integer campusId) {
this.campusId = campusId;
}
public String getCampusName() {
return campusName;
}
public void setCampusName(String campusName) {
this.campusName = campusName;
} }

5jsp 页面

            <div id="addcampus" name="addcampus" class="control-group form-group" style="margin-bottom: 0">
<label class="col-md-2 control-label" style="margin-right: 14px">是否再添加校区</label>
<div class="form-group">
<select id="addCampusId" name="addCampusId" class="form-control" style="margin-bottom: 0;width:178px" onchange="queryCampusIdToStall()">
<option value="">所有</option>
<c:forEach items="${campusAllList }" var="campus">
<option value="${campus.id }">${campus.name }</option>
</c:forEach>
</select>
</div>
</div> <div id="campusAll" name="campusAll" class="control-group form-group">
<label class="col-md-2 control-label" style="margin-right: 14px">校区</label>
<div class="parent_campus" style="width:60%; float:left;">
<c:forEach items="${campusList }" var="campus">
<div style="width: 100%" class="choose_label">
<input type="checkbox" name="campusId" id="${campus.id }" value="${campus.id }" checked="checked"/>
<label for="${campus.id }">${campus.name }</label>
</div>
</c:forEach>
</div> </div>

6:ajax 请求

//点击校区获取饭堂
function queryCampusIdToStall() {
var campusId = [];
$('input[name="campusId"]').each(function(i,o){
campusId.push(o.value);
}); var addCampusId = $("#addCampusId").val();
var deliveryPersonId = $("#deliveryPersonId").val();
if (addCampusId != null && addCampusId != "") {
$.ajax({
type: "GET",
url: "<%=basePath %>delivery/deliveryperson/update!queryCampusIdToStall",
data: {
"addCampusId" : addCampusId,
"campusId": campusId,
},
dataType: "json",
success: function(data){
if (data != null) {
var campus = data.deliveryPersonCampus;
var stallList=data.deliveryPersonStallList;
var campusList = data.campusList;
//请求下拉校区重新绑定新数据
$("#addCampusId").empty();
$('#addCampusId').append($("<option>").text("所有").val(""));
for(var item in campusList){
$("#addCampusId").append($("<option>").val(campusList[item].campusId).text(campusList[item].campusName));
}
//动态添加校区和档口
addCampus(campus.campusId,campus.campusName);
for(var item in stallList){
addStall(stallList[item].stallId,stallList[item].stallName);
}
}
}
});
}
}

7:java后台代码

    /**
* ajax 请求获取档口
*
* @throws IOException
*/
public void queryCampusIdToStall() throws IOException {
HttpServletResponse response = ServletActionContext.getResponse();
DeliveryPersonVO deliveryPersonVO = new DeliveryPersonVO();
Campus campus = new Campus();
DeliveryPersonCampus deliveryPersonCampus = new DeliveryPersonCampus();
DeliveryPersonStall deliveryPersonStall = null;
List<DeliveryPersonStall> personStallList = new ArrayList<DeliveryPersonStall>();
List<DeliveryPersonCampus> personCampusList = new ArrayList<DeliveryPersonCampus>();// 动态重新封装下拉框校区数据 //获取校区
campus = campusService.load(addCampusId.longValue());
deliveryPersonCampus.setCampusId(campus.getId().intValue());
deliveryPersonCampus.setCampusName(campus.getName());
// 根据用户选择的校区ID获取档口数据档口
List<Stall> stallsList = stallService.getCampusIdToStallList(addCampusId.intValue());
if (stallsList != null && stallsList.size() > 0) {
for (Stall stall : stallsList) {
deliveryPersonStall = new DeliveryPersonStall();
deliveryPersonStall.setStallId(stall.getId().intValue());
deliveryPersonStall.setStallName(stall.getName());
personStallList.add(deliveryPersonStall);
}
}
//获取重新要加载的下拉框校区数据
List<Integer> campusIdsList = new ArrayList<Integer>();
campusIdsList.add(addCampusId.intValue());
if(campusId !=null && campusId.length>0){
for(int i=0;i<campusId.length;i++){
campusIdsList.add(campusId[i]);
}
}
List<Campus> notDeliverPersonList = campusService.getNotDeliverPersonList(campusIdsList);
for(Campus cp : notDeliverPersonList){
DeliveryPersonCampus dpc =new DeliveryPersonCampus();
dpc.setCampusId(cp.getId().intValue());
dpc.setCampusName(cp.getName());
personCampusList.add(dpc);
}
//set VO
deliveryPersonVO.setDeliveryPersonCampus(deliveryPersonCampus);//单个校区
deliveryPersonVO.setDeliveryPersonStallList(personStallList);//某校区区所有档口
deliveryPersonVO.setCampusList(personCampusList);//除以选中的所有的校区
//转JSON
String jsonString = JSON.toJSONString(deliveryPersonVO);
response.setContentType("text/plain;charset=" + "utf-8");
response.setCharacterEncoding("utf-8");
response.getWriter().write(jsonString); }

8:json格式数据

{
"campusList": [
{
"campusId": 98,
"campusName": "揭阳市职业技术学院"
},
{
"campusId": 99,
"campusName": "汕头职业技术学院"
},
{
"campusId": 100,
"campusName": "广东工商职业学院肇庆校区"
},
{
"campusId": 101,
"campusName": "岭南职业技术学院(清远校区)"
},
{
"campusId": 102,
"campusName": "测试(江门区)"
},
{
"campusId": 103,
"campusName": "测试"
}
],
"deliveryPersonCampus": {
"campusId": 12,
"campusName": "广东轻工职业技术学院(南海校区)"
},
"deliveryPersonStallList": [
{
"stallId": 56,
"stallName": "原味汤菜饭(一饭二楼)"
},
{
"stallId": 82,
"stallName": "小亨美食(一饭六楼)"
}
]
}

AJAX请求返回JSON数据动态生成html的更多相关文章

  1. 在使用Ajax请求返回json数据的时候IE浏览器弹出下载保存对话框的解决方法

    在使用Ajax请求返回json数据的时候IE浏览器弹出下载保存对话框的解决方法 最近在做一个小东西,使用kindeditor上传图片的时候,自己写了一个上传的方法,按照协议规则通过ajax返回json ...

  2. ajax请求返回json数据弹出下载框的解决方法

    将返回的Content-Type由application/json改为text/html. 在struts2下: <action name="XXXAjax" class=& ...

  3. AJAX请求,返回json进行页面绑值

    AJAX请求,返回json进行页面绑值 后台 controller @RequestMapping(value = "backjson.do",method=RequestMeth ...

  4. ashx文件结合ajax使用(返回json数据)

    ashx文件返回json数据: public void ProcessRequest(HttpContext context) { context.Response.ContentType = &qu ...

  5. Ajax前台返回JSON数据后再Controller中直接转换成类型使用,后台接收json转成实体的方法

    之前写过一篇记录文章,写的是将一个比较复杂的数据结构在前台组合起来后传递到后台. 当时并不太了解@RequestBody,也并没有使用js提供的JSON.stringify()方法 所有都是自己写的, ...

  6. jquery序列化from表单使用ajax提交返回json数据(使用struts2注解result type = json)

    1.action类引入struts2的"json-default"拦截器栈 @ParentPackage("json-default") //示例 @Paren ...

  7. js将json数据动态生成表格

    今天开发中遇到需要展示动态数据的问题, 具体要求是后端传来的json字符串,要在前端页面以table表格的形式展示, 其实没啥难的,就是拼接table标签,纯属体力活,于是自己写了个呆萌,保存起来,以 ...

  8. 使用jQuery发送POST,Ajax请求返回JSON格式数据

    问题: 使用jQuery POST提交数据到PHP文件, PHP返回的json_encode后的数组数据,但jQuery接收到的数据不能解析为JSON对象,而是字符串{"code" ...

  9. ajax请求返回json字符串/json对象 处理

    1. 返回json字符串如何处理 $.ajax({ url:xxx, success:function(date){ }, error:function(){ } }); 通过最原始的返回: Prin ...

随机推荐

  1. 一文搞懂vim复制粘贴

    转载自本人独立博客https://liushiming.cn/2020/01/18/copy-and-paste-in-vim/ 概述 复制粘贴是文本编辑最常用的功能,但是在vim中复制粘贴还是有点麻 ...

  2. oracle中以dba_、user_、v$_、all_、session_、index_开头

    原 oracle中以dba_.user_.v$_.all_.session_.index_开头 2011年07月05日 11:26:06 clbxp 阅读数:3279   oracle中以dba_.u ...

  3. VS Code的git配置

    最近打算使用VS Code作为python的编辑器,这里记录一下VS Code中git的配置方法 因为vscode中git只是使用本地的git,所以本地必须先安装git才行. 1.git的安装 git ...

  4. txt文件太大打不开怎么办

    #开始 最近在调试代码的时候,生成了一个400MB的日志文件 找了很多文本编辑器,都表示太大了打不开 QAQ #解决方案 百度下载 “txt杀手” 用这个软件把文本文件拆分成小份就可以打开了 输入如图 ...

  5. MYSQL 传汉字获取拼音首字母

    --获取单个汉字首字母拼音 --CREATE DEFINER=`by`@`%` FUNCTION `fun_first_pinyin`(`P_NAME` VARCHAR(5)) RETURNS var ...

  6. iOS 开发之函数式编程思想(Functional Programming)

    函数式编程(Functional Programming), 函数式编程强调的函数:1.不依赖外部状态:2.不改变外部状态. 函数式编程可解决线程安全问题,每一个函数都是线程安全的. 时间状态:变量一 ...

  7. Codeforces Round #575 (Div. 3) 题解

    比赛链接:https://codeforc.es/contest/1196 A. Three Piles of Candies 题意:两个人分三堆糖果,两个人先各拿一堆,然后剩下一堆随意分配,使两个人 ...

  8. 红黑树java代码实现

    红黑树 思想源于:https://www.cnblogs.com/nananana/p/10434549.html有解释有图,很清晰(删除时需考虑根节点和兄弟节点的子节点是否存在) package t ...

  9. pillow 初级用法

    # 转载至:https://www.cnblogs.com/apexchu/p/4231041.html Image类 Pillow中最重要的类就是Image,该类存在于同名的模块中.可以通过以下几种 ...

  10. linux的数据盘挂载

    图文教程: Linux的云服务器数据盘未做分区和格式化,可以根据以下步骤进行分区以及格式化操作. 一:登陆 用Linux 的SSH 登陆软件(xshell 或者putty) 登陆阿里云主机服务器. 二 ...