PDA

View Full Version : [CLOSED] TreePanel selection problem



metci
May 29, 2014, 12:24 PM
Hi,

I've added a tree panel to a page of which nodes will be loaded dynamically.

Tree panel has two levels of nodes, when the user click parent nodes, related leaf nodes should be selected also. It seems everything is working fine on browser but when I try to get the checked nodes in code behind, leaf nodes that are checked automatically are unchecked.

I've uploaded necessary files to create case.

Any help would be much appreciated.
Thanks.

Daniil
May 29, 2014, 2:40 PM
Hi @metci,

Welcome to the Ext.NET forums!

Please post the code directly here wrapping in [CODE] tags.

Also, please put the code behind directly on .aspx page. Please see Example (Very Helpful) here:
Forum Guidelines For Posting New Topics (http://forums.ext.net/showthread.php?3440)

metci
May 30, 2014, 5:31 AM
Hi Daniil, here is the code. I'm using ext.net version 2.4.



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TreePanelProblem.aspx.cs"
Inherits="TreePanelProblem" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<script runat="server">
public class BSMenuItem
{
long oid;

public long Oid
{
get { return oid; }
set { oid = value; }
}

long parentMenuOid;

public long ParentMenuOid
{
get { return parentMenuOid; }
set { parentMenuOid = value; }
}

string menuCode;

public string MenuCode
{
get { return menuCode; }
set { menuCode = value; }
}

string menuName;

public string MenuName
{
get { return menuName; }
set { menuName = value; }
}

bool isActive;

public bool IsActive
{
get { return isActive; }
set { isActive = value; }
}
}

public class BSMenuAuthorization
{
long oid;

public long Oid
{
get { return oid; }
set { oid = value; }
}

BSMenuItem menuItem;

public BSMenuItem MenuItem
{
get { return menuItem; }
set { menuItem = value; }
}

long userOid;

public long UserOid
{
get { return userOid; }
set { userOid = value; }
}

bool isUserAuthorized;

public bool IsUserAuthorized
{
get { return isUserAuthorized; }
set { isUserAuthorized = value; }
}
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadNodes(0);
}
}

public void LoadNodes(long userOid)
{
BSMenuAuthorization[] menuAuthorizationList = GetMenuAuthorizationList(userOid);

var parentMenus = from menuAuthorization in menuAuthorizationList
where menuAuthorization.MenuItem.ParentMenuOid == 0
select menuAuthorization;

BSMenuAuthorization[] parentMenuAuthorizationList = parentMenus.ToArray();

NodeCollection nodes = new NodeCollection();
foreach (BSMenuAuthorization menuAuthorization in parentMenuAuthorizationList)
{
Node parentNode = new Node();
parentNode.Text = menuAuthorization.MenuItem.MenuName;
parentNode.NodeID = menuAuthorization.MenuItem.MenuCode;
parentNode.Checked = false;
tpMenuAuthorization.Root[0].Children.Add(parentNode);
AddChildNodes(parentNode, menuAuthorization.MenuItem.Oid, menuAuthorizationList);
parentNode.Expandable = (parentNode.Children.Count > 0);
if (parentNode.Expandable)
{
parentNode.Expanded = true;
}
}
tpMenuAuthorization.Reload();
}

