Selecting the tree nodes that match the selected criteria

1,092 views
Skip to first unread message

Ravi K

unread,
Jun 16, 2015, 8:43:58 AM6/16/15
to jst...@googlegroups.com
Currently i am using the below to highlight the nodes in red color which match the search criteria

 $('#TreeView').jstree("search", $("#searchVal").val());

How would i select the nodes that match the search criteria?

Ivan Bozhanov

unread,
Jun 16, 2015, 11:54:01 AM6/16/15
to jst...@googlegroups.com, prog...@gmail.com
$('#TreeView').on('search.jstree', function (e, data) { data.instance.select_node(data.res); });

Best regards,
Ivan

Ravi K

unread,
Jun 16, 2015, 12:14:08 PM6/16/15
to jst...@googlegroups.com, prog...@gmail.com
Hey Ivan,
I tried the below but its not selecting all nodes. Example- all nodes have text "Root" in them and when i type Root in the search box its turning all nodes Red as expected as it matches all nodes but only selecting a few nodes (The first and second).
I included as below:

$('#TreeView').jstree({
        "plugins": ["themes", "checkbox", "search", "types"],
        'core': {
            'data': {
                'url': function (node) {
                    return node.id === '#' ?
                    'api' + '/DemographicElements.mvc/DemographicElements/' + elementId :
                    'api' + '/DemographicChildElements.mvc/DemographicChildElements/' + node.id;
                },
                'type': 'GET',
                'dataType': 'JSON',
            },
            "themes": {
                "icons": false,
                "stripes": true,
                "default": true
            }
        }
    }).on("select_node.jstree",
     function (evt, data) {
         UpdateSummaryFromTreeView();

     }
    ).on('search.jstree',
        function (e, data) {
            data.instance.select_node(data.res);
        });

Ivan Bozhanov

unread,
Jun 16, 2015, 1:22:12 PM6/16/15
to jst...@googlegroups.com, prog...@gmail.com
I am afraid I can't reproduce: http://jsfiddle.net/DGAF4/533/

Just to be safe - which version are you using? You might also try select_node(data.nodes) just in case.
The other reason for any failure might be the function you are invoking (if it changes the tree in any way).

Best regards,
Ivan

Ravi K

unread,
Jun 16, 2015, 4:32:18 PM6/16/15
to jst...@googlegroups.com, prog...@gmail.com
Hey Ivan,
I am using the latest version 3.1.1.
I am sorry all nodes don't have the name Root, they have the names like below
Root Node : 1
Root Node : 2 etc upto Root Node : 100
I type in Root Node :1 and it just selects first node Root Node : 1 (when the debug the event data.res and data.nodes show all matches but nodes are not being selected)

Ravi K

unread,
Jun 16, 2015, 4:38:56 PM6/16/15
to jst...@googlegroups.com
function UpdateSummaryFromTreeView() {

    var selectedPredicates = new Array();
    var selectedFloor = new Array();
    var selectedCeil = new Array();
    var selectedCount = 0;
    var predicateId = "";
    var groupId = 0;
    var elementId = 0;

    var selectedElementsCount = $('#TreeView').jstree().get_selected(true).length;
    for (counter = 0; counter < selectedElementsCount ; counter++) {
        selectedPredicates[selectedCount] = $('#TreeView').jstree().get_selected(true)[counter].text;
        selectedFloor[selectedCount] = "";
        selectedCeil[selectedCount] = "";
        selectedCount++;
    }

    groupId = DemographicElementForm.DemographicTreeViewControl$GroupId.value;
    elementId = DemographicElementForm.DemographicTreeViewControl$ElementId.value;

    sourceWindow.UpdateElementStatus(groupId, elementId, selectedPredicates, selectedFloor, selectedCeil);
}

I am not altering the treeview in the above function which is called when node is selected

Ivan Bozhanov

unread,
Jun 17, 2015, 3:25:54 AM6/17/15
to jst...@googlegroups.com, prog...@gmail.com
I am sorry, but I can't help you without a demo - could you please modify the fiddle, so that I can see the issue and fix it.

Btw - you can seriously optimize the function you are calling - using get_selected(true) on each iteration is not efficient at all - first you get all selected nodes along with their details, just to use the count, and then on each iteration you get all selected nodes with all their details again, just to use a single result. Also there is no need for all the extra variables. Try this:
    var selectedElements = $('#TreeView').jstree().get_selected(true);
    for (counter = 0; counter < selectedElements.length ; counter++) {
        selectedPredicates[counter] = selectedElements[counter].text;
        selectedFloor[counter] = "";
        selectedCeil[counter] = "";
    }

Best regards,
Ivan

Ravi K

unread,
Jun 17, 2015, 8:14:19 AM6/17/15
to jst...@googlegroups.com, prog...@gmail.com
thanks Ivan for the suggestion. i will get together a demo for you.
Reply all
Reply to author
Forward
0 new messages