php jstree 如何仅当我单击父级展开时才从数据库加载子级
php jstree how to load children from database only when i click the parent to expand
我正在创建一个 jstree 来对数据库的所有数据进行分类并将其显示在 ajax 树中。我希望我可以创建一个 jstree,当我单击父级展开时,子级只从数据库加载,以减少开始时加载页面的时间。以下是我用来创建 jstree 的代码。如何修改代码,让我可以创建一个jstree,当我点击父节点展开时只加载子节点?
这是我用来显示 jstree 的代码。
$('#ajax').jstree({
'core' : {
'data' : {
"url" : "<?php echo $weburls; ?>loadtree.php",
"dataType" : "json"
}
}
});
这是我用来将数据加载到数组并传递给 jstree 的代码。
//insert parents into array
$resultProj = getResult("SELECT DISTINCT parents FROM table WHERE parents IS NOT NULL ");
for($i2=0;$i2<sizeOf($resultProj);$i2++){
$dresult[$i2]['id']= $i2+1;
$dresult[$i2]['name']=$resultProj[$i2]['parents'];
$dresult[$i2]['text']=$resultProj[$i2]['parents'];
$dresult[$i2]['parent_id']='0';
$ddresult[$i2]['id']= $i2+1;
$ddresult[$i2]['name']=$resultProj[$i2]['parents'];
}
$i3=sizeOf($dresult);
$i4=sizeOf($dresult);
//insert childrens into array
foreach($ddresult as $dresult2){
$resultcust = getResult("SELECT no, name FROM table WHERE parents='" . $dresult2['name'] . "'");
$i5=0;
for($i3;$i3<(sizeOf($resultcust)+$i4);$i3++){
$dresult[$i3]['id']=$resultcust[$i5]['no'];
$dresult[$i3]['name']=$resultcust[$i5]['name'];
$dresult[$i3]['text']=$resultcust[$i5]['name'];
$dresult[$i3]['parent_id']=$dresult2['id'];
$i5++;
}
$i3=sizeOf($dresult);
$i4=sizeOf($dresult);
}
$itemsByReference = array();
// Build array of item references:
foreach($dresult 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']]['dresult'] = new StdClass();
}
// Set items as children of the relevant parent item.
foreach($dresult 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($dresult as $key => &$item) {
if($item['parent_id'] && isset($itemsByReference[$item['parent_id']]))
unset($dresult[$key]);
}
echo json_encode( $dresult );
为特定 parent 延迟加载节点的方法是通过 REST API 仅获取 child 节点。对于每个请求,您必须在 AJAX 调用中传递一个节点 ID。因此,您将需要在 jstree 配置中添加一个额外的数据回调,它将节点 ID 附加到您的请求中。
$('#ajax').jstree({
'core' : {
'data' : {
"url" : "<?php echo $weburls; ?>loadtree.php",
"data" : function (node) {
if(node.id === '#'){ return; }
return { 'id': node.id };
}
}
}
});
因此 url 的生成可能看起来有点像这样
为根节点获取 children - myservice.com/loadtree.php
为 Child1 获取 children - myservice.com/loadtree.php?id=child1
注意:为节点生成的 ID 在整个树结构中必须是唯一的,这一点很重要。
我正在创建一个 jstree 来对数据库的所有数据进行分类并将其显示在 ajax 树中。我希望我可以创建一个 jstree,当我单击父级展开时,子级只从数据库加载,以减少开始时加载页面的时间。以下是我用来创建 jstree 的代码。如何修改代码,让我可以创建一个jstree,当我点击父节点展开时只加载子节点?
这是我用来显示 jstree 的代码。
$('#ajax').jstree({
'core' : {
'data' : {
"url" : "<?php echo $weburls; ?>loadtree.php",
"dataType" : "json"
}
}
});
这是我用来将数据加载到数组并传递给 jstree 的代码。
//insert parents into array
$resultProj = getResult("SELECT DISTINCT parents FROM table WHERE parents IS NOT NULL ");
for($i2=0;$i2<sizeOf($resultProj);$i2++){
$dresult[$i2]['id']= $i2+1;
$dresult[$i2]['name']=$resultProj[$i2]['parents'];
$dresult[$i2]['text']=$resultProj[$i2]['parents'];
$dresult[$i2]['parent_id']='0';
$ddresult[$i2]['id']= $i2+1;
$ddresult[$i2]['name']=$resultProj[$i2]['parents'];
}
$i3=sizeOf($dresult);
$i4=sizeOf($dresult);
//insert childrens into array
foreach($ddresult as $dresult2){
$resultcust = getResult("SELECT no, name FROM table WHERE parents='" . $dresult2['name'] . "'");
$i5=0;
for($i3;$i3<(sizeOf($resultcust)+$i4);$i3++){
$dresult[$i3]['id']=$resultcust[$i5]['no'];
$dresult[$i3]['name']=$resultcust[$i5]['name'];
$dresult[$i3]['text']=$resultcust[$i5]['name'];
$dresult[$i3]['parent_id']=$dresult2['id'];
$i5++;
}
$i3=sizeOf($dresult);
$i4=sizeOf($dresult);
}
$itemsByReference = array();
// Build array of item references:
foreach($dresult 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']]['dresult'] = new StdClass();
}
// Set items as children of the relevant parent item.
foreach($dresult 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($dresult as $key => &$item) {
if($item['parent_id'] && isset($itemsByReference[$item['parent_id']]))
unset($dresult[$key]);
}
echo json_encode( $dresult );
为特定 parent 延迟加载节点的方法是通过 REST API 仅获取 child 节点。对于每个请求,您必须在 AJAX 调用中传递一个节点 ID。因此,您将需要在 jstree 配置中添加一个额外的数据回调,它将节点 ID 附加到您的请求中。
$('#ajax').jstree({
'core' : {
'data' : {
"url" : "<?php echo $weburls; ?>loadtree.php",
"data" : function (node) {
if(node.id === '#'){ return; }
return { 'id': node.id };
}
}
}
});
因此 url 的生成可能看起来有点像这样
为根节点获取 children - myservice.com/loadtree.php
为 Child1 获取 children - myservice.com/loadtree.php?id=child1
注意:为节点生成的 ID 在整个树结构中必须是唯一的,这一点很重要。