private BSMenuAuthorization[] GetMenuAuthorizationList(long userOid)
{
BSMenuItem menuItem1 = new BSMenuItem();
menuItem1.IsActive = true;
menuItem1.MenuCode = "1";
menuItem1.MenuName = "Parent Menu1";
menuItem1.Oid = 1;
menuItem1.ParentMenuOid = 0;

BSMenuItem menuItem2 = new BSMenuItem();
menuItem2.IsActive = true;
menuItem2.MenuCode = "2";
menuItem2.MenuName = "Parent Menu2";
menuItem2.Oid = 2;
menuItem2.ParentMenuOid = 0;

BSMenuItem menuItem3 = new BSMenuItem();
menuItem3.IsActive = true;
menuItem3.MenuCode = "3";
menuItem3.MenuName = "Menu1";
menuItem3.Oid = 3;
menuItem3.ParentMenuOid = 1;

BSMenuItem menuItem4 = new BSMenuItem();
menuItem4.IsActive = true;
menuItem4.MenuCode = "4";
menuItem4.MenuName = "Menu2";
menuItem4.Oid = 4;
menuItem4.ParentMenuOid = 1;

BSMenuItem menuItem5 = new BSMenuItem();
menuItem5.IsActive = true;
menuItem5.MenuCode = "5";
menuItem5.MenuName = "Menu3";
menuItem5.Oid = 5;
menuItem5.ParentMenuOid = 2;

BSMenuAuthorization menuAuth1 = new BSMenuAuthorization();
menuAuth1.Oid = 6;
menuAuth1.MenuItem = menuItem1;
menuAuth1.IsUserAuthorized = false;

BSMenuAuthorization menuAuth2 = new BSMenuAuthorization();
menuAuth2.Oid = 7;
menuAuth2.MenuItem = menuItem2;
menuAuth2.IsUserAuthorized = false;

BSMenuAuthorization menuAuth3 = new BSMenuAuthorization();
menuAuth3.Oid = 8;
menuAuth3.MenuItem = menuItem3;
menuAuth3.IsUserAuthorized = false;

BSMenuAuthorization menuAuth4 = new BSMenuAuthorization();
menuAuth4.Oid = 9;
menuAuth4.MenuItem = menuItem4;
menuAuth4.IsUserAuthorized = false;

BSMenuAuthorization menuAuth5 = new BSMenuAuthorization();
menuAuth5.Oid = 10;
menuAuth5.MenuItem = menuItem5;
menuAuth5.IsUserAuthorized = true;

BSMenuAuthorization[] menuAuthList = new BSMenuAuthorization[5];
menuAuthList[0] = menuAuth1;
menuAuthList[1] = menuAuth2;
menuAuthList[2] = menuAuth3;
menuAuthList[3] = menuAuth4;
menuAuthList[4] = menuAuth5;

return menuAuthList;
}

private void AddChildNodes(Node parentNode, long parentMenuOid, BSMenuAuthorization[] menuAuthorizationList)
{
var childMenus = from menuAuthorization in menuAuthorizationList
where menuAuthorization.MenuItem.ParentMenuOid == parentMenuOid
select menuAuthorization;

BSMenuAuthorization[] childMenuAuthorizationList = childMenus.ToArray();

foreach (BSMenuAuthorization menuAuthorization in childMenuAuthorizationList)
{
Node childNode = new Node();
childNode.Text = menuAuthorization.MenuItem.MenuName;
childNode.NodeID = menuAuthorization.MenuItem.MenuCode;
childNode.Leaf = true;
childNode.Checked = menuAuthorization.IsUserAuthorized;
parentNode.Children.Add(childNode);
AddChildNodes(childNode, menuAuthorization.MenuItem.Oid, childMenuAuthorizationList);
childNode.Expandable = (childNode.Children.Count > 0);
if (childNode.Expandable)
{
childNode.Expanded = true;
}
}
}

protected void UpdateAuthorization(object sender, DirectEventArgs e)
{
long oid = 0;
string selectedMenuString = "";
BSMenuAuthorization[] menuAuthorizationList = GetMenuAuthorizationList(oid);

foreach (SubmittedNode node in tpMenuAuthorization.CheckedNodes)
{
BSMenuAuthorization menuAuthorization = (from _menuAuthorization in menuAuthorizationList
where _menuAuthorization.MenuItem.MenuCode == node.NodeID
select _menuAuthorization).FirstOrDefault();

selectedMenuString += menuAuthorization.MenuItem.MenuName + ",";
}
X.Msg.Show(new MessageBoxConfig
{
Title = "Selected Menu List",
Message = selectedMenuString,
Buttons = MessageBox.Button.OK,
Icon = MessageBox.Icon.ERROR
});

}

private void SetAuthorization(Node parentNode, BSMenuAuthorization[] menuAuthorizationList)
{
NodeCollection nodeCollection = parentNode.Children;
foreach (Node node in nodeCollection)
{
if (node.Leaf)
{
BSMenuAuthorization menuAuthorization = (from _menuAuthorization in menuAuthorizationList
where _menuAuthorization.MenuItem.MenuCode == node.NodeID
select _menuAuthorization).FirstOrDefault();

if (menuAuthorization != null)
{
menuAuthorization.IsUserAuthorized = node.Checked.GetValueOrDefault();
}
}
SetAuthorization(node, menuAuthorizationList);
}
}

protected void SelectDeselectChildMenus(object sender, DirectEventArgs e)
{
bool nodeFound = false;
List<SubmittedNode> submittedNodeList = RowSelectionModel2.SelectedNodes;
if (submittedNodeList != null && submittedNodeList.Count > 0)
{
List<SubmittedNode> treeWidesubmittedNodeList = tpMenuAuthorization.CheckedNodes;
if (treeWidesubmittedNodeList != null)
{
foreach (SubmittedNode node in treeWidesubmittedNodeList)
{
if (node.NodeID.Equals(submittedNodeList[0].NodeID))
{
tpMenuAuthorization.GetNodeById(submittedNodeList[0].NodeID).ChildNodes().SetChecked(node.Checked);
nodeFound = true;
break;
}
}
}

if (!nodeFound)
{
submittedNodeList[0].ToProxyNode().ChildNodes().SetChecked(false);
}
}
}
</script>
<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
<title>Men? Yetkilendirmeleri</title>
</head>
<body>
<form id="Form1" runat="server">
<ext:ResourceManager ID="ResourceManager1" runat="server" />
<ext:Viewport ID="vpApplicationTypeDefinition" runat="server" Layout="FitLayout"
OverflowX="Auto" OverflowY="Auto">
<Items>
<ext:Container ID="conMain" runat="server" Layout="VBoxLayout" MinWidth="1300" MinHeight="640"
Padding="5">
<LayoutConfig>
<ext:VBoxLayoutConfig Align="Stretch" />
</LayoutConfig>
<Items>
<ext:TreePanel ID="tpMenuAuthorization" runat="server" Title="Men? Yetkilendirmeleri"
UseArrows="true" AutoScroll="true" Animate="true" RootViseible="false" Flex="26"
MarginSpec="0 0 5 0">
<Root>
<ext:Node NodeID="0" Text="Root" />
</Root>
<SelectionModel>
<ext:TreeSelectionModel ID="RowSelectionModel2" runat="server" Mode="Multi">
</ext:TreeSelectionModel>
</SelectionModel>
<DirectEvents>
<CheckChange OnEvent="SelectDeselectChildMenus" />
</DirectEvents>
</ext:TreePanel>
<ext:Button ID="btnUpdateAuthorization" runat="server" Text="G?ncelle" Flex="1" MaxWidth="100">
<DirectEvents>
<Click OnEvent="UpdateAuthorization">
<EventMask ShowMask="true" Msg="L?tfen bekleyiniz..." />
</Click>
</DirectEvents>
</ext:Button>
</Items>
</ext:Container>
</Items>
</ext:Viewport>
</form>
</body>
</html>
</html>

Daniil
May 30, 2014, 6:25 AM
Thank you.

A hidden field containing checked nodes is not updated automatically after a SetChecked node.

Please update from the SVN trunk (there was a small bug) and put this at the end of the SelectDeselectChildMenus method.

this.tpMenuAuthorization.Call("updateCheckSelection");

Also please remove that. It is not needed.

tpMenuAuthorization.Reload();

metci
May 30, 2014, 2:37 PM
Thanks Daniil, it worked.