软件产品,最常用的控件,树(Tree)封装对使用更方便:
客户端:
// 树 id:控件ID show:是否能维护树:true不能;false能;fun(node, checked) _this.UI_Tree = function (uid, checkbox, isShow, fun) { if (isShow) { // 显示非维护模式 var _tree = $("#" + uid).tree({ method: "get", animate: true, checkbox: checkbox, url: path + "exec.ashx?m=TreeBuild" + "&p={uid:" + escape(uid) + "'}" }); } else { // 维护模式 $("<div id='" + uid + "Menu' class='easyui-menu' style='width:120px;'><div οnclick='' iconCls='icon-add'>添加</div><div οnclick='' iconCls='icon-edit'>修改</div><div οnclick='' iconCls='icon-remove'>删除</div></div>").appendTo("body"); $("#" + uid + "Menu").menu({ onClick: function (item) { var node = $("#" + uid).tree("getSelected"); if (node != null) { if (item.text == '添加') { _this.exec("TreeAdd", { uid: uid, pid: node.id,text:"新记录" }, {}, null); } if (item.text == '修改') { $("#" + uid).tree("beginEdit", node.target); } if (item.text == '删除') { _this.exec("TreeDelete", { uid: uid, id: node.id }, {}, null); } } else alert('请高亮选择一项'); } }); var _tree = $("#" + uid).tree({ method: "get", animate: true, dnd: true, checkbox: checkbox, url: path + "exec.ashx?m=TreeBuild" + "&p={uid:" + escape(uid) + "'}", onCheck: function (node, checked) { if (fun) fun(node, checked); }, onSelect: function (node) { if (fun) fun(node, true); }, onDblClick: function (node) { $("#" + uid).tree("beginEdit", node.target); }, onBeforeEdit: function (node) { if (node.id == 0) return false; }, onAfterEdit: function (node) { _this.exec("TreeEdit", { uid: uid, id: node.id, newText: node.text }, {}, null); }, onDrop: function (target, source, point) { _this.exec("TreeMove", { uid: uid, target: $("#" + uid).tree('getNode', target).id, source: source.id, point: point }, {}, null); }, onContextMenu: function (e, node) { $("#" + uid + "Menu").menu("show", { left: e.pageX, top: e.pageY }); } }); } }
服务器端:
public class TreeNode { public string id; // 节点ID,这是很重要的加载远程数据 public string text; // 文字显示节点 public bool state = false; // 节点的状态,“开放open”或“关闭closed”,默认为“打开”。 当设置为“关闭”,该节点有子节点,并将它们远程加载 public bool isCheck = false; // 指示节点是否被选中选中 public NameValueCollection attributes; // 自定义属性可以被添加到一个节点 public ArrayList children; // 一个数组节点有一些子节点 public TreeNode( string _id, string _text) { children = new ArrayList(); attributes = new NameValueCollection(); id = _id; text = _text; } public void Add(TreeNode tn) { children.Add(tn); } public JsonObject Json { get { JsonArray ja_children = new JsonArray(); if (children.Count > 0) { for ( int i = 0; i < children.Count; i++) { ja_children.Add((children[i] as TreeNode).Json); } } string[] _name = new string[attributes.Count]; object[] _value = new object[attributes.Count]; for ( int i = 0; i < attributes.Count; i++) { _name[i] = attributes.GetKey(i); _value[i] = attributes[i]; } JsonObject jo_attributes = new JsonObject(_name, _value); string[] name = new string[] { " id ", " text ", " state ", " isCheck ", " attributes ", " children " }; object[] value = new object[] { id, text, state ? " closed " : " open ", isCheck, jo_attributes, ja_children }; return ( new JsonObject(name, value)); } } public string toJson() { return JsonConvert.ExportToString(Json); } } public class Tree { public string idField = " ID "; public string textField = " 名称 "; public string parentField = " ParentID "; public ArrayList nodes; public Tree() { nodes = new ArrayList(); } public Tree( string ParentID,DataTable dt) { nodes = new ArrayList(); DataRow[] drs = dt.Select(parentField + " = " + ParentID); if (ParentID == " 0 ") { TreeNode allNode = new TreeNode( " 0 ", " 所有分类 "); if (drs.Length > 0) allNode.state = false; nodes.Add(allNode); foreach (DataRow dr in drs) { TreeNode node = new TreeNode(dr[idField].ToString(), dr[textField].ToString()); node.state = dt.Select(parentField + " = " + dr[idField].ToString()).Length> 0; allNode.Add(node); } } else { foreach (DataRow dr in drs) { TreeNode node = new TreeNode(dr[idField].ToString(), dr[textField].ToString()); node.state = dt.Select(parentField + " = " + dr[idField].ToString()).Length > 0; nodes.Add(node); } } } public void Add(TreeNode tn) { nodes.Add(tn); } public string toJson() { JsonArray ja = new JsonArray(); if (nodes.Count > 0) { for ( int i = 0; i < nodes.Count; i++) { ja.Add((nodes[i] as TreeNode).Json); } } return JsonConvert.ExportToString(ja); } }