Return the value of a selected tree node

85 visualizaciones (últimos 30 días)
Theodore
Theodore el 9 de Ag. de 2023
Editada: Walter Roberson el 13 de Ag. de 2023
Hello, I am having trouble getting the value of a tree node in a UI that I am creating:
PrechargesNode is the parent node, Here i expand it and make all child nodes checked at the beginning
I am creating children nodes in the for loop. The number of nodes is dependent on what file I am using (which is why I have it looped)
My question is: How can i call each child node and make statements based off it being checked or not?
the names of each child node are different.
I can't seem to figure out how to do this easily. Thanks in advance!

Respuestas (1)

recent works
recent works el 10 de Ag. de 2023
Editada: Walter Roberson el 13 de Ag. de 2023
Theodore ha marcado con alerta este/a respuesta
It sounds like you're trying to work with a tree structure in a UI, where the tree has a parent node ("PrechargesNode") and several dynamically created child nodes. You want to perform actions based on whether each child node is checked or not. To achieve this, you'll need to have a way to identify and access each child node in your code. Here's a general outline of how you can approach this:
  1. Create Child Nodes: In your for loop, where you're creating child nodes, make sure to keep track of each node's handle (reference). This handle will allow you to interact with each node programmatically.
  2. Event Handling: Attach an event handler to each child node's check state change event. This event handler will be triggered when the user checks or unchecks a node in the tree.
  3. Action Based on Checked State: Inside the event handler, you can access the checked state of the node and perform the desired actions based on whether the node is checked or not.
Here's a basic example in MATLAB that demonstrates these steps:
% Assuming you have a tree object 'treeObj' and the parent node is 'PrechargesNode'
% 'numChildNodes' is the number of child nodes you're creating dynamically
% Create an array to store handles of child nodes
childNodeHandles = gobjects(1, numChildNodes);
% Create child nodes
for i = 1:numChildNodes
% Create a child node and store its handle
childNodeHandles(i) = uitreenode('v0', 'Text', ['ChildNode', num2str(i)], 'Parent', PrechargesNode);
end
% Attach event handlers to each child node
for i = 1:numChildNodes
set(childNodeHandles(i), 'NodeCheckedFcn', @(src,evt) onNodeChecked(src,evt,i));
end
% Event handler for node checked state change
function onNodeChecked(src, evt, childIndex)
node = evt.Node; % Get the node that triggered the event
isChecked = node.Checked; % Check if the node is checked
childName = node.Text; % Get the name of the child node
% Perform actions based on checked state
if isChecked
disp(['Child node ', childName, ' is checked.']);
% Add your actions for checked state here
else
disp(['Child node ', childName, ' is unchecked.']);
% Add your actions for unchecked state here
end
end
In this example, each dynamically created child node has an associated handle stored in the childNodeHandles array. We attach an event handler to the NodeCheckedFcn event of each child node. When a child node is checked or unchecked, the onNodeChecked function is called, and you can perform actions based on the checked state of the node.
Please adapt this code to your specific UI framework and language, as the example is based on MATLAB's UI components. The key concept is to track node handles, attach event handlers, and perform actions based on the checked state of each node.

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by