剛接觸easyui,不太熟,根據datagrid的格式拼裝了數據,無法顯示到datagrid裡,但是在jsp裡面創建一個var變量就可以顯示,求大牛幫忙看下
JSP頁面代碼:
$(function() {
//alert(getParasFromUrl("selectType"));
//alert(getParasFromUrl("objectID"));
//初始化表格內容
$('#dataList').datagrid({
title : 'IP地址詳情',
//data:dataGrid,
method : 'post',
singleSelect : true,
height : 370,
fit : true,
fitColumns : true,
striped : true,
collapsible : true,
//url : "/jsp/jcss/IPQueryDetailAction.do?operation=queryIPFromDB",
url : "/InfoMgr/jsp/jcss/IPQueryDetailAction.do?operation=query&selectType="+getParasFromUrl("selectType") + "&objectID=" + getParasFromUrl("objectID"),
sortName : 'IP',
pageSize: 50, //每頁顯示的記錄條數,默認為10
pageList: [30, 50, 100, 200, 300], //分頁顯示選項
sortOrder : 'asc',
remoteSort : false,
idField : 'ID',
pagination : true, // 顯示分頁
rownumbers : true, // 顯示行號
columns : [ [
{field : 'ID', title : '序號', width : 20, sortable : true, halign : 'center'},
{field : 'IP', title : 'IP', width : 100, sortable : true, halign : 'center'},
{field : 'HOSTNAME', title : '主機名稱', width : 100, sortable : true, halign : 'center'},
{field : 'GATEWAY', title : '網關', width : 80, sortable : true, halign : 'center'},
{field : 'USEFOR', title : '用途', width : 120, sortable : true, halign : 'center'},
{field : 'PROPOSER', title : '申請人', width : 120, sortable : true, halign : 'center'},
{field : 'CREATEDATE', title : '申請時間', width : 120, sortable : true, halign : 'center'},
] ],
onLoadSuccess : function() {
//alert('OK');
//$('#dataList').datagrid('reload');
//$('#dataList').datagrid('clearSelections'); // 一定要加上這一句,要不然datagrid會記住之前的選擇狀態,刪除時會出問題
},
onDblClickRow: function (rowIndex, rowData) {
},
onClickRow: function (rowIndex, rowData) {
},
});
});
後台數據拼裝:
public List getIPSegFromDB(String ID){
System.out.println("call getIPSegFromDB");
List<String> lstIPs = new ArrayList<String>();
StringBuffer sb = new StringBuffer();
sb.append("{\"total\":28,\"rows\":[");
sb.append("{\"ID\":\"1\",\"IP\":\"192.168.1.1\",\"HOSTNAME\":\"test\",\"GATEWAY\":\"192.168.1.254\",\"USEFOR\":\"測試\",\"PROPOSER\":\"11\",\"CREATEDATE\":\"2015-11-21\"},");
sb.append("{\"ID\":\"2\",\"IP\":\"192.168.1.2\",\"HOSTNAME\":\"test\",\"GATEWAY\":\"192.168.1.254\",\"USEFOR\":\"測試\",\"PROPOSER\":\"111\",\"CREATEDATE\":\"2015-11-21\"}");
sb.append("]}");
lstIPs.add(sb.toString());
return lstIPs;
}
返回json:
response.getWriter().print(JSONArray.fromObject(qis.getIPSegFromDB(objectID)).toString());
鼓搗一晚上了沒查出問題來,但是按照下面的格式寫到jsp頁面裡,就能顯示出來。。。。求解啊。。。
var dataGrid = {"total":28,"rows":[
{"ID":"1","IP":"192.168.1.1","HOSTNAME":"test","GATEWAY":"192.168.1.254","USEFOR":"測試","PROPOSER":"111","CREATEDATE":"2015-11-21"},{"ID":"2","IP":"192.168.1.2","HOSTNAME":"test","GATEWAY":"192.168.1.254","USEFOR":"測試","PROPOSER":"111","CREATEDATE":"2015-11-21"},
{"ID":"1","IP":"192.168.1.1","HOSTNAME":"test","GATEWAY":"192.168.1.254","USEFOR":"測試","PROPOSER":"111","CREATEDATE":"2015-11-21"},{"ID":"2","IP":"192.168.1.2","HOSTNAME":"test","GATEWAY":"192.168.1.254","USEFOR":"測試","PROPOSER":"111","CREATEDATE":"2015-11-21"}
]};
終於解決了,後台print的方法寫的有問題,應該用JSONObject.fromObject(String),不能用JSONArray.fromObject(String),關閉問題