基于EasyUI的软件框架打造-树(Tree)封装
2012-06-27 09:31
375 查看
软件产品,最常用的控件,树(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 onclick='' iconCls='icon-add'>添加</div><div onclick='' iconCls='icon-edit'>修改</div><div onclick='' 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);
}
}
客户端:
//树 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 onclick='' iconCls='icon-add'>添加</div><div onclick='' iconCls='icon-edit'>修改</div><div onclick='' 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);
}
}
相关文章推荐
- 基于EasyUI的软件框架打造-数据(JSON)封装
- 基于EasyUI的软件框架打造-多层
- 基于EasyUI的软件框架打造-数据交互
- 基于EasyUI的软件框架打造-界面操作
- 基于Retrofit、OkHttp、Gson封装通用网络框架
- EasyUI1.4.4 + zTree3.5.19 完美结合框架布局 tree+tab
- 基于Qt的软件框架设计
- 一个基于 EasyUI 的前台架构(3)封装操作Tabs的JS代码
- 基于MVC+EasyUI的Web开发框架经验总结(10)--在Web界面上实现数据的导入和导出
- 基于MVC+EasyUI的Web开发框架经验总结(17)--布局和对话框自动适应大小的处理
- 优雅设计封装基于Okhttp3的网络框架(三):多线程下载功能核心实现 及 线程池、队列机制、终止线程解析
- 基于Easyui的Tree控件做的表达式配置工具
- 教你打造基于贝壳内核的最简单一键备份还原软件(源代码)
- 基于框架开发的办公软件实例
- 重新封装BaseAdapter,打造最简洁框架
- 一个基于 EasyUI 的前台架构(4)主体页面框架收工
- 《开源框架那点事儿13》:基于开源框架做应用是未来中小型软件公司的发展趋势
- Netra(DM8168)基于Rdk平台的软件框架设计
- 我的基于asp.net mvc5 +mysql+dapper+easyui 的Web开发框架(1)数据库访问(0)
- 打造颠覆你想象中的高性能,轻量级的webform框架---js直接调后台的封装(第三天)