我正在使用jsTree创建在数据库中创建的文件夹和文件的树状视图,我已经将jsTree与ajax一起使用来显示文件夹,但是,我试图弄清楚如何在文件夹中显示文件。
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页面
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数据库是项目,具有以下几列:id,user_id,project_id,project_name,project_path,parent_id,created_at
我已经读到JsTee中读取的数据通过使用以下命令在数据中寻找子级:
"children" : [
{
"text" : "Child node 1",
"state" : { "selected" : true },
"icon" : "jstree-file"
},
{ "text" : "Child node 2", "state" : { "disabled" : true }
}
]
我如何将其放入get-projects.php输出中? 任何帮助将不胜感激,谢谢