coolite treepanel增删改并自动刷新
刚开始用coolite发现虽然比较强大,但太不熟悉里面东西了,还是应该仔细研究demo,边用边学实在费力
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<ext:Hidden ID="SaveAction" runat="server" />
<ext:Hidden ID="Save_NodeParent" runat="server" />
<ext:Window ID="WinCategoryAction" Resizable="false" Maximizable="false" ShowOnLoad="false"
BodyStyle="padding:0px" runat="server" BodyBorder="false" Collapsible="false"
Icon="Application" Frame="false" Width="350" AutoHeight="true" Title="节点管理">
<LoadMask ShowMask="true" />
<Body>
<ext:FormPanel ID="FormPanelCategory" runat="server" BodyStyle="padding:1px;" ButtonAlign="Center"
Frame="true" BodyBorder="false" MonitorPoll="500" MonitorValid="true" Header="false">
<Defaults>
<ext:Parameter Name="AllowBlank" Value="false" Mode="Raw" />
<ext:Parameter Name="MsgTarget" Value="side" />
</Defaults>
<Listeners>
<ClientValidation Handler="#{btnRefreshCategory}.setDisabled(!valid);" />
</Listeners>
<Body>
<ext:FormLayout ID="FormLayout1" runat="server" LabelAlign="Left" LabelWidth="60">
<ext:Anchor Horizontal="92%">
<ext:TextField ID="txf_TreeName" Width="250" FieldLabel="节点名称" BlankText="节点名称不能为空!"
AllowBlank="false" runat="server" />
</ext:Anchor>
<ext:Anchor Horizontal="92%">
<ext:ComboBox ID="cb_Type" Editable="false" EmptyText="选取部门信息..." EnableShadow="true"
FieldLabel="选取部门信息" Mode="Local" LoadingText="部门信息正在加载..." runat="server">
</ext:ComboBox>
</ext:Anchor>
</ext:FormLayout>
</Body>
<Buttons>
<ext:Button ID="btnRefreshCategory" runat="server" Icon="Add" Text="更新">
<Listeners>
<Click Handler="if(#{FormPanelCategory}.getForm().isValid()){return true;}else{#{txf_TreeName}.focus(true); return false;}" />
</Listeners>
<AjaxEvents>
<Click OnEvent="btn_upData_Ajax">
<EventMask CustomTarget="WinCategoryAction" Target="CustomTarget" ShowMask="true" MinDelay="20" />
</Click>
</AjaxEvents>
</ext:Button>
<ext:Button ID="btnCancleCategory" runat="server" Icon="Cancel" Text="取消">
<Listeners>
<Click Handler="#{FormPanelCategory}.getForm().reset();#{WinCategoryAction}.hide();" />
</Listeners>
</ext:Button>
</Buttons>
</ext:FormPanel>
</Body>
</ext:Window>
<ext:Panel ID="TreePanel" AutoScroll="true" runat="server" Title="公司部门" AutoWidth="true">
<Body>
<ext:TreePanel ContextMenuID="MenuDepartment" Icon="Anchor" ContainerScroll="true"
Border="false" ID="TypePanel" runat="server" AutoHeight="true">
<TopBar>
<ext:Toolbar ID="Toolbar2" runat="server">
<Items>
<ext:Button ID="Button1" runat="server" Text="刷新" Icon="ArrowRefresh">
<Listeners>
<Click Handler="refreshTree(#{TypePanel});" />
</Listeners>
</ext:Button>
<ext:Button ID="btnExpandAll" runat="server" Text="全部展开" Icon="ArrowOut">
<Listeners>
<Click Handler="#{TypePanel}.expandAll();" />
</Listeners>
</ext:Button>
<ext:Button ID="btnCollapseAll" runat="server" Text="全部折叠" Icon="ArrowIn">
<Listeners>
<Click Handler="#{TypePanel}.collapseAll();" />
</Listeners>
</ext:Button>
</Items>
</ext:Toolbar>
</TopBar>
<Listeners>
<ContextMenu Handler="#{MenuDepartment}.node=node;#{MenuDepartment}.showAt(e.getPoint());" />
<Click Handler="if(node.firstChild == null){e.stopEvent();};" />
</Listeners>
</ext:TreePanel>
</Body>
</ext:Panel>
<ext:Menu ID="MenuDepartment" runat="server" AllowOtherMenus="false">
<Items>
<ext:MenuItem ID="MI_Add" Icon="Add" runat="server" Text="添加子节点">
<Listeners>
<Click Handler="#{FormPanelCategory}.getForm().reset();Coolite.AjaxMethods.ActionDo(#{MenuDepartment}.node.id,'add');" />
</Listeners>
</ext:MenuItem>
<ext:MenuItem ID="MI_Update" Icon="Anchor" runat="server" Text="修改该节点">
<Listeners>
<Click Handler="Coolite.AjaxMethods.ActionDo(#{MenuDepartment}.node.id,'edit');" />
</Listeners>
</ext:MenuItem>
<ext:MenuItem ID="MI_Delete" Icon="Delete" runat="server" Text="删除该节点">
<Listeners>
<Click Handler="Coolite.AjaxMethods.Delete(#{MenuDepartment}.node.id,'del');" />
</Listeners>
</ext:MenuItem>
</Items>
</ext:Menu>
C#
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack && !Ext.IsAjaxRequest)
{
NodeLoad(TypePanel.Root);
}
if (!Ext.IsAjaxRequest)
{
BindType();
}
}
private void BindType()
{
DepartmentList list = DepartmentManager.GetDepartments(100, 1, new QwpDataParameter[] { });
for (int i = 0; i < list.RecordCount; i++)
{
Coolite.Ext.Web.ListItem item = new Coolite.Ext.Web.ListItem(list[i].DepartmentName, list[i].ID.ToString());
cb_Type.Items.Add(item);
}
cb_Type.Items.Insert(0, new Coolite.Ext.Web.ListItem("所有部门", "0"));
}
[AjaxMethod]
public string RefreshMenu()
{
Coolite.Ext.Web.TreeNodeCollection nodes = this.NodeLoad(null);
return nodes.ToJson();
}
public Coolite.Ext.Web.TreeNodeCollection NodeLoad(Coolite.Ext.Web.TreeNodeCollection nodes)
{
if(nodes==null)
nodes = new Coolite.Ext.Web.TreeNodeCollection();
DepartmentList Dlist = DepartmentManager.GetDepartments(100, 1, new QwpDataParameter[] { });
Coolite.Ext.Web.TreeNode root = root = new Coolite.Ext.Web.TreeNode("0", "所有部门", Icon.Folder);//新建根节点
if (Dlist!=null)
{
AddChild(root);
}
nodes.Add(root);
return nodes;
}
/// <summary>
/// 递归添加子节点
/// </summary>
/// <param name="root"></param>
private void AddChild(Coolite.Ext.Web.TreeNode root)
{
List<Coolite.Ext.Web.TreeNode> list = GetChilds(root);
if (list.Count == 0)
{
return;
}
else
{
for (int i = 0; i < list.Count; i++)
{
root.Nodes.Add(list[i]);
}
for (int i = 0; i < list.Count; i++)
{
AddChild(list[i]);
}
}
}
/// <summary>
/// 得到父节点
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
private List<Coolite.Ext.Web.TreeNode> GetChilds(Coolite.Ext.Web.TreeNode node)
{
DepartmentList list = DepartmentManager.GetDepartments(100, 1, new QwpDataParameter[] { new QwpDataParameter("ParentID", DbType.Int16, node.NodeID) });
List<Coolite.Ext.Web.TreeNode> tn = new List<Coolite.Ext.Web.TreeNode>();
if (list != null)
{
for (int i = 0; i < list.Count; i++)
{
Coolite.Ext.Web.TreeNode n = new Coolite.Ext.Web.TreeNode();
n.Text = list[i].DepartmentName;
n.NodeID = list[i].ID.ToString();
n.Qtip = string.Format("右键管理{0}", list[i].DepartmentName);
//List<DT.Model.Type> nl = DT.BLL.ShowView.NewsType.SelAllNewsTypeByFID(n.NodeID);
DepartmentList nl = DepartmentManager.GetDepartments(100, 1, new QwpDataParameter[] { new QwpDataParameter("ParentID", DbType.Int16, n.NodeID) });
if (nl == null)
{
n.Qtip = string.Format("点击管理{0}下的用户", list[i].DepartmentName);
}
tn.Add(n);
}
}
return tn;
}
[AjaxMethod]
public void ActionDo(string nodeid, string action)
{
SaveAction.Text = nodeid + "," + action;
Department depart = DepartmentManager.GetDepartment(int.Parse(nodeid));
switch (action)
{
case "add":
cb_Type.HideMode = HideMode.Display;
cb_Type.Hidden = true;
WinCategoryAction.Icon = Icon.ApplicationFormAdd;
//if (depart != null)
//{
// WinCategoryAction.SetTitle(string.Format("添加{0}的子节点", depart.DepartmentName));
//}
//else
//{
// WinCategoryAction.SetTitle(string.Format("添加{0}的子节点", "所有部门"));
//}
if (depart != null && depart.ParentID != 0)
{
Department departParent = DepartmentManager.GetDepartment(depart.ParentID);
WinCategoryAction.SetTitle(string.Format("添加{0}的子节点{1}", departParent.DepartmentName, depart.DepartmentName));
cb_Type.SetRawValue(departParent.DepartmentName);
}
else
{
WinCategoryAction.SetTitle(string.Format("添加{0}的子节点{1}", "所有部门", depart.DepartmentName));
cb_Type.SelectedItem.Value="0";
cb_Type.SetRawValue("所有部门");
//cb_Type.SelectedIndex = 0;
}
//cb_Type.SetRawValue(depart.DepartmentName);
WinCategoryAction.Show();
break;
case "edit":
cb_Type.HideMode = HideMode.Display;
cb_Type.Hidden = true;
WinCategoryAction.Icon = Icon.ApplicationFormAdd;
txf_TreeName.Text = depart.DepartmentName;
if (depart != null && depart.ParentID != 0)
{
Department departParent = DepartmentManager.GetDepartment(depart.ParentID);
WinCategoryAction.SetTitle(string.Format("编辑{0}的子节点{1}", departParent.DepartmentName, depart.DepartmentName));
cb_Type.SetRawValue(departParent.DepartmentName);
}
else
{
WinCategoryAction.SetTitle(string.Format("编辑{0}的子节点{1}", "所有部门",depart.DepartmentName));
cb_Type.SelectedItem.Value="0";
cb_Type.SetRawValue("所有部门");
//cb_Type.SelectedIndex = 0;
}
WinCategoryAction.Show();
break;
case null:
Ext.Msg.Alert(ConfigurationManager.AppSettings["MsgAlert"], ConfigurationManager.AppSettings["AlertMessage"]).Show();
break;
}
}
[AjaxMethod]
public void Delete(string nodeid, string action)
{
switch (action)
{
case "del":
Ext.Msg.Confirm(ConfigurationManager.AppSettings["MsgTitle"], "<span style=\"color:#F00;\">此操作将会删除该节点类型下所有的数据!</span><br />是否确定删除?", new MessageBox.ButtonsConfig
{
Yes = new MessageBox.ButtonConfig
{
Handler = "Coolite.AjaxMethods.TreeNote_Del(" + nodeid + ");",
Text = "确 定"
},
No = new MessageBox.ButtonConfig
{
Text = "取 消"
}
}).Show();
break;
}
}
[AjaxMethod]
public void TreeNote_Del(string treeNote)
{
if (DepartmentManager.DeleteDepartments(int.Parse(treeNote)))
{
Ext.Msg.Alert(ConfigurationManager.AppSettings["MsgTitle"], "数据已成功删除!", new MessageBox.ButtonsConfig
{
Yes = new MessageBox.ButtonConfig
{
Handler = "refreshTree(" + TypePanel.ClientID + ");",
Text = "确 定"
}
}).Show();
}
else
{
Ext.Msg.Alert(ConfigurationManager.AppSettings["MsgError"], ConfigurationManager.AppSettings["ErrorMessage"]).Show();
}
}
/// <summary>
/// 更新
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btn_upData_Ajax(object sender, AjaxEventArgs e)
{
Department depart = new Department();
string action = SaveAction.Text.Split(new char[] { ',' })[1];
switch (action)
{
case "add":
depart.ParentID = Convert.ToInt32(SaveAction.Text.Split(new char[] { ',' })[0]);
//depart.TypeID = 0;
break;
case "edit":
depart.ID = Convert.ToInt32(SaveAction.Text.Split(new char[] { ',' })[0]);
if (Save_NodeParent.Text == "0")
{
depart.ParentID = 0;
}
else
{
depart.ParentID = Convert.ToInt32(cb_Type.SelectedItem.Value);
}
break;
default:
break;
}
depart.DepartmentName = txf_TreeName.Text;
depart.ApplicationID = WebUtils.GetApplicationId();
if (depart.Save())
{
Ext.Msg.Alert(ConfigurationManager.AppSettings["MsgTitle"], "数据已经提交成功!~", new MessageBox.ButtonsConfig
{
Yes = new MessageBox.ButtonConfig
{
Handler = "refreshTree(" + TypePanel.ClientID + ");",
Text = "确 定"
}
}).Show();
WinCategoryAction.Hidden = true;
//QwpDataCache.RemoveByKeyPrefix
}
else
{
Ext.Msg.Alert(ConfigurationManager.AppSettings["MsgError"], ConfigurationManager.AppSettings["ErrorMessage"]).Show();
}
//TypePanel.to
}