在layui中,新的页面怎么获取另一个页面传过来的数据,并可以对数据进行判断,layui中的后台分页(table)。

例如:打开一个新页面的同时,传数据。

layer.open({
type: 2,
title: '新增项目',
shadeClose: false,
shade: [0.3],
maxmin: true, //开启最大化最小化按钮
area: ['900px', '90%'],
scrollbar: false, //屏蔽滚动条
content: 'operate.html?type=add&buildingId=' + buildingId + '&businessId=' + businessId
});


=======================================


// 在新的页面: 操作类型:add新增,edit修改,view查看
var operateType = QueryUtils.GetQueryString("type");

switch (operateType) {
case "add":
break;
  case "edit":
break;
  case "view":
break;
}


=======================================

这个是layui中的table分页

table.render({
elem: '#roomTable',
url: '/room/json/',
page: true,
limit: 20,
height: 'full-150',
where: data,
cols: [[
{field: 'ROWNUM', fixed: true}
/*, {field: 'ROOM_NAME', title: '名称', 84}*/
, {field: 'ROOM_NUM', title: '编号', 100}
, {field: 'NAME', title: '项目类型', 100}
, {field: 'ROOM_AREA', title: '面积(㎡)', 100}
, {field: 'UNIT_NAME', title: '单元', 80}
, {field: 'FLOOR_NAME', title: '楼层', 80}
, {field: 'PROPERTY_RIGHT_TYPE', title: '产权所属', templet: '#prorigTpl', 120}
, {field: 'ROOM_INFO', title: '备注', 100}
, {field: 'CREATE_NAME', title: '创建人', 120}
, {field: 'CREATE_DATE', title: '创建时间', 180, align: 'center'}
, {field: 'IS_OPEN', title: '是否开启', templet: '#statusTpl', 100, align: 'center'}
, {fixed: 'right', title: '操作', 360, align: 'center', toolbar: '#barDemo'}
]]
});



controller层:


 /**
* 返回list 列表
*/
@RequestMapping(value = "/json")
@ResponseBody
public Object json(Page page) {
PageData pd = new PageData();
Map<String, Object> map = new HashMap<String, Object>();
List<PageData> varList;
String datalayui = null;
//获取当前登录用户
Subject currentUser = SecurityUtils.getSubject();
Session session = currentUser.getSession();
User user = (User) session.getAttribute(Const.SESSION_USER);
try {
//测试分页 导出为layui的json数据
pd = this.getPageData();
pd.put("USER_ID", user.getUSER_ID());
getPData(page, pd);//必填 2参
varList = roomService.list(page); //列出Building列表
datalayui = getLData(page, varList);//必填 3参
} catch (Exception e) {
logger.error(e.toString(), e);
} finally {
logAfter(logger);
}
return datalayui;
}



这个是page包装类:



package com.fh.entity;

import com.fh.util.Const;
import com.fh.util.PageData;
import com.fh.util.Tools;

public class Page {

private int showCount; //每页显示记录数
private int totalPage; //总页数
private int totalResult; //总记录数
private int currentPage; //当前页
private int currentResult; //当前记录起始索引
private boolean entityOrField; //true:需要分页的地方,传入的参数就是Page实体;false:需要分页的地方,传入的参数所代表的实体拥有Page属性
private String pageStr; //最终页面显示的底部翻页导航,详细见:getPageStr();
private PageData pd = new PageData();



public Page(){
try {
this.showCount = Integer.parseInt(Tools.readTxtFile(Const.PAGE));
} catch (Exception e) {
this.showCount = 15;
}
}

public int getTotalPage() {
if(totalResult%showCount==0)
totalPage = totalResult/showCount;
else
totalPage = totalResult/showCount+1;
return totalPage;
}

public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}

public int getTotalResult() {
return totalResult;
}

public void setTotalResult(int totalResult) {
this.totalResult = totalResult;
}

public int getCurrentPage() {
if(currentPage<=0)
currentPage = 1;
if(currentPage>getTotalPage())
currentPage = getTotalPage();
return currentPage;
}

public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}

