获取自定义节树中的父节点

Get parent node in custom section tree

我在 Umbraco 7 的自定义部分中有一个多级树,其中根节点始终是国家/地区。

现在,在处理丹麦的项目 2 时,我想获取根节点(丹麦)的节点 ID,以便我知道我在哪个国家/地区 ID 下工作。

是否有任何 JavaScript API 可以递归返回节点级别,并获得最顶层的扩展?

提前致谢。 乔纳斯

最简单的方法是使用 Razor:

var parentNode = CurrentPage.AncestorOrSelf(1);

这将为您提供当前页面的根节点,您可以根据需要访问根节点的属性:

int id = parentNode.Id;

我所做的是使用editorState
(我是 运行 Umbraco 7.5.7)

angular.module("umbraco").controller("Client.Controller",
  function ($scope, editorState) {

    // Get the rootNode out of the path.
    var rootNode = editorState.current.path.split(",")[1];
});

由于路径包含了当前节点所在的整个结构,所以倒数第一个节点就是根节点。


另一个解决方案可能是这样的:
(我自己还没有测试过)
https://our.umbraco.org/forum/developers/extending-umbraco/73796-how-to-get-the-parentid-of-the-current-node-in-custom-tree-using-angularjs

简单地说:

inject appState and treeService into your angular controller

This will get the top most node of the current tree:
var rootNode = appState.getTreeState("currentRootNode");

This will get the current selected node in the custom tree
var selectedNode = appState.getTreeState("selectedNode");