页面启动时在jsTree中以编程方式选择节点的问题

问题描述 投票:0回答:1

我无法在页面启动时选择树形元素。有一个带有设置的JSFiddle:https://jsfiddle.net/voltel/aszn3h46/

我的数据是JSON数组:


    const a_data = [
      {
        "id": "1296",
        "text": "Disposable and Single-Use Medical Supplies",
        "children": null
      },
      {
        "id": "1275",
        "text": "Implantables",
        "children": [
          {
            "id": "1276",
            "text": "Defibrillators, Implantable",
            "children": null
          },
          {
            "id": "1338",
            "text": "Analysers, Laboratory In-Vitro Diagnostic, Clinical Chemistry, Manual",
            "children": null
          },
        ]}  
    ];

    // Rest of JavaScript code 

    const $tree = $("#display");

    $tree.jstree({
      'plugins' : [ "wholerow", "checkbox" ],
      'core': {
        data : a_data
      },
      'checkbox': {
        three_state: false
      },
    });
    //

    // The problem: I can't select/check node with this id
    const c_icd_device_type = "1338";

    // show initial value
    if (c_icd_device_type) {
      console.log(`Request to select node with id ${c_icd_device_type}`);

      const o_selected_node = $tree.jstree('get_node', c_icd_device_type);
      console.log('Selected node: ', o_selected_node);

        // Uncomment ONE of the following: 
      if (o_selected_node) {
        $tree.jstree('select_node', o_selected_node, true);
      } else {
          $tree.jstree(true).select_node(c_icd_device_type);
      }//endif
    }//endif

请在启动时帮助选择一个节点,并消除控制台中的一些可疑错误(请参阅JSFiddle)。>>

现在,我从StackOverflow上jsTree的其他问题中了解到,如果将我的代码包装在事件处理程序中选择一个元素,它将起作用。如果没有异步调用,我不太明白为什么这么复杂。如果不可避免,为什么不使用Promise?

$tree.on('ready.jstree', function (e, data) {
  console.log(`Request to select node with id ${c_icd_device_type}`);

  const o_selected_node = data.instance.get_node(c_icd_device_type);
  //const o_selected_node = $tree.jstree('get_node', c_icd_device_type);

  console.log('Selected node: ', o_selected_node);

    // Uncomment ONE of the following: 
  if (o_selected_node) {
    $tree.jstree('select_node', o_selected_node, true);
  } else {
      $tree.jstree(true).select_node(c_icd_device_type);
  }//endif

});

我无法在页面启动时选择树形元素。有一个带有设置的JSFiddle:https://jsfiddle.net/voltel/aszn3h46/我的数据是JSON数组:const a_data = [{“ id”:“ 1296”,...

jstree
1个回答
0
投票

我在Chrome的控制台中看不到任何错误:

© www.soinside.com 2019 - 2024. All rights reserved.