Integrate jstree and select2

On my page I use jstreeand select2. I would like both plugins to be integrated. Is there anyway? My code looks very simple because it relies only on plugin integration fields and looks like this:

var dataSelect = [
    {
        id: 1,
        text: "test"
    },
    {
        id: 2,
        text: "test2"
    },
    {
        id: 3, text: "test3"
    }
];

var dataTree = [
    {
        id: 1, text: "test"
    },
    {
        id: 3,
        text: "test3"
    }
];

    $('#jstree').jstree({
    'core': {
        'data': dataTree,
        "themes": {
            "icons": false
        }
    },
    "checkbox": {
        "keep_selected_style": false,
        "three_state": true
    },
    "plugins": ["checkbox", "state"]
});

$("#selectSample").select2({
    data: dataSelect,
    multiple: true
});

JSFIDDLE

As you can see, the data in both cases has the same ID. If I click testin my tree, then I want mine to selectbe set to "test". Is there a solution to this problem?

EDIT

The answer to the question of Nikolai Ermakov led me to think correctly, but the problem is that I download selectajax. When I click on any option in my jstree, the data is destroyed and I cannot select anything later in mine select. In addition, none of the options are selected on select2.

EDIT2

When I click on any option in jstree, all parameters are deleted, and on select- one empty value that can be selected.

+4
source share
3 answers

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);
    });
+2

- -

var dataSelect = [{id: 1, text: "test"},{id:2, text: "test2"},{id:3, text: "test3"}];
var dataTree = [{id:1, text: "test"},{id:3, text: "test3"}];

$('#jstree').jstree({
        'core': {
            'data': dataTree,
            "themes":{
            "icons":false
        }
        },
            "checkbox": {
            "keep_selected_style": false,
            "three_state": true
        },
            "plugins" : [ "checkbox","state" ]
    })
.on("select_node.jstree", function(a,b){
  $("#selectSample").append('<option value="' + b.node.original.text + '">' + b.node.original.text + '</option>');
  var selectedValues = $("#selectSample").val();
  if (selectedValues == null) {
        selectedValues = new Array();
     }
  selectedValues.push(b.node.original.text);
  $("#selectSample").val(selectedValues).trigger('change');
});

$("#selectSample").select2({data:dataSelect, multiple: true});
0

Yes, I solved my problem! :)
I used the solution posted by @Nikolay Ermakov. However, my initiation of the selection was a bit wrong. Using $ .ajax () reads the argument, and I gave it to my choice later. Thus, the generated code works fine! :) Thank you all for your commitment! :)

0
source

Source: https://habr.com/ru/post/1618420/


All Articles