vakata / jstree

jquery tree plugin
http://jstree.com
MIT License
5.15k stars 1.38k forks source link

Deselect nodes, then select on tree init and refresh (with state plugin activated) #2551

Closed nightrider77 closed 3 years ago

nightrider77 commented 3 years ago

Hello.

What is the proper way to combine deselect_all and then _open_to functions, so the init and refresh logical blocks will work correctly? Now just nothing gets selected after deselect_all and _open_to commands.

var on_init = true;

function tree_init(node_id){
    if ( on_init == false ) {
        $('#tree').jstree(true).settings.core.data = _some_data_;
        $('#tree').jstree(true).refresh();
    } else {
        on_init = false;
        $('#tree').on('refresh.jstree state_ready.jstree', function (e, data) {
            if ( node_id != 0 ) {
                data.instance.deselect_all(true); // for state plugin
                data.instance._open_to(node_id);
            }
        }).jstree({
        'state' : { "key" : "tree_manager" },
        'core' : {
            'check_callback' : true,
            'data' : [_some_data_],
            'multiple': false,
            'open_parents': true,
            'load_open': true
        },
        'plugins' : [ "search", "state", "unique", "changed", "wholerow" ]
        });
    }
}

Thank you for answer.

vakata commented 3 years ago

I am not sure what you are trying to achieve here. It seems you want to keep only the open state but not the selected state. If that is the case - use this: https://www.jstree.com/api/#/?q=filter&f=$.jstree.defaults.state.filter