当我尝试从json打印结果时,我正在尝试一个问题,这是我的代码:
_js_repo.php:
$db=new Database;
$res=$db->query("SELECT id, name, text, link as href, parent_id FROM repositorio");
//iterate on results row and create new index array of data
while( $row = mysqli_fetch_assoc($res) ) {
$data[] = $row;
}
$itemsByReference = array();
// Build array of item references:
foreach($data as $key => &$item) {
$itemsByReference[$item['id']] = &$item;
}
// 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']]['nodes'][] = &$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]);
}
// Encode:
echo json_encode($data);
repositorio.php:
<script>
jQuery(document).ready(function(){
jQuery.ajax({
url: "_js_repo.php",
cache: false,
success: function(response){
$('#treeview').treeview({
data: response,
showBorder: false,
enableLinks: true,
expandIcon: 'fa fa-angle-right',
collapseIcon: 'fa fa-angle-down',
nodeIcon: 'fa fa-folder'
});
}});
});
</script>
您可以在bramsc.com.mx/repositorio.php
和bramsc.com.mx/_js_repo.php
上查看实时问题
Chrome浏览器检查器:
Uncaught SyntaxError: Unexpected token .
bootstrap-treeview.min.js:1 Uncaught TypeError: Cannot set property 'nodeId' of undefined
at bootstrap-treeview.min.js:1
at Function.each (jquery-2.2.3.min.js:2)
at g.setInitialStates (bootstrap-treeview.min.js:1)
at g.init (bootstrap-treeview.min.js:1)
at new g (bootstrap-treeview.min.js:1)
at HTMLDivElement.<anonymous> (bootstrap-treeview.min.js:1)
at Function.each (jquery-2.2.3.min.js:2)
at n.fn.init.each (jquery-2.2.3.min.js:2)
at n.fn.init.a.fn.(anonymous function) [as treeview] (http://bramsc.com.mx/js/tree/bootstrap-treeview.min.js:1:16415)
at Object.success (repositorio.php:195)
repositorio.php上的输出为空,但在_js_repo.php中看起来不错。 非常感谢您的帮助。