现在树表格与表单编辑完全的兼容。你可以增加,编辑,删除一个节点。
如果一行被选中,每一个节点都会作为一个子节点被添加进去。
如果一行未被选中,那么添加的是一个跟节点。
HTML代码举例
<html> <head> <title>jqGrid 实例</title> </head> <body> ···代码省略··· <table id="addtree"></table> <div id="paddtree"></div> ···代码省略··· </body> </html>
javascript代码举例
$(function(){ pageInit(); }); function pageInit(){ jQuery("#addtree").jqGrid({ url: ctx+'/TreeGrid2', treedatatype: "xml", mtype: "POST", colNames:["id","Account","Acc Num", "Debit", "Credit","Balance"], colModel:[ {name:'id',index:'id', width:1,hidden:true,key:true, editable:true}, {name:'name',index:'name', width:180, editable:true}, {name:'num',index:'acc_num', width:80, align:"center",editable:true}, {name:'debit',index:'debit', width:80, align:"right",editable:true}, {name:'credit',index:'credit', width:80,align:"right",editable:true}, {name:'balance',index:'balance', width:80,align:"right",editable:true} ], height:'auto', pager : "#paddtree", treeGrid: true, ExpandColumn : 'name', editurl:'server.php?q=dummy', caption: "Add Tree node example" }); jQuery("#addtree").jqGrid('navGrid',"#paddtree"); }
java servlet代码举例
package net.mn886.blog.jqgrid.new_3_4; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class TreeGrid2 */ public class TreeGrid2 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public TreeGrid2() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String nodeid = request.getParameter("nodeid"); String xml = ""; if(nodeid ==null || "".equals(nodeid)){//一级数据 xml = "<?xml version='1.0' encoding='utf-8'?>" + " <rows>" + " <page>1</page>" + " <total>1</total>" + " <records>1</records>" + " <row>" + " <cell>1</cell>" + " <cell>Cash</cell>" + " <cell>100</cell>" + " <cell>400.00</cell>" + " <cell>250.00</cell>" + " <cell>150.00</cell>" + " <cell>0</cell>" + " <cell><![CDATA[NULL]]></cell>" + " <cell>false</cell>" + " <cell>false</cell>" + " </row>" + " <row>" + " <cell>5</cell>" + " <cell>Bank's</cell>" + " <cell>200</cell>" + " <cell>1500.00</cell>" + " <cell>1000.00</cell>" + " <cell>500.00</cell>" + " <cell>0</cell>" + " <cell><![CDATA[NULL]]></cell>" + " <cell>false</cell>" + " <cell>false</cell>" + " </row>" + " <row>" + " <cell>8</cell>" + " <cell>Fixed asset</cell>" + " <cell>300</cell>" + " <cell>0.00</cell>" + " <cell>1000.00</cell>" + " <cell>-1000.00</cell>" + " <cell>0</cell>" + " <cell><![CDATA[NULL]]></cell>" + " <cell>true</cell>" + " <cell>false</cell>" + " </row>" + " </rows>"; }else if("5".equals(nodeid)){//二级数据 xml = "<?xml version='1.0' encoding='utf-8'?>" + " <rows>" + " <page>1</page>" + " <total>1</total>" + " <records>1</records>" + " <row>" + " <cell>3</cell>" + " <cell>Sub Cash 1</cell>" + " <cell>1</cell>" + " <cell>300.00</cell>" + " <cell>200.00</cell>" + " <cell>100.00</cell>" + " <cell>1</cell>" + " <cell><![CDATA[2]]></cell>" + " <cell>true</cell>" + " <cell>false</cell>" + " </row>" + " </rows>"; } response.getWriter().write(xml); } }