NodeJS 获取jstree中所有选定值的数组

uubf1zoe  于 2022-12-03  发布在  Node.js
关注(0)|答案(1)|浏览(154)

我有一个使用jstree插件的程序。一个带有层次数据的json文件被用作输入。你可以看到下面这个程序的代码示例。

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>Simple jsTree</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>
    <script type="text/javascript">
        $(function () {
            var jsondata = [
                           { "id": 1, "parent": "#", "text": "Math" ,"icon":false},
                           { "id": 2, "parent": 1, "text": "Algebra","icon":false },
                           { "id": 3, "parent": 2, "text": "Polynomials","icon":false },
                           { "id": 4, "parent": 3,"disabled":true, "text": "sum of polynomials","icon":false },
                           { "id": 5, "parent": "#", "text": "Physics","icon":false },
                           { "id": 6, "parent": 5, "text": "Mechanics","icon":false },
                           { "id": 7, "parent": 6, "text": "Kinematics","icon":false },
                           { "id": 8, "parent": 6, "text": "Dynamics","icon":false },
            ];

            createJSTree(jsondata);
        });

        function createJSTree(jsondata) {
            $('#SimpleJSTree').jstree({
                "core": {                    
                    'data': jsondata,
                    "multiple" : false
                },
                "plugins": ["search", "checkbox"],
                "search": {
                    "case_sensitive": false,
                    "show_only_matches": true
                }
            });
        }

        $(document).ready(function () {
            $(".search-input").keyup(function () {
                var searchString = $(this).val();
                $('#SimpleJSTree').jstree('search', searchString);
            });
        });
        
    </script>
</head>
<body>
    <input id="search-input" class="search-input" />
    <br />
    <div id="SimpleJSTree"></div>
</body>
</html>

我需要得到所有选定值的id数组。我应该如何解决这个问题

a64a0gku

a64a0gku1#

我会尝试,例如在事件select_node上:

$("#SimpleJSTree")
  .on("select_node.jstree", function(e, data) {
    for(var i = 0; i < data.selected.length; i++) {
      var id = data.instance.get_node(data.selected[i]).id;
      console.log(id);
    }
  });

相关问题