springMVC实现ajax分页

服务端代码

	/**
	 * 付费问题详情
	 * 
	 * @return
	 */
	@RequestMapping(value = "/questionPay/{id}.html", produces = "text/html;charset=UTF-8")
	public String questionPay(@PathVariable Long id, ModelMap model, HttpServletRequest request) {
		try {
			QidaQuestion question = qidaQuestionService.get(id);
			question.setPageViews((question.getPageViews() == null ? 0 : question.getPageViews()) + 1);
			qidaQuestionService.save(question);
			model.put("question", question);
			model.put("nowDate", new Date());
			FuUser fuUser = (FuUser) request.getSession().getAttribute("fuUser");
			model.put("fuUser", fuUser);
			if (fuUser != null) {
				// 用户关注
				List<QidaConcern> concernList = qidaConcernService.findByMySelf(fuUser.getId());
				model.put("concernList", concernList);
				// 用户收藏
				List<QidaCollection> collectionList = qidaCollectionService.findByMySelf(fuUser.getId());
				model.put("collectionList", collectionList);
				// 是否关注
				int res = qidaConcernService.findIsWatch(fuUser, question.getFuUser());
				model.put("res", res);
				// 是否收藏
				QidaCollection collection = qidaCollectionService.findByQuestion(fuUser.getId(), id);
				if (collection == null) {
					model.put("isCollection", 0);
				} else {
					model.put("isCollection", 1);
				}
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("qidaRank", "专家");
			Integer totalCount = fuUserService.getCount(map);
			model.put("totalCount", totalCount);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "qida/questionDetail/questionPay";
	}

	/**
	 * 分页查询专家列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/questionExpert.html", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String questionExpert(Integer pageNo, Integer pageSize, ModelMap model) {
		JSONObject json = new JSONObject();
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("qidaRank", "专家");
			List<FuUser> expertList = fuUserService.findList((pageNo - 1) * pageSize, pageSize, map);
			JSONArray array = new JSONArray();
			if (expertList != null && expertList.size() > 0) {
				for (FuUser user : expertList) {
					JSONObject obj = new JSONObject();
					obj.put("id", user.getId());
					obj.put("userAvatar", user.getUserAvatar() == null ? "../../images_qiDa/tx.png" : user.getUserAvatar());
					obj.put("nickName", user.getNickName() == null ? "佚名" : user.getNickName());
					array.add(obj);
				}
				json.put("array", array);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json.toString();
	}

	/**
	 * 邀请专家解答付费问题
	 */
	@RequestMapping(value = "/InvitationAnswer/{expertId}/{questionId}.html", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String InvitationAnswer(@PathVariable Long expertId, @PathVariable Long questionId) {
		try {
			QidaQuestion question = qidaQuestionService.get(questionId);
			question.setAnswerUser(fuUserService.get(expertId));
			qidaQuestionService.save(question);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}


前端代码

html代码

<div class="discussBtnBod">
	<c:if test="${fuUser.id==question.fuUser.id}">
	<a class="invite"><i class="inviteImg"></i>邀请专家<input class="inviteHid" type="hidden" value="0"/></a>
	</c:if>
</div>
<div class="zjList">
	<div class="zjInfo">您已邀请${empty question.answerUser?'':question.answerUser.nickName==null?'佚名':question.answerUser.nickName}</div>
	<ul class="zjList-ul">
	</ul>
	<nav class="fenye">
	<ul class="pager">
		<li><a href="javascript:void(0)" οnclick="lastPage()">上一页</a></li>
		<li><a href="javascript:void(0)" οnclick="nextPage()">下一页</a></li>
	</ul>
	</nav>
</div>

js代码

        var totalCount=${totalCount};
		var pageNo=1;
		var pageSize=4;
		var totalPage=(totalCount%pageSize==0)?totalCount/pageSize:totalCount/pageSize+1;
		searchExpert(pageNo,pageSize);
		
		//分页查询邀请的专家
		function searchExpert(pageNo,pageSize){
			$.post("${ctx}/web/qida/questionExpert.html",{pageNo:pageNo,pageSize:pageSize},function(data){
				var data=eval("("+data+")");
				$(".zjList-ul").find("li").remove(); 
				var length=data.array.length;
				var result = '';
				for(var i=0;i<length;i++){
					var arrText = [];
					arrText.push("<li><div class='tx'><a class='txImg'><img src='"+data.array[i].userAvatar+"' /></a></div>");
					arrText.push("<div class='zjList-infos'><div class='zjList-inf userNm grayCol'><span class='smalBlu'>"+array[i].nickName+"</span><br></div>");
					if(${question.answerUser==null}){
					      arrText.push("<a class='zjList-btn' οnclick='InvitationAnswer("+data.array[i].id+",${question.id})'>邀请回答</a>");
					}
					if(data.array[i].id==${question.answerUser==null?0:question.answerUser.id}){
                                arrText.push("<a class='zjList-btnac'>已邀请</a>");
                         }
					arrText.push("</div></li>");
					result +=  arrText.join('');
				}
				$(".zjList-ul").append(result);
			})
		}
		
		//上一页
		function lastPage(){
			pageNo -= 1;
			pageNo=pageNo>=0?pageNo:0;
			searchExpert(pageNo,pageSize);
		}
		
		//下一页
		function nextPage(){
			if(pageNo+1<=totalPage){
				pageNo += 1;
			}
			searchExpert(pageNo,pageSize);
		}
		
		//邀请专家解答
		function InvitationAnswer(expertId,questionId){
			$.post("${ctx}/web/qida/InvitationAnswer/"+expertId+"/"+questionId+".html",null,function(){
				layer.open({
	 			    content: "邀请成功!",
	 			    btn: ["确定"],
	 			    shadeClose: false,
	 			    yes: function(){
	 			        layer.closeAll();
	 			        location.href=location.href;
	 			    }
 				});
			})
		}

效果图


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于 Spring MVC 和 layui 数据表格的 AJAX 表格查询实现的示例: 1. 首先,定义一个包含搜索条件的实体类: ``` public class SearchParam { private String name; private Integer age; // 省略 getter 和 setter 方法 } ``` 2. 在控制器中定义处理 AJAX 请求的方法: ``` @Controller @RequestMapping("/user") public class UserController { @RequestMapping("/search") @ResponseBody public Map<String, Object> search(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit, SearchParam searchParam) { // 处理查询逻辑,根据搜索条件查询数据并返回 Map<String, Object> result = new HashMap<>(); // 查询总记录数 int count = userService.getCountBySearchParam(searchParam); // 查询当前页数据 List<User> userList = userService.getListBySearchParam(searchParam, page, limit); result.put("code", 0); result.put("msg", ""); result.put("count", count); result.put("data", userList); return result; } } ``` 3. 在页面中使用 layui 数据表格,并编写 AJAX 请求: ``` <script> layui.use('table', function () { var table = layui.table; table.render({ elem: '#userTable', // 指定原始表格元素选择器(如:#id、.class) url: '/user/search', // AJAX 请求的 URL method: 'post', // 请求方式 page: true, // 开启分页 cols: [[ {field: 'id', title: 'ID', sort: true}, {field: 'name', title: '姓名'}, {field: 'age', title: '年龄', sort: true} ]] }); // 监听搜索按钮点击事件 $('#searchBtn').on('click', function () { // 执行搜索操作 table.reload('userTable', { where: { name: $('#name').val(), age: $('#age').val() } }); }); }); </script> ``` 4. 在页面中添加搜索条件输入框和搜索按钮: ``` <div class="layui-form-item"> <div class="layui-inline"> <label class="layui-form-label">姓名</label> <div class="layui-input-inline"> <input id="name" class="layui-input" type="text" placeholder="请输入姓名"> </div> </div> <div class="layui-inline"> <label class="layui-form-label">年龄</label> <div class="layui-input-inline"> <input id="age" class="layui-input" type="text" placeholder="请输入年龄"> </div> </div> <div class="layui-inline"> <button id="searchBtn" class="layui-btn">搜索</button> </div> </div> ``` 以上就是一个基于 Spring MVC 和 layui 数据表格的 AJAX 表格查询实现的示例,具体实现方式可能会因项目需求而有所不同,可根据个人需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值