douzhun5971 2017-09-27 16:16
浏览 129
已采纳

JsTree和Laravel麻烦

I'm following this guide to setup JsTree with lazy load using Ajax in my Laravel 5.5 app .

This is my controller: https://gist.github.com/aaronr0207/7fa0a38f40bfd2f728a15d655254f82d

My View: https://gist.github.com/aaronr0207/f87720263e3d6026b04b00c08bae5cb2

My JsTree class is exactly the same I didn't make any change.

Actually I'm getting the following error on chrome's console:

d9973d3e-1476-4453-a013-9e9c8430bcba:1 Uncaught TypeError: Cannot read property 'children' of undefined

enter image description here

But when I dump the response to debug it (at the end of TreeViewController data method):

        dd(response()->json($tree->build()));

It works...

enter image description here

My response looks like this (when I die-dump it):

enter image description here

Any idea? Thank you

EDIT1: If I return a simple json_encode($tree->build) there are no errors but it shows an empty tree... and the response looks like this:

enter image description here

EDIT2: got it! But now there are a new issue... All I did to solve it was change the url string with a callback:

 $('#jstree').jstree({
    'core': {
        'data': {
            'url': function (node) {
                return '{!! route('tree.data') !!}' ;
            },
            'data': function (node) {
                console.log(node);
                return {'id': node.id};
            }
        }
    }
});

But now when I fetch next level directories, if they have another directorie inside it fails without error:

enter image description here

Test1 content is the following:

enter image description here

If I delete test1/test2 folder, it works showing:

enter image description here

Same when I delete the txt file...What is happening now? Maybe this is a new question so I'll post my solution to the main problem and I'll accept it.

  • 写回答

3条回答 默认 最新

  • douniangliao4327 2017-09-29 08:43
    关注

    got it! All I did to solve it was change the url string with a callback:

    $('#jstree').jstree({
        'core': {
            'data': {
                'url': function (node) {
                    return '{!! route('tree.data') !!}' ;
                },
                'data': function (node) {
                    console.log(node);
                    return {'id': node.id};
                }
            }
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥100 栈回溯相关,模块加载后KiExceptionDispatch无法正常回溯了
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错