今日總結:
小菜鳥總是在跌打滾爬中慢慢成長,哈哈!
凡事都有個目的,每一步都是一連串的代碼,當遇到問題的時候,千萬不要一股腦的嘗試然后調試,必須先想想這一步這么做的目的是什么,然后再根據問題分析修改代碼,這樣比一直調試和修改效率更高,而且對於自身的成長也有很大的幫助。
今天,在嘗試使用jqGrid實現列表顯示的時候,在實現數據分頁的過程中遇到一個問題,其實很簡單,但是我硬是搞了一下午。
前台可以將page(當前頁)和rows(當前頁顯示的數據記錄數)傳給后台,后台根據傳遞的參數進行計算,實現分頁。

startID和endID是數據分頁的初始ID和結束ID,然而這兩個數據只需要傳給數據庫,作為sql查詢的條件即可,我卻想盡辦法想要將它們傳遞給前台來實現分頁(根本就沒必要),在這個過程中就報了各種錯誤,什么數據類型轉換錯誤,還有什么變量未定義。
一開始我並沒有思考我為什么要傳這兩個參數,這兩個參數目的何在,就是不斷嘗試解決提示的錯誤,但是到最后一個問題解決了,就有下一個問題出現,然后就不斷解決問題,出現問題,解決問題,惡性循環。結果不但誒呦解決問題,還浪費了很多時間,最后自己也搞得一頭霧水。所以綜上所述,凡事遇到問題,到要先思考為什么,其目的何在,理清思路,才能更快更好的解決問題。
js代碼:
/**
* jQGrid 列表顯示
*/
$(function(){
pageInit();
});
function pageInit(){
jQuery("#userList").jqGrid ({
mtype:'POST',
pager : '#userListPager',
// width: 'auto',
height: 400,
rowNum : 30,
rowList : [ 30, 50, 100 ],
viewrecords : true,
multiselect: true,
url: "/appLTEjzxnService/users/list",//請求的action路徑
datatype : "json",
jsonReader: {
root:"dataRows", page:"page", total:"total", records:"records", repeatitems:false, id : "userId"
},
colNames : [ '用戶ID', '用戶賬號', '密碼','用戶名稱', '手機號', '郵箱', '狀態', '所屬組', 'Eoms用戶ID','創建時間', '所屬公司' ],
colModel : [
{name:'userId',label:'用戶ID',index:'userId',width:60} // 這里的name和index保持一致, 與json數據中的key值保持一致
, {name:'userAccount',label:'用戶賬號',index:'userAccount', width:70}
, {name:'userPassword', label:'密碼',index:'userPassword',width:100}
, {name:'userName',label:'用戶名稱', index:'userName',width:100}
, {name:'userMobile', label:'手機號', index:'userMobile',width:100}
, {name:'userEmail', label:'郵箱',index:'userEmail',width:100}
, {name:'userStatu', label:'狀態',index:'userStatu',width:70}
, {name:'groupId', label:'所屬組',index:'groupId', width:70}
, {name:'eomsUserId', label:'Eoms用戶ID', index:'eomsUserId',width:70}
, {name:'createTime', label:'創建時間',index:'createTime',formatter:"date",formatoptions: {srcformat:'Y-m-d H:i:s',newformat:'Y-m-d H:i:s'},width:100}
, {name:'companyId', label:'所屬公司',index:'companyId', width:70}
],
sortname: 'userId',
sortorder: 'desc',
onSelectRow: function(userId){
}
});
jQuery("#userList").jqGrid('navGrid', '#userListPager', {edit : false,add : false,del : false});
jQuery("#grid_id").jqGrid('setGridParam',{startID:'startID',endID:'endID'}).jqGrid('hideCol',"somecol").trigger("reloadGrid");
}
后台代碼:
package me.gacl.web.controller;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import me.gacl.domain.User;
import me.gacl.service.UserServiceI;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONObject;
@Controller
@RequestMapping("/users")
public class UserController {
@Resource
UserServiceI userService;
@RequestMapping(value = "/list", produces = "text/json;charset=UTF-8")
@ResponseBody
public String getAllUser(HttpServletRequest request,
HttpServletResponse response) {
// String sID=request.getParameter("startID");
// String eID=request.getParameter("endID");
// int startID=Integer.parseInt(sID);
// int endID=Integer.parseInt(eID);
String p = request.getParameter("page"); // 取得當前頁數,這是jqgrid自身的參數
String r = request.getParameter("rows"); // 取得每頁顯示行數,這是jqgrid自身的參數
int page=Integer.parseInt(p);
int rows=Integer.parseInt(r);
int records=userService.getUsersCount(); //總的記錄數
int totalPage = records %rows == 0 ? records
/ rows : records / rows+ 1; // 計算總頁數
List<Object> listGrid = new ArrayList<Object>();
List<User> listUser = userService.getAllUser(page,rows);
for (int i = 0; i < listUser.size(); i++) {
User user = listUser.get(i);// 分類信息
JSONObject jsonTemp = new JSONObject();
jsonTemp.put("userId", user.getUserId());
jsonTemp.put("userAccount", user.getUserAccount());
jsonTemp.put("userPassword", user.getUserPassword());
jsonTemp.put("userName", user.getUserName());
jsonTemp.put("userMobile", user.getUserMobile());
jsonTemp.put("userEmail", user.getUserEmail());
jsonTemp.put("userStatu", user.getUserStatu());
jsonTemp.put("groupId", user.getGroupId());
jsonTemp.put("eomsUserId", user.getEomsUserId());
jsonTemp.put("createTime", user.getCreateTime());
jsonTemp.put("companyId", user.getCompanyId());
listGrid.add(jsonTemp);
}
JSONObject jsonObject = new JSONObject();
jsonObject.put("page", page);
jsonObject.put("total", totalPage);
jsonObject.put("records", records);
jsonObject.put("dataRows", listGrid);
String jsonUsers = jsonObject.toJSONString();
System.out.print(jsonUsers);
return jsonUsers;
}
}
