웹 개발

jqgrid에서 json, xml 사용법

노루아부지 2019. 8. 15. 11:53
$("#gridList").jqGrid({
	url: 'string.jsp',
	datatype: "json",
	height:400,    //높이
	//width:300,
	autowidth: true,  //자동 width 설정
	colNames:['ID', 'Name', 'Date'],
	colModel:[
		{name:'addin_id', index:'addin_id', width:30, align:"center", key:true, editable:false },
		{name:'addin_name', index:'addin_name', width:170, align:"center", editable:false},
		{name:'reg_date', index:'reg_date', width:100, align:"center", editable:false}
	],

	rowList:[10, 20, 30],     /** 페이지 보여주는 수 **/
	mtype:"POST",
	rownumbers:true,
	rownumWidth:40,
	gridview:true,
    
	toolbar: [false, "top"],     
	hiddengrid:false,
	imgpath:'style/grid',
	emptyrecords: "데이터가 존재하지 않습니다.",
	sortname:'addin_id',
	viewrecords: true,
	sortorder:"asc",
	multiselect:false,
	caption:"TEST",
	//loadonce:true,      /** 한번만 Loading 할 것인지에 대한 여부 **/
	rowNum:10,        /** 한 페이지에 보여주는 수 **/
	pager:jQuery('#gridpaper'),
	recordtext:"From {0} - {1} to {2}",
	emptyrecords: "No Records",
	loadtext:"Loading...",
	pgtext:"{0}",

 

이러한 옵션들에서 url과 datatype만 맞으면 최초 로딩시에 자동 매핑된다.

xml같은 경우는 jsp문서 상단에 contentType을 application/xml로 해주면 된다.

json은 string이나 jsonObject 형태로 리턴해주면 되는데,

반드시 total:   ,    page:,      records:    가 포함되어야 하며,

실제 데이터는 전체를 rows로 묶어 주고,

각 줄마다 cell 로 묶어 주어야 한다.

 

ex)

1. total, tabe, records

"\"total\":\""+ dataList.totalPage +"\", \"page\":\""+ (gotoPage <= 0 ? 1 : gotoPage) +"\", \"records\":\""+ dataList.totalCount +"\"";

 

2. 전체

rtnJson += ", \"rows\":[";

 

3. 각 줄

for (Hashtable<String, Object> ht : dataList){
	if (recCount != 0)
		rtnJson += ",";
      
	rtnJson += "{";
		rtnJson += "\"cell\":[";

			rtnJson += "\""+ ht.get("addin_id").toString() +"\",";
			rtnJson += "\""+ ht.get("addin_name").toString() +"\",";
			rtnJson += "\""+ ht.get("reg_date") +"\"";
      
		rtnJson += "]";
	rtnJson += "}";
      
	if (ht != null) ht = null;
      
	recCount += 1;
}
728x90
loading