Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Selected Jstree node values JQuery

Tags:

jquery

jstree

Hi i have problem with getting data of selected node of jstree model.

<script type="text/javascript">
    $('#preview').on("changed.jstree", function (e, data) {
        console.log(data.selected);
        console.log(data.selected.attr("text"));
    });
</script>

the first console log shows me "[js1_1]" or "[js1_2]" depending on the selected node. But The second log "undefined is not a function" ;/ I have tried many different ways but i failed to achieve getting node text(title) or any other info

I send list of models as json and it looks like this:

Public Class JsTreeModel
   Public Property text As String
   Public Property icon As String
   Public Property Id As String
   Public Property PId As String
   Public Property ParentId As String
   Public Property Status As Integer
End Class

anyone has solution ?

UPDATE jstree code

<script type="text/javascript">
$(document).ready(function () {

    $('#WhenRemoving').toggle($('.RemoveCheckbox').is(":checked"));
    $('#WhenAdding').toggle(!$('.RemoveCheckbox').is(":checked"));

    $('#preview').jstree({
        'core': {
            'data': {
                'url': '/TreeTest/TreePreview/',
                'data': function (node) {
                    return node;
                }
            }
        }
    }).bind("loaded.jstree", function (event, data) {
        $(this).jstree("open_all");
    })
});
</script>

when i add this to my jstree i can see selected name node in html div

.on('changed.jstree', function (e, data) {
        var i, j, r = [];
        for (i = 0, j = data.selected.length; i < j; i++) {
            r.push(data.instance.get_node(data.selected[i]).text);
        }
        $('#event_result').html('Selected: ' + r.join(', '));
    })
});
like image 265
Jakub Wisniewski Avatar asked Nov 29 '22 01:11

Jakub Wisniewski


2 Answers

None above worked for me. This worked:

$(treeID).jstree().get_selected(true)[0].text;
like image 118
Strabek Avatar answered Dec 11 '22 10:12

Strabek


instead of

data.selected.attr("text")

try this

data.selected.text()

You can also get selected jstree node text with this :

console.log($("#preview").jstree("get_selected").text());

OR just bind select_node.jstree as shown

.bind("select_node.jstree", function (NODE, REF_NODE) {
        var a = $.jstree._focused().get_selected();
    }
like image 36
Kartikeya Khosla Avatar answered Dec 11 '22 11:12

Kartikeya Khosla