天天看點

坑之easyui資料表格(daagird)rows參數

rows是datagird的固定參數

看下圖:

坑之easyui資料表格(daagird)rows參數

由于我沒有使用分頁,是以

total

NaN

這裡采用的原生

Servlet

+

JDBC

完成的宿舍管理系統中的其中一小點功能。

下面開始入坑:

1. 前端資料表格是這樣的:

<script type="text/javascript">
	$(function() {
		$('#dg').datagrid({
			title : "宿舍成員資訊",
			iconCls : "icon-more",
			rownumbers : "true",
			idField : 'sid',
			loadMsg : '資料正在加載,小夥伴耐心等待哦!!',
			striped : "true",
			fitColumns : "true",
			url : "getStudentByHid",
			columns:[
				[
					{field:'sid', title:'學号', width:'80', resizable:'false'},
					{field:'sname', title:'姓名', width:'80', resizable:'false'},
					{field:'scollege', title:'所在學院', width:'80', resizable:'false'},
					{field:'hid', title:'寝室編号', width:'80', resizable:'false'},
					{field:'ssex', title:'性别', width:'80', resizable:'false'},
					{field:'sage', title:'年齡', width:'80', resizable:'false'},
					{field:'saddress', title:'家庭位址', width:'180', resizable:'false'},
					{field:'sphone', title:'聯系電話', width:'140', resizable:'false'}
				]
			],
			onLoadSuccess : function(data) {
				console.log(data);
			}
		});
	})
</script>
<table id="dg" class="easyui-datagrid" cellspacing="0" cellpadding="0"></table>
           

這裡沒什麼,注意一點L就是

field

字段不要寫錯了。

這裡列印的

data

是這樣的:

坑之easyui資料表格(daagird)rows參數

因為除了管理者就兩個同學在一起住。

2. Controller層

StudentSelectService sss=new StudentSelectService();
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	resp.setCharacterEncoding("utf8");
	HttpSession session=req.getSession();		
	UserModel user=(UserModel)session.getAttribute("usermodel");
	int hid = user.getHid();
	JSONObject stuList = sss.getStuByHid(hid);
	PrintWriter pw=resp.getWriter();
	pw.write(stuList.toString());
}
           

解釋:

  第一句設定字元編碼。

  第二句建構Session對象

  第三句擷取session中的登入使用者。

  第四句擷取學生的宿舍号。

  第五句通過宿舍号擷取學生集合并轉為JSON對象

  然後寫到前端。

這裡背景列印的

stuList

資料是這樣的:

{
	"rows":[
		{
			"hid":11001,
			"saddress":"湖南",
			"sage":21,
			"scollege":"資訊工程學院",
			"sid":1000,
			"sname":"豬八戒",
			"sphone":"15588887777",
			"spower":0,
			"spwt":"",
			"ssex":"女"
		},{
			"hid":11001,
			"saddress":"上海",
			"sage":22,
			"scollege":"資訊工程學院",
			"sid":1005,
			"sname":"唐僧",
			"sphone":"18811114444",
			"spower":0,
			"spwt":"",
			"ssex":"男"
		}
	]
}
           

因為

"spower"

"spwt"

兩個字段沒有查。是以沒有資料。

3. Service層

StudentSelectDao sd = new StudentSelectDao();
public JSONObject getStuByHid(int hid) {
	JSONObject jObject = new JSONObject();
	jObject.put("rows", sd.getStuByHid(hid));
	return jObject;
}
           

解釋:

  通過hid查詢到一個學生集合放到JSONObject中傳回。

  就這裡。

rows

我剛開始寫的是

studentList

,導緻

datagird

擷取不到資料。

  報錯:

Unable to get property 'length' of undefined or null reference
           

4. Dao層也貼上吧。反正也沒用

public List<UserModel> getStuByHid(int hid) {
	List<UserModel> list = new ArrayList<>();
	String sql="select sid,sname,scollege,ssex,sage,hid,sphone,saddress from user where spower=1 and hid=?";
	Connection con=null;
	try {
		con=JdbcUtil.getDBConnection();
		PreparedStatement pst = con.prepareStatement(sql);
		pst.setInt(1, hid);
		ResultSet rs = pst.executeQuery();
		while (rs.next()) {
			UserModel user=new UserModel(rs.getInt("sid"),rs.getString("sname"),rs.getString("scollege"),rs.getString("ssex"),rs.getInt("sage"),rs.getInt("hid"),rs.getString("sphone"),rs.getString("saddress"));
			list.add(user);
		}
	} catch (SQLException e) {
		e.printStackTrace();
	}finally {
		JdbcUtil.closedConnection(con);
	}
	return list;
}
           

沒了。

繼續閱讀