例如:打开一个新页面的同时,传数据。
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: '名称', width: 84}*/
, {field: 'ROOM_NUM', title: '编号', width: 100}
, {field: 'NAME', title: '项目类型', width: 100}
, {field: 'ROOM_AREA', title: '面积(㎡)', width: 100}
, {field: 'UNIT_NAME', title: '单元', width: 80}
, {field: 'FLOOR_NAME', title: '楼层', width: 80}
, {field: 'PROPERTY_RIGHT_TYPE', title: '产权所属', templet: '#prorigTpl', width: 120}
, {field: 'ROOM_INFO', title: '备注', width: 100}
, {field: 'CREATE_NAME', title: '创建人', width: 120}
, {field: 'CREATE_DATE', title: '创建时间', width: 180, align: 'center'}
, {field: 'IS_OPEN', title: '是否开启', templet: '#statusTpl', width: 100, align: 'center'}
, {fixed: 'right', title: '操作', width: 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>\n");
if(currentPage==1){
sb.append(" <li><a>共<font color=red>"+totalResult+"</font>条</a></li>\n");
sb.append(" <li><input type=\"number\" value=\"\" id=\"toGoPage\" style=\"width:50px;text-align:center;float:left\" placeholder=\"页码\"/></li>\n");
sb.append(" <li style=\"cursor:pointer;\"><a onclick=\"toTZ();\" class=\"btn btn-mini btn-success\">跳转</a></li>\n");
sb.append(" <li><a>首页</a></li>\n");
sb.append(" <li><a>上页</a></li>\n");
}else{
sb.append(" <li><a>共<font color=red>"+totalResult+"</font>条</a></li>\n");
sb.append(" <li><input type=\"number\" value=\"\" id=\"toGoPage\" style=\"width:50px;text-align:center;float:left\" placeholder=\"页码\"/></li>\n");
sb.append(" <li style=\"cursor:pointer;\"><a onclick=\"toTZ();\" class=\"btn btn-mini btn-success\">跳转</a></li>\n");
sb.append(" <li style=\"cursor:pointer;\"><a onclick=\"nextPage(1)\">首页</a></li>\n");
sb.append(" <li style=\"cursor:pointer;\"><a onclick=\"nextPage("+(currentPage-1)+")\">上页</a></li>\n");
}
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>\n");
else
sb.append(" <li style=\"cursor:pointer;\"><a onclick=\"nextPage("+i+")\">"+i+"</a></li>\n");
}
if(currentPage==totalPage){
sb.append(" <li><a>下页</a></li>\n");
sb.append(" <li><a>尾页</a></li>\n");
}else{
sb.append(" <li style=\"cursor:pointer;\"><a onclick=\"nextPage("+(currentPage+1)+")\">下页</a></li>\n");
sb.append(" <li style=\"cursor:pointer;\"><a onclick=\"nextPage("+totalPage+")\">尾页</a></li>\n");
}
sb.append(" <li><a>第"+currentPage+"页</a></li>\n");
sb.append(" <li><a>共"+totalPage+"页</a></li>\n");
sb.append(" <li><select title='显示条数' style=\"width:55px;float:left;\" onchange=\"changeCount(this.value)\">\n");
sb.append(" <option value='"+showCount+"'>"+showCount+"</option>\n");
sb.append(" <option value='10'>10</option>\n");
sb.append(" <option value='20'>20</option>\n");
sb.append(" <option value='30'>30</option>\n");
sb.append(" <option value='40'>40</option>\n");
sb.append(" <option value='50'>50</option>\n");
sb.append(" <option value='60'>60</option>\n");
sb.append(" <option value='70'>70</option>\n");
sb.append(" <option value='80'>80</option>\n");
sb.append(" <option value='90'>90</option>\n");
sb.append(" <option value='99'>99</option>\n");
sb.append(" </select>\n");
sb.append(" </li>\n");
sb.append("</ul>\n");
sb.append("<script type=\"text/javascript\">\n");
//换页函数
sb.append("function nextPage(page){");
sb.append(" top.jzts();");
sb.append(" if(true && document.forms[0]){\n");
sb.append(" var url = document.forms[0].getAttribute(\"action\");\n");
sb.append(" if(url.indexOf('?')>-1){url += \"&"+(entityOrField?"currentPage":"page.currentPage")+"=\";}\n");
sb.append(" else{url += \"?"+(entityOrField?"currentPage":"page.currentPage")+"=\";}\n");
sb.append(" url = url + page + \"&" +(entityOrField?"showCount":"page.showCount")+"="+showCount+"\";\n");
sb.append(" document.forms[0].action = url;\n");
sb.append(" document.forms[0].submit();\n");
sb.append(" }else{\n");
sb.append(" var url = document.location+'';\n");
sb.append(" if(url.indexOf('?')>-1){\n");
sb.append(" if(url.indexOf('currentPage')>-1){\n");
sb.append(" var reg = /currentPage=\\d*/g;\n");
sb.append(" url = url.replace(reg,'currentPage=');\n");
sb.append(" }else{\n");
sb.append(" url += \"&"+(entityOrField?"currentPage":"page.currentPage")+"=\";\n");
sb.append(" }\n");
sb.append(" }else{url += \"?"+(entityOrField?"currentPage":"page.currentPage")+"=\";}\n");
sb.append(" url = url + page + \"&" +(entityOrField?"showCount":"page.showCount")+"="+showCount+"\";\n");
sb.append(" document.location = url;\n");
sb.append(" }\n");
sb.append("}\n");
//调整每页显示条数
sb.append("function changeCount(value){");
sb.append(" top.jzts();");
sb.append(" if(true && document.forms[0]){\n");
sb.append(" var url = document.forms[0].getAttribute(\"action\");\n");
sb.append(" if(url.indexOf('?')>-1){url += \"&"+(entityOrField?"currentPage":"page.currentPage")+"=\";}\n");
sb.append(" else{url += \"?"+(entityOrField?"currentPage":"page.currentPage")+"=\";}\n");
sb.append(" url = url + \"1&" +(entityOrField?"showCount":"page.showCount")+"=\"+value;\n");
sb.append(" document.forms[0].action = url;\n");
sb.append(" document.forms[0].submit();\n");
sb.append(" }else{\n");
sb.append(" var url = document.location+'';\n");
sb.append(" if(url.indexOf('?')>-1){\n");
sb.append(" if(url.indexOf('currentPage')>-1){\n");
sb.append(" var reg = /currentPage=\\d*/g;\n");
sb.append(" url = url.replace(reg,'currentPage=');\n");
sb.append(" }else{\n");
sb.append(" url += \"1&"+(entityOrField?"currentPage":"page.currentPage")+"=\";\n");
sb.append(" }\n");
sb.append(" }else{url += \"?"+(entityOrField?"currentPage":"page.currentPage")+"=\";}\n");
sb.append(" url = url + \"&" +(entityOrField?"showCount":"page.showCount")+"=\"+value;\n");
sb.append(" document.location = url;\n");
sb.append(" }\n");
sb.append("}\n");
//跳转函数
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("}\n");
sb.append("</script>\n");
}
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;
}
}