public String getPageStr() {
StringBuffer sb = new StringBuffer();
if(totalResult>0){
sb.append(" <ul> ");
if(currentPage==1){
sb.append(" <li><a>共<font color=red>"+totalResult+"</font>条</a></li> ");
sb.append(" <li><input type="number" value="" id="toGoPage" style="50px;text-align:center;float:left" placeholder="页码"/></li> ");
sb.append(" <li style="cursor:pointer;"><a onclick="toTZ();" class="btn btn-mini btn-success">跳转</a></li> ");
sb.append(" <li><a>首页</a></li> ");
sb.append(" <li><a>上页</a></li> ");
}else{
sb.append(" <li><a>共<font color=red>"+totalResult+"</font>条</a></li> ");
sb.append(" <li><input type="number" value="" id="toGoPage" style="50px;text-align:center;float:left" placeholder="页码"/></li> ");
sb.append(" <li style="cursor:pointer;"><a onclick="toTZ();" class="btn btn-mini btn-success">跳转</a></li> ");
sb.append(" <li style="cursor:pointer;"><a onclick="nextPage(1)">首页</a></li> ");
sb.append(" <li style="cursor:pointer;"><a onclick="nextPage("+(currentPage-1)+")">上页</a></li> ");
}
int showTag = 5;//分页标签显示数量
int startTag = 1;
if(currentPage>showTag){
startTag = currentPage-1;
}
int endTag = startTag+showTag-1;
for(int i=startTag; i<=totalPage && i<=endTag; i++){
if(currentPage==i)
sb.append("<li><a><font color='#808080'>"+i+"</font></a></li> ");
else
sb.append(" <li style="cursor:pointer;"><a onclick="nextPage("+i+")">"+i+"</a></li> ");
}
if(currentPage==totalPage){
sb.append(" <li><a>下页</a></li> ");
sb.append(" <li><a>尾页</a></li> ");
}else{
sb.append(" <li style="cursor:pointer;"><a onclick="nextPage("+(currentPage+1)+")">下页</a></li> ");
sb.append(" <li style="cursor:pointer;"><a onclick="nextPage("+totalPage+")">尾页</a></li> ");
}
sb.append(" <li><a>第"+currentPage+"页</a></li> ");
sb.append(" <li><a>共"+totalPage+"页</a></li> ");


sb.append(" <li><select title='显示条数' style="55px;float:left;" onchange="changeCount(this.value)"> ");
sb.append(" <option value='"+showCount+"'>"+showCount+"</option> ");
sb.append(" <option value='10'>10</option> ");
sb.append(" <option value='20'>20</option> ");
sb.append(" <option value='30'>30</option> ");
sb.append(" <option value='40'>40</option> ");
sb.append(" <option value='50'>50</option> ");
sb.append(" <option value='60'>60</option> ");
sb.append(" <option value='70'>70</option> ");
sb.append(" <option value='80'>80</option> ");
sb.append(" <option value='90'>90</option> ");
sb.append(" <option value='99'>99</option> ");
sb.append(" </select> ");
sb.append(" </li> ");



sb.append("</ul> ");
sb.append("<script type="text/javascript"> ");

//换页函数
sb.append("function nextPage(page){");
sb.append(" top.jzts();");
sb.append(" if(true && document.forms[0]){ ");
sb.append(" var url = document.forms[0].getAttribute("action"); ");
sb.append(" if(url.indexOf('?')>-1){url += "&"+(entityOrField?"currentPage":"page.currentPage")+"=";} ");
sb.append(" else{url += "?"+(entityOrField?"currentPage":"page.currentPage")+"=";} ");
sb.append(" url = url + page + "&" +(entityOrField?"showCount":"page.showCount")+"="+showCount+""; ");
sb.append(" document.forms[0].action = url; ");
sb.append(" document.forms[0].submit(); ");
sb.append(" }else{ ");
sb.append(" var url = document.location+''; ");
sb.append(" if(url.indexOf('?')>-1){ ");
sb.append(" if(url.indexOf('currentPage')>-1){ ");
sb.append(" var reg = /currentPage=\d*/g; ");
sb.append(" url = url.replace(reg,'currentPage='); ");
sb.append(" }else{ ");
sb.append(" url += "&"+(entityOrField?"currentPage":"page.currentPage")+"="; ");
sb.append(" } ");
sb.append(" }else{url += "?"+(entityOrField?"currentPage":"page.currentPage")+"=";} ");
sb.append(" url = url + page + "&" +(entityOrField?"showCount":"page.showCount")+"="+showCount+""; ");
sb.append(" document.location = url; ");
sb.append(" } ");
sb.append("} ");

//调整每页显示条数
sb.append("function changeCount(value){");
sb.append(" top.jzts();");
sb.append(" if(true && document.forms[0]){ ");
sb.append(" var url = document.forms[0].getAttribute("action"); ");
sb.append(" if(url.indexOf('?')>-1){url += "&"+(entityOrField?"currentPage":"page.currentPage")+"=";} ");
sb.append(" else{url += "?"+(entityOrField?"currentPage":"page.currentPage")+"=";} ");
sb.append(" url = url + "1&" +(entityOrField?"showCount":"page.showCount")+"="+value; ");
sb.append(" document.forms[0].action = url; ");
sb.append(" document.forms[0].submit(); ");
sb.append(" }else{ ");
sb.append(" var url = document.location+''; ");
sb.append(" if(url.indexOf('?')>-1){ ");
sb.append(" if(url.indexOf('currentPage')>-1){ ");
sb.append(" var reg = /currentPage=\d*/g; ");
sb.append(" url = url.replace(reg,'currentPage='); ");
sb.append(" }else{ ");
sb.append(" url += "1&"+(entityOrField?"currentPage":"page.currentPage")+"="; ");
sb.append(" } ");
sb.append(" }else{url += "?"+(entityOrField?"currentPage":"page.currentPage")+"=";} ");
sb.append(" url = url + "&" +(entityOrField?"showCount":"page.showCount")+"="+value; ");
sb.append(" document.location = url; ");
sb.append(" } ");
sb.append("} ");

//跳转函数
sb.append("function toTZ(){");
sb.append("var toPaggeVlue = document.getElementById("toGoPage").value;");
sb.append("if(toPaggeVlue == ''){document.getElementById("toGoPage").value=1;return;}");
sb.append("if(isNaN(Number(toPaggeVlue))){document.getElementById("toGoPage").value=1;return;}");
sb.append("nextPage(toPaggeVlue);");
sb.append("} ");
sb.append("</script> ");
}
pageStr = sb.toString();
return pageStr;
}

