dongqufi82315 2019-03-16 18:59
浏览 117

如何让jsTree用PHP json数据显示叶子

I am using jsTree to create a treeview of my folders and files that are created in my database i have got jsTree to work with ajax to show the folders but, i am trying to figure out how to show files within the folders.

index.php

<?phpc session_start();
   $_SESSION['user_id'] = 1234567899;
?>
<html>
  <head>test</head>
  <body>
     <div id="projects_head"></div>

     <script type="text/javascript" src="dist/jstree.js"></script>
     <script type="text/javascript" src="cdn-jquery.js"></script>
     <script type="text/javascript">

        // get all projects
        $.ajax({
            type: "GET",
            url: 'assets/api/get-projects.php',
            dataType: "json",
            success: function(json){
                createJSTrees(json)
            }
        });

         //  jsTree view
        function createJSTrees(jsonData) {
            $('#projects_head').jstree({
                "plugins" : ["contextmenu", "dnd", "search", "state", "types", "wholerow"],
                'core': {
                    "check_callback": true,
                    'data': jsonData
                },
                'types': {
                    "#" : {
                        "max_children" : 1,
                        "max_depth" : 4,
                        "valid_children" : ["root"]
                    },
                    "child": {
                        "icon": "fa fa-file"
                    },
                    "root": {
                        "icon": "fa fa-folder-open",
                        "valid_children" : ["default"]
                    },
                    "default": {
                        "icon": "fa fa-folder",
                        "valid_children" : ["default","file"]
                    },
                    "file" : {
                        "icon" : "fa fa-file",
                        "valid_children" : []
                    }
                },
            });


     </script>
  </body
</html>

get-projects.php page

session_start();

include "conn.php";

$user_id = $_SESSION['user_id'];

$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * from projects where user_id = '$user_id'";
// $res = mysqli_query($conn, $sql) or die("database error:". 
mysqli_error($conn));
//     //iterate on results row and create new index array of data
//     while( $row = mysqli_fetch_assoc($res) ) { 
//         $data[] = $row;
//     }
//     $itemsByReference = array();
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_array($result)){
  $sub_data["id"] = $row["project_id"];
  $sub_data["name"] = $row["project_path"];
  $sub_data["text"] = $row["project_name"];
  $sub_data["parent_id"] = $row["parent_id"];
  $data[] = $sub_data;
}
$itemsByReference = array();
// Build array of item references:
foreach($data as $key => &$item) {
   $itemsByReference[$item['id']] = &$item;
   // Children array:
   $itemsByReference[$item['id']]['children'] = array();
   // Empty data class (so that json_encode adds "data: {}" ) 
   $itemsByReference[$item['id']]['data'] = new StdClass();
}
// Set items as children of the relevant parent item.
foreach($data as $key => &$item){
   if($item['parent_id'] && 
isset($itemsByReference[$item['parent_id']])){
       $itemsByReference [$item['parent_id']]['children'][] = &$item;
   }
}

// Remove items that were added to parents elsewhere:
foreach($data as $key => &$item) {
   if($item['parent_id'] && 
isset($itemsByReference[$item['parent_id']])){
      unset($data[$key]);
   }
}
// iterate to make the index in a sequential order
$record = array();
foreach($data as $rec){
  $record[] = $rec;
}
// Encode:
echo json_encode($record);

MySql database is projects and has the following columns: id, user_id, project_id, project_name, project_path, parent_id, created_at

i have read that in the JsTee the data that it read looks for children in the data by using:

"children" : [
       {
         "text" : "Child node 1",
         "state" : { "selected" : true },
         "icon" : "jstree-file"
       },
       { "text" : "Child node 2", "state" : { "disabled" : true } 
       }
   ]

How would i put that in the get-projects.php output? any help would be much appreciated thank you

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 远程桌面文档内容复制粘贴,格式会变化
    • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
    • ¥15 这种微信登录授权 谁可以做啊
    • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
    • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
    • ¥15 网络设备配置与管理这个该怎么弄
    • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
    • ¥20 西门子S7-Graph,S7-300,梯形图
    • ¥50 用易语言http 访问不了网页
    • ¥50 safari浏览器fetch提交数据后数据丢失问题