I think I made your interaction two-way. Check out this script: JS Fiddle .
I used the changedjsTree and select/unselectselect2 event event .
:
var dataSelect = [{id: "1", text: "test"},{id:"2", text: "test2"},{id:"3", text: "test3"}];
var dataTree = [{id: "1", text: "test"},{id:"3", text: "test3"}];
$("#selectSample").select2({data:dataSelect, multiple: true});
$('#jstree')
.on('changed.jstree', function (event, data) {
var $select = $("#selectSample").select2(),
selectedOptions = $select.val() || [],
optionId = data.node.id;
if( data.action == 'select_node'){
selectedOptions.push( optionId );
$select.val(selectedOptions).trigger("change");
}
if( data.action == 'deselect_node'){
selectedOptions.splice( selectedOptions.indexOf( optionId ), 1 );
$select.val(selectedOptions).trigger("change");
}
})
.jstree({
'core': {
'data': dataTree,
"themes":{
"icons":false
}
},
"checkbox": {
"keep_selected_style": false,
"three_state": true
},
"plugins" : [ "checkbox" ]
});
$("#selectSample")
.on('select2:select', function(event) {
$('#jstree').jstree('select_node', '#'+event.params.data.id, true);
})
.on('select2:unselect', function(event) {
$('#jstree').jstree('deselect_node', '#'+event.params.data.id, true);
});