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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 求数学坐标画圆以及直线的算法
    • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
    • ¥15 名为“Product”的列已属于此 DataTable
    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 自己瞎改改,结果现在又运行不了了
    • ¥15 链式存储应该如何解决
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站