vakata / jstree

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

cannot add nodes dynamically using "_append_json_data" #2624

Closed gmetreveli closed 2 years ago

gmetreveli commented 2 years ago
  function createJSTree(jsonData) {
    $('#SimpleJSTree').jstree({
      core: {
        animation: 0,
        check_callback: true,
        themes: { stripes: true },
        data: jsonData,
      },
      types: {
        root: {
          icon: '/static/3.3.12/assets/images/tree_icon.png',
          valid_children: ['default'],
        },
        default: {
          valid_children: ['default', 'file'],
        },
        file: {
          icon: 'glyphicon glyphicon-file',
          valid_children: [],
        },
      },
      plugins: [
        'contextmenu',
        'dnd',
        'search',
        'state',
        'types',
        'wholerow',
      ],
    });
  }
  $.ajax({
    type: 'GET',
    url: 'urlwhereparentidisnull=',
    error: err => {},
    success: data => {
      createJSTree(data);
    },
  });

  function getNodeId() {
    var selectedNode = $('#SimpleJSTree').jstree('get_selected', true);
    var node_id = selectedNode[0].id;
    return node_id;
  }

  function ajax_children_data(parent_id) {
    $.ajax({
      type: 'GET',
      url: `urlwhereparentid=${parent_id}`,
      error: err => {},
      success: data => {
        var node_id = getNodeId();

        // console.log(selectedNode[0]);
        console.log(data);

        $('#SimpleJSTree').jstree(true)._append_json_data(node_id, data);
        // $('#SimpleJSTree').jstree(true).refresh();
      },
    });
  }

  $(document).on(
    'click',
    'ul.jstree-container-ul li a.jstree-anchor',
    function (e) {
      ajax_children_data(getNodeId());
    }
  );

ERROR

jstree.min.js:1559 Uncaught TypeError: Cannot read properties of undefined (reading 'call') at a.jstree.plugins.wholerow.k (jstree.min.js:1559:27) at a.jstree.plugins.wholerow. (jstree.min.js:1579:27)

I can log the data meaning I am getting json data from service I can't append it to selected node

vakata commented 2 years ago

This is not what _append_json_data is supposed to do - it is an internal function - do not use it to add data yourself.