我正在使用 JQGRID 以表格格式显示数据。我已经成功地从 servlet 访问了 jqgrid 中的数据。我使用 XML 作为数据类型。
我的问题是我应该如何将列名作为数组传递?到目前为止,我可以通过手动从 javascript 中为列命名来在 jqgrid 中进行 diaplya 记录。但我想从数据库中获取列名并将其作为数组传递给 jqgrid。
以下是我的代码片段:
<script language="javascript">
function fillGridOnEvent()
{
$("#jQGrid").html("<table id=\"list\"></table><div id=\"page\"></div>");
jQuery("#list").jqGrid({
url:'<%=request.getContextPath()%>/JQGridServlet?q=1&action=fetchData',
datatype: "xml",
height: 250,
colNames:['Sr. No.','Student Name','Student Std.','Student RollNo.',"Action"],
colModel:[{name:'srNo',index:'srNo', width:90,sortable:true},
{name:'stdName',index:'stdName', width:130,sortable:false},
{name:'stdStd',index:'stdStd', width:100,sortable:false},
{name:'stdRollNo',index:'stdRollNo', width:180,sortable:false},
{name:'view',index:'view', width:100,sortable:false}
],
multiselect: false,
paging: true,
rowNum:10,
rowList:[10,20,30],
pager: $("#page"),
loadonce:true,
caption: "Table1"
}).navGrid('#page',{edit:false,add:false,del:false});
}
jQuery().ready(function (){
//fillGrid();
});
</script>
小服务程序:
protected void showData(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
PrintWriter out = response.getWriter();
response.setContentType("text/xml;charset=UTF-8");
String rows = request.getParameter("rows");
int totalPages = 0;
int totalCount = 15;
if (totalCount > 0)
{
if (totalCount % Integer.parseInt(rows) == 0)
{
totalPages = totalCount / Integer.parseInt(rows);
}
else
{
totalPages = (totalCount / Integer.parseInt(rows)) + 1;
}
}
else
{
totalPages = 0;
}
out.print("<?xml version='1.0' encoding='utf-8'?>\n");
out.print("<rows>");
out.print("<page>" + request.getParameter("page") + "</page>");
out.print("<total>" + totalPages + "</total>");
out.print("<records>" + 15 + "</records>");
int srNo = 1;
for (int i=0;i<15;i++)
{
out.print("<row id='" + i + "'>");
out.print("<cell>" + srNo + "</cell>");
out.print("<cell>ABCDEFG</cell>");
out.print("<cell>ABC</cell>");
out.print("<cell>125</cell>");
out.print("</row>");
srNo++;
}
out.print("</rows>");
}
在上面的 servlet 中,我可以发送数据,但现在我也想从 servlet 中设置列名。有什么想法吗?