We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

TreeView getSelectedNode() empty even if selected node checked.

Hi,

I have a Treeview that is populated with the viewBag.Data.

<div style="text-align:right;">
    <div style="display: inline-block;">
        @Html.EJ().TreeView("treeGeneralSecurityGroupMaintenance").TreeViewFields(s => s.Datasource((IEnumerable<DocTracerPortal.Models.Visual.VisualGroup>)ViewBag.groupsSourceGrid)
                .Id("id").ParentId("parentId").Text("name").HasChild("hasChild")).ShowCheckbox(true).AllowEditing(false).EnablePersistence(true).EnableRTL(true).LoadOnDemand(true).ClientSideEvents(e => e.NodeCheck("onItemSelectGroupsGeneralMaintenance")).ClientSideEvents(e => e.NodeUncheck("onItemSelectGroupsGeneralMaintenance"))
    </div>
</div>


Generally, the Treeview works fine.  However, I found some strange behaviour.

Sometimes, the getSelectedNode() functions simply return empty. Even if the Treeview have nodes selected.



In the previous image, the Treeview has the test developer node selected.
If I check the internal details of the Treeview the property  _CurrentSelectedNodes is fine with data.




This behaviour is erratic, I used the same code pieces to populated on demand the nodes. With other selections, it works perfect.

So, I'm not quite sure what could be the trigger for this behaviour.



Could you please tell what could be the problem.

Kind regards,

Juan Acosta

7 Replies

JA Juan Acosta December 5, 2016 06:49 AM UTC

I was checking the nodes received and they have the information properly mapped.



So, I think the issue is in the addNode function in js.

But,I don't why it could be happening.

Kind regards,
Juan


PR Piramanayagam Ramakrishnan Syncfusion Team December 6, 2016 08:50 AM UTC

Hi Juan, 
 
Thanks for contacting Syncfusion support. 
 
We have analyzed your query. We would like to inform you that, we can get the checked nodes by using getCheckedNodes method. This method return the all checked nodes of TreeView control. Please refer the below code sample, 
 
[Script] 
 
function getcheckednodes() { 
    var treeObj = $("#tree").data("ejTreeView"), nodetext = ""; 
    var nodes = treeObj.getCheckedNodes();//to get Checked nodes 
    console.log(nodes); 
    for (var i = 0; i < nodes.length; i++) { 
        nodetext += treeObj.getText($(nodes[i])) + ", "; //to get text of the given node 
    } 
    alert("Checked nodes are: " + nodetext); 
} 
 
 
For more details about the getCheckedNodes method then please refer the help document, 
 
For your reference, we have prepared a sample based on your requirement in the link: Sample 
 
Please let us know whether the provided sample is helpful in achieving your requirement. If not, get back to us with more information for us to assist you. 
 
Regards, 
Piramanayagam R


JA Juan Acosta December 7, 2016 10:26 AM UTC

Hi Piramanayagam,

I was checking the code that you passed to me and I was already using something similar.

function getAllSelectedNodesMaintenance() {
    console.log("getAllSelectedNodesMaintenance");
    treeObjGroup = $("#treeGeneralSecurityGroupMaintenance").ejTreeView('instance');
    //to get checkednodes
    var checkGroupNodes = treeObjGroup.getCheckedNodes();
    var checkAllGroupNodes = [];
    for (var i = 0; i < checkGroupNodes.length; i++) {
        checkAllGroupNodes.push(checkGroupNodes[i].id);
    }
    treeObjIndividual = $("#treeGeneralSecurityIndividualMaintenance").ejTreeView('instance');
    //to get checkednodes
    var checkIndividualNodes = treeObjIndividual.getCheckedNodes();
    for (var i = 0; i < checkIndividualNodes.length; i++) {
        checkAllGroupNodes.push(checkIndividualNodes[i].id);
    }
    return checkAllGroupNodes;
}


However, the issue is the same the treeView is marked and I cannot recover the checkedNode information.


The js console shows this.
 


As you can see, the length of the list of objects returned is 0.

There is any another way to recover the information in the nodes ?

I will be waiting for your reply.

Kind regards,

Juan



JA Juan Acosta December 7, 2016 10:30 AM UTC

By the way, you did not reply. Why if the getCheckedNodes() is 0 the _CurrentSelectedNode is 1?



So, what is happening with the control?

How can I activate the debug properties in the syncfusion controls? Well, you have debug options for the controls?

Kind regards,

Juan




BP Balamurugan P Syncfusion Team December 8, 2016 01:52 PM UTC

Hi Juan, 
Sorry for the Inconvenience caused. 
We have analyzed the reported issue with your code blocks; its working fine in our end. In our analysis, your reported case occurs, while unchecking the last remaining one checked node of TreeView. Here you will get the checked node details as empty, since you have unchecked the last node also. This is a correct behavior.  
 
Also we suggest you that don’t use our private properties “treeObjIndividual._CurrenctSelectedNodes”, since it won’t maintain the details properly. We recommend our public API’s that you can check this with our online API Documentation: https://help.syncfusion.com/api/js/ejtreeview   (Our EJMVC components are created as wrapper for the JavaScript components. So the API, and events will be the same in both JS and EJMVC components
 
As per our API documentation, you can get the checked node details in following ways. 
·         treeObjIndividual.model.checkedNodes  
 
Also, you can get the currently checked and unchecked node details innodeCheck”, “nodeUncheck” event arguments 
 
For your convenience we have prepared the simple sample based on this along with your code blocks and you can get it from following link. 
 
In case, if you face the same problem, please revert us by modifying the sample based on your application along with replication procedure and EJ script version and browser details. This would be helpful for us to serve you. 
Regards, 
Balamurugan


JA Juan Acosta December 9, 2016 07:24 AM UTC

Hi Balamurugan,

I will check if I can get the correct values using any of the other methods you suggested.

As a matter of fact, I got the issue when I repopulate the Treeview on demand depending on the changes in another dropdown .

In my opinion, the node details should not be empty. Because I repopulate the Treeview with complete details about the selected nodes with Ids and names.

The funny thing is if I rechecked the node with the issue with the mouse, the event populate the checked nodes with the proper values. So, That will mean the nodes are correctly populated. 

I will let you know if I can understand and reproduce the issue in your latest sample.

Kind regards,

Juan



PR Piramanayagam Ramakrishnan Syncfusion Team December 13, 2016 11:43 AM UTC

Hi Juan, 
 
Thanks for your update. 
 
We will wait to hear from you. Please share the more information along with below details, 
1.     How to expect state in TreeView after repopulate the TreeView on demand depending on the changes in another dropdown? Ie., if you want to maintain the initial checked states of TreeView after repopulate it then no need to use the EnablePersistence property in TreeView. 
2.     Repopulate the TreeView code. 
3.     Any simple sample or revert back the previously provided sample to reproduce issue in our end. 
 
These information would be helpful for us to identify the issue and provide a solution at the earliest. 
 
Regards, 
Piramanayagam R

Loader.
Live Chat Icon For mobile
Up arrow icon