link 的重定向在 jstree 中不起作用
redirection by link doesn't work in jstree
在 jstree jquery plugin 中,我希望当用户点击一个节点时,被重定向到点击的 href link。
这是传递给 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;
});
如您所见,我使用 changed jstree 事件来捕获点击事件和重定向,因为推荐 jstree 作者 here .
但是当点击节点上的 link 时,出现了这个错误:
Uncaught TypeError: Cannot read property 'href' of undefined
什么是问题,什么是正确的解决方案?
我可以找到解决方案。
通过尝试下面的代码我的问题已经解决了。
on("changed.jstree", function (e, data) {
if(data.node) {
document.location = data.node.a_attr.href;
}
});
在 jstree jquery plugin 中,我希望当用户点击一个节点时,被重定向到点击的 href link。
这是传递给 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;
});
如您所见,我使用 changed jstree 事件来捕获点击事件和重定向,因为推荐 jstree 作者 here .
但是当点击节点上的 link 时,出现了这个错误:
Uncaught TypeError: Cannot read property 'href' of undefined
什么是问题,什么是正确的解决方案?
我可以找到解决方案。
通过尝试下面的代码我的问题已经解决了。
on("changed.jstree", function (e, data) {
if(data.node) {
document.location = data.node.a_attr.href;
}
});