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

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.