通过链接重定向不;我不在jstree中工作

redirection by link doesn't work in jstree

本文关键字:jstree 工作 链接 重定向      更新时间:2023-09-26

在jstree jquery插件中,我希望当用户点击一个节点时,被重定向到点击链接的href。

这是传递给jstree:的数据

[
    {
        "id": 4,
        "contentable_id": 8,
        "contentable_type": "App''Unit",
        "text": "واحد جدید",
        "parent_id": "#",
        "icon": "fa fa-file-text-o",
        "a_attr": {
            "href": "./unit/8/edit"
        }
    },
    {
        "id": 5,
        "contentable_id": 5,
        "contentable_type": "App''Unit",
        "text": "واحد من ",
        "parent_id": "#",
        "icon": "fa fa-file-text-o",
        "a_attr": {
            "href": "./unit/5/edit"
        }
    }
]

下面是jstree调用代码:

$treeview.jstree({
    "core": {
        'data': {
            'url': targetUrl
        },
        "check_callback": true,
        "animation": 200,
        "dblclick_toggle": false,
        "keep_selected_style": false
    },
    "plugins": ["dnd", "contextmenu"],
    "contextmenu": {
        "select_node": false, 
        "show_at_node": false,
    }
    }).on('changed.jstree', function (e, data) {
        document.location = data.instance.get_node(data.selected[0], true).a_attr.href;
    });

正如您所看到的,我使用更改的jstree事件来捕获点击事件和重定向,因为这里推荐jstree作者。

但当点击节点上的链接时,出现了此错误:

Uncaught TypeError: Cannot read property 'href' of undefined

什么是问题,什么是正确的解决方案?

我可以找到解决方案。

通过尝试下面的代码,我的问题得到了解决。

on("changed.jstree", function (e, data) {
    if(data.node) {
       document.location = data.node.a_attr.href;
    }
});