在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: '名稱', 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;
}
}
















免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM