Get (main)parent id in extjs treeview - extjs

TreeView Extjs
As shown in the above image , if Node-1.2.3.1.1 is checked , how do i get the Id of Node-1.2 ?

you should navigate through the tree structure via the NodeInterface.
You have have access to parentNode and childNodes property.
So I suggest to do a recursive function to find your top node.
It should like:
function findRootParent(node) {
var parentNode = node.parentNode;
// wrong case (if you give the root node for example)
if(!parentNode) {
return null; // or self
}
// we've found it!
if(node.parentNode.isRoot())
return node;
// go recursive
return findRootParent(node.parentNode);
}
And when you've found the parent, you can check isExpanded() or not

Related

Creating a "Chain Reaction" of colliders or triggers in Unity 2D

I'm trying to create a 'Chain Reaction" of sorts. What I mean is that I have a bool on all of my game objects. When Object A's bool is set to true during gameplay, I would like object B to be set to true and then C and so on for every game object that is in the chain. It should also work the other way around, so that if A is set to false again, all of the objects in the 'chain' are set to false.
I currently have a system where I add each currently colliding object to a list of gameObjects. But I have no idea how to create this 'knock on' effect where one object affects all of the objects in the chain.
Is there a simpler way of achieving this?
What you show above can be described as a node, where each node has a child node (or nodes) and a parent node.
public class Node
{
public Node parentNode;
public Node childNode;
public bool someBool;
}
Then you can use a recursive function to traverse the nodes. Below is an example of traversal utilizing the child nodes.
public void SetBoolOnChildNodes(Node rootNode, bool value)
{
if (rootNode != null)
{
rootNode.someBool = value;
SetBoolOnChildNodes(rootNode.childNode, value);
}
}
If order is not important, it would make sense to have the root object hold a list of all nodes and have each node hold reference to the root object. Then you just set the boolean value using a loop constructed from the root objects node list.
public class Node2
{
public Node2 rootNode; // Null for root node, populated for all others
public List<Node2> nodes; // Populated only on root node (or all.. doesnt really matter other than memory)
public bool someBool;
}
public void SetValueOnChildNodes(Node2 rootNode, bool value)
{
if (rootNode == null) return;
rootNode.someBool = value;
foreach (var node in rootNode.nodes)
{
node.someBool = value;
}
}

Rename tree node in primeng

I am using primeng tree component, and I need to edit the name of selected tree node inline. I have implemented rename of the node with another dialog box. but, I want to rename it inline.
Is it possible to rename the selected tree node?
Yes i have implemented same functionality in tree node,
you will face the actual issue in IE. IE wont allow to input any text when it is draggable so you need to make it false when click on it or so.
//On blur
public renameFolder(selectedNode: TreeNode): any {
let flag: Boolean = false;
selectedNode.type = null;
selectedNode.draggable = true;
selectedNode.droppable = true;
}
/**
* To rename selected folder when rename folder
* is selected from context menu.
* type is used to match it with ng-template.
*/
private renameFolderContextMenu(selectedNode: TreeNode) {
this.renameFolderName = selectedNode.label;
selectedNode.type = 'renameFolder';
selectedNode.draggable = false;
selectedNode.droppable = false;
}

Visual Tree Finder is returning null while searching for Data Grid

I have Tab Control which has many tab items, I am checking Data Grid Items Count while closing tab items. For the first time it works fine(I mean in first iteration). After closing one tab item, in second iteration sellDtg is null. Does anyone know why it is happening? I am concerning that this is UI problem, layout is not being refreshed. Please help me or show direction.
while (tc.HasItems)
{
TabItem ti = tc.SelectedItem as TabItem;
if (ti.Header == "Продажа")
{
Microsoft.Windows.Controls.DataGrid sellDtg = FindChild<Microsoft.Windows.Controls.DataGrid>(tc, "SellDataGrid");
if (sellDtg.Items.Count > 0)
{
Sell sl = new Sell();
if (Sell.basketfromSellDateListBox == false)
{
sl.ClearBasket(sellDtg);
Sell.ClearFromSellBasket((int)sellDtg.Tag);
}
}
}
if (ti != null)
tc.Items.Remove(ti);
}
Thanks in advance!!!
I've written a simple FindChildLogical function in analogy for LogicalTreeHelper below:
public static T FindChildLogical<T>(DependencyObject parent, string childName)
where T : DependencyObject
{
if (parent == null) return null;
var child = LogicalTreeHelper.FindLogicalNode(parent, childName);
return (T)child;
}
and you call it as:
Microsoft.Windows.Controls.DataGrid sellDtg = FindChildLogical<Microsoft.Windows.Controls.DataGrid>(ti, "SellDataGrid");
I hope it gets you where you intend to.
I am going to assume your FindChild method uses the VisualTreeHelper to find its children.
In the first iteration, the TabItem's Content has been through a layout pass, and is visible. This means that the TabItem's Content will be in the visual tree.
However, for the other tab items, their Content hasn't been through a layout pass (it is only added to the visual tree when it's parent gets selected, and this has to then go through a layout/render pass), and won't be in the visual tree.
There are a couple of ways to get the child content of a TabItem that hasn't been through a layout pass as the selected tab:
1) You can try using the LogicalTreeHelper to find the Grid you're looking for (and you will likely have to search the Content of the TabItem, not the TabControl).
2) You can take your code out of the while loop, and do a callback on the dispatcher at the Loaded priority:
void RemoveAllItems()
{
if (!tc.HasItems) return;
TabItem ti = tc.SelectedItem as TabItem;
if (ti.Header == "Продажа")
{
var sellDtg = FindChild<Microsoft.Windows.Controls.DataGrid>(tc, "SellDataGrid");
if (sellDtg.Items.Count > 0)
{
Sell sl = new Sell();
if (Sell.basketfromSellDateListBox == false)
{
sl.ClearBasket(sellDtg);
Sell.ClearFromSellBasket((int)sellDtg.Tag);
}
if (ti != null)
tc.Items.Remove(ti);
}
}
Dispatcher.BeginInvoke(new Action(RemoveAllItems), DispatcherPriority.Loaded);
}
If you use the second method, you will likely be able to see the tab items removed one at a time, which may be something you don't want to see.

how do i select the node's index?

I have a xml populated combobox. If builderemail (from parsing of text using streamreader) is equals to the any one value found in the xml file, the combobox will select the index. how do i go about selecting it?
if (line.StartsWith("Builder_Email:"))
{
bool IsNodeExists = false;
string[] fields = line.Split('\t');
string builderemail = fields[3];
XmlDocument emailparse = new XmlDocument();
emailparse.Load(#"C:\GUI\buildermanageremail.xml");
XmlNodeList emailnode = emailparse.GetElementsByTagName("value");
if (string.IsNullOrEmpty(builderemail))
comboBox1.SelectedIndex = -1;
else
foreach (XmlNode node in emailnode)
{
if (builderemail == node.InnerText)
{
// how do i get the combobox selection right?
// need some code here
IsNodeExists = true;
break;
}
}
if(!IsNodeExists)
{
//create main node
XmlNode abc = emailparse.CreateNode(XmlNodeType.Element, "builder", null);
//create the first child node
XmlNode value = emailparse.CreateElement("value");
//set the value
value.InnerText = builderemail;
// add childes to father
//node.AppendChild(id);
abc.AppendChild(value);
// find the node we want to add the new node to
XmlNodeList l = emailparse.GetElementsByTagName("builderemail");
// append the new node
l[0].AppendChild(abc);
// save the file
emailparse.Save(#"C:\GUI\buildermanageremail.xml");
//then we populate the new updated xml file into the drop down list:
PopulateDDLFromXMLFile();
int count = emailparse.SelectNodes("email/builderemail/builder").Count;
count--;
comboBox1.SelectedIndex = count;
}
}
the place to look at is here:
foreach (XmlNode node in emailnode)
{
if (builderemail == node.InnerText)
{
// how do i get the combobox selection right?
// need some code here
IsNodeExists = true;
break;
}
}
I believe this code does everything that you want your code to do. This code is certainly not perfect, and may not even work, but if you compare it to yours you should find that it employs probably half a dozen practices that you're not following in your code and probably should be. If you cut out all the assertions, you'll find that it's only 10 lines of code (not counting the refactored method).
if (line.StartsWith("Builder_email:"))
{
Debug.Assert(
line.Where(x => x == '\t').Count() > 2),
"Can't parse input line.");
string builderEmail = line.Split('\t')[3];
Debug.Assert(
builderEmail != null && builderEmail == builderEmail.Trim(),
"Input data is bad.");
string filename = #"C:\GUI\buildermanageremail.xml"
Debug.Assert(
File.Exists(filename),
"Expected XML file does not exist.");
XmlDocument emailXml = new XmlDocument();
emailXml.Load(filename);
// In your real application, you know the name of the document element, so you
// should replace * with it in the XPath below.
string xpath = string.Format(
"/*/builderemail/builder/value[.='{0}']",
builderEmail);
if (emailXml.SelectSingleNode(xpath) == null)
{
CreateBuilderEmailElement(emailXml, builderEmail);
emailXml.Save(filename);
// I've changed the name of this method, which is problematic for several
// reasons - not least of which is that in my world, at least, "DDL" means
// "Data Definition Language."
//
// This also assumes that you've created an overload of the method that
// takes an XmlDocument argument.
PopulateEmailComboBox(emailXml);
}
// I'm assuming that the builderEmail is the actual text value stored in the
// combo box items, in which case all you need to do is find the item with that
// value and set SelectedItem, which will automatically set SelectedIndex. Also,
// if the value isn't found, setting SelectedItem to null automatically sets
// SelectedIndex to -1.
builderEmailComboBox.SelectedItem = builderEmailComboBox.Items
.Where(x => x.ToString() == builderEmail)
.FirstOrNull();
}
And here's the method for creating the builderemail element - which, by the way, should be named builderEmail if you have any say over it:
// this is refactored out as its own function, and made internal so that you
// can unit test it.
internal void CreateBuilderEmailElement(XmlDocument emailXml, string builderEmail)
{
XmlElement builder = emailXml.CreateNode("builder");
XmlElement value = emailXml.CreateNode("value");
builder.AppendChild(valueElm);
value.InnerText = builderEmail;
// again, you know the name of the document element in your application,
// so replace the * below with it.
Debug.Assert(
emailXml.SelectSingleNode("/*/builderemail") != null,
"No builderemail element found under the document element.");
emailXml.SelectSingleNode("/*/builderemail").AppendChild(builder);
}
Also, is there a reason that your XML has a separate value element under builderEmail, instead of builderEmail just containing the value?

AssociatedObject.FindName in Silverlight behavior OnAttached method returns null

I'm making a Silverlight behavior to enable dragging an element by a contained "drag handle" element (rather than the whole element being draggable). Think of it like a window title bar.
In the OnAttached method I am calling: AssociatedObject.FindName(DragHandle)
but this is returning null.
I then tried handling the AssociatedObject's Loaded event and running my code there, but I still get a null returned.
Am I misunderstanding what FindName is able to do? The AssociatedObject is in an ItemsControl (I want a collection of draggable elements). So is there some kind of namescope problem?
Yes, it sounds like a namescope problem. The MSDN documentation on XAML namescopes goes over how namesopes are defined for templates and item controls. Are you using a template for the items in your ItemsControl?
You may just have to walk the visual tree recursively with something like this to find the correct element by name:
private static FrameworkElement FindChildByName(FrameworkElement parent, string name)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
FrameworkElement child = VisualTreeHelper.GetChild(parent, i) as FrameworkElement;
if (child != null && child.Name == name)
{
return child;
}
else
{
FrameworkElement grandChild = FindChildByName(child, name);
if (grandChild != null)
{
return grandChild;
}
}
}
return null;
}

Resources