public void setPageStr(String pageStr) {
this.pageStr = pageStr;
}

public int getShowCount() {
return showCount;
}

public void setShowCount(int showCount) {

this.showCount = showCount;
}

public int getCurrentResult() {
currentResult = (getCurrentPage()-1)*getShowCount();
if(currentResult<0)
currentResult = 0;
return currentResult;
}

public void setCurrentResult(int currentResult) {
this.currentResult = currentResult;
}

public boolean isEntityOrField() {
return entityOrField;
}

public void setEntityOrField(boolean entityOrField) {
this.entityOrField = entityOrField;
}

public PageData getPd() {
return pd;
}

public void setPd(PageData pd) {
this.pd = pd;
}



这个是:PageData类:



import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

public class PageData extends HashMap implements Map{

private static final long serialVersionUID = 1L;

Map map = null;
HttpServletRequest request;

public PageData(HttpServletRequest request){
this.request = request;
Map properties = request.getParameterMap();
Map returnMap = new HashMap();
Iterator entries = properties.entrySet().iterator();
Entry entry;
String name = "";
String value = "";
while (entries.hasNext()) {
entry = (Entry) entries.next();
name = (String) entry.getKey();
Object valueObj = entry.getValue();
if(null == valueObj){
value = "";
}else if(valueObj instanceof String[]){
String[] values = (String[])valueObj;
for(int i=0;i<values.length;i++){
value = values[i] + ",";
}
value = value.substring(0, value.length()-1);
}else{
value = valueObj.toString();
}
returnMap.put(name, value);
}
map = returnMap;
}

public PageData() {
map = new HashMap();
}

@Override
public Object get(Object key) {
Object obj = null;
if(map.get(key) instanceof Object[]) {
Object[] arr = (Object[])map.get(key);
obj = request == null ? arr:(request.getParameter((String)key) == null ? arr:arr[0]);
} else {
obj = map.get(key);
}
return obj;
}

public String getString(Object key) {
return (String)get(key);
}

@SuppressWarnings("unchecked")
@Override
public Object put(Object key, Object value) {
return map.put(key, value);
}

@Override
public Object remove(Object key) {
return map.remove(key);
}

public void clear() {
map.clear();
}

public boolean containsKey(Object key) {
// TODO Auto-generated method stub
return map.containsKey(key);
}

public boolean containsValue(Object value) {
// TODO Auto-generated method stub
return map.containsValue(value);
}

public Set entrySet() {
// TODO Auto-generated method stub
return map.entrySet();
}

public boolean isEmpty() {
// TODO Auto-generated method stub
return map.isEmpty();
}

public Set keySet() {
// TODO Auto-generated method stub
return map.keySet();
}

@SuppressWarnings("unchecked")
public void putAll(Map t) {
// TODO Auto-generated method stub
map.putAll(t);
}

public int size() {
// TODO Auto-generated method stub
return map.size();
}

public Collection values() {
// TODO Auto-generated method stub
return map.values();
}

}



这个是:组装:getPData(page, pd);//必填 2参

import com.fh.entity.Page;
import com.fh.entity.system.User;
import com.fh.util.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import javax.xml.crypto.Data;
import java.util.List;

public class GetDatalayui {
protected static Logger logger = Logger.getLogger(GetDatalayui.class);
public static void getPData(Page page, PageData pd) {
//分页暂时设置状态
page.setShowCount(Integer.parseInt(pd.get("limit").toString()));
page.setCurrentPage(Integer.parseInt(pd.get("page").toString()));
page.setPd(pd);
}

public static String getLData(Page page, List<PageData> varList) {
String start="{"code":0,"msg":"","count":" +page.getTotalResult()+","data":";
String end="}";
//返回layui table接收的json数据
String datalayui = null;
if (varList != null && varList.size() > 0) {
for (int i = 0; i < varList.size(); i++) {
varList.get(i).put("ROWNUM", (page.getCurrentPage() - 1) * 10 + i + 1);
}
}
datalayui= JSONHelper.array2json(varList);
datalayui=start+datalayui+end;
return datalayui;
}
public static String getusername(){
//获取当前登录用户
Subject currentUser = SecurityUtils.getSubject();
Session session = currentUser.getSession();
User user = (User) session.getAttribute(Const.SESSION_USER);
return user.getUSERNAME();
}

public static int getPageTotal(int num,int pageCount,Page page){
page.setTotalResult(num+pageCount);
return num+pageCount;
}
}















原文地址:https://www.cnblogs.com/songyinan/p/8574035.html