【问题标题】:Multi Level Tree in Umbraco Custom SectionUmbraco 自定义部分中的多级树
【发布时间】:2012-07-30 07:18:37
【问题描述】:

我目前正在尝试创建自定义树,但在尝试渲染子节点时遇到了麻烦。在浏览了各种文章/帖子后,我在这一点上:

public override void Render(ref XmlTree tree)
{
  List<Node> articles = NodeUtil.GetAllNodesOfDocumentType(-1, "Promoter");
  Node article = articles.Where(p => p.CreatorID == UmbracoEnsuredPage.CurrentUser.Id).FirstOrDefault();

  if(promo != null)
  {
      var dNode = XmlTreeNode.Create(this);
      dNode.NodeID = article.Id.ToString();
      dNode.Text = article.Name;
      dNode.Icon = "doc.gif";
      dNode.Action = "javascript:openArticle(" + article.Id + ")";
      dNode.Source = article.Children.Count > 0 ? this.GetTreeServiceUrl("" + article.Id) : "";
      tree.Add(dNode);
  }
}

上面的代码获取了属于当前用户的文章(为了测试,目前每个用户只有一篇文章)。然后,我尝试打印出本文的子项,但没有得到所需的输出,而是得到以下结果:

Article Name
- Article Name
  - Article Name
   - Article Name

每次我展开一个节点时,它似乎只是渲染同一个节点,然后继续。

我见过其他使用树服务的方法,例如:

TreeService treeService = new TreeService(...);
node.Source = treeService.GetServiceUrl();

但我收到一条错误消息,指出没有采用 0 个参数的 GetServiceUrl 方法。我假设上述方法适用于早期版本?

【问题讨论】:

    标签: c# .net tree umbraco


    【解决方案1】:

    我花了一段时间才解决这个问题。这是解决方案,希望对某人有所帮助。

    const string PARENT_ID = "10"; // The ID of the node that has child nodes
    
    public override void Render(ref XmlTree tree)
    {
      if (this.NodeKey == PARENT_ID) // Rendering the child nodes of the parent folder
      {
        // Render a child node
        XmlTreeNode node = XmlTreeNode.Create(this);
        node.NodeID = "11";
        node.Text = "child";
        node.Icon = "doc.gif";
        node.Action = ...
        tree.Add(node);
      }
      else // Default (Rendering the root)
      {
        // Render the parent folder
        XmlTreeNode node = XmlTreeNode.Create(this);
        node.NodeID = PARENT_ID;
        node.Source = this.GetTreeServiceUrl(node.NodeID);
        node.Text = "parent";
        node.Icon = "folder.gif";
        tree.Add(node);
      }
    }
    

    【讨论】:

    • 啊!谢谢你 - 我没有意识到每次打开节点时它都会调用渲染 - 所以很长一段时间以来我一直对为什么我不断在整个地方重复节点感到困惑;)
    【解决方案2】:

    输出表明您正在构建的节点树正在嵌套每个子节点 - 这是because the nodeId is being reset to -1 with each pass

    our.umbraco.org 上的This post 描述了同样的问题,并建议您使用 NodeKey 而不是 ID 在节点之间移动。

    **

    不一定有用,但我会使用 ucomponents 包附带的uQuery language extensions(谁安装 Umbraco 而没有 ucomponents?),以简化方法调用:

    例如:

    List<Node> articles = uQuery.getNodesByType("Promoter");
    foreach(Node article in articles)
    {
      List<Node> children = article.GetDescendantNodes();
      ... build tree
    }
    

    【讨论】:

    • 您好 amelvin,感谢您的回复。我目前不在电脑旁,但您提供的信息似乎很有帮助。当我尝试过时,我会报告。再次感谢! :)
    • 嗨 amelvin,我还没有开始尝试这个,但它似乎正是我所做的,并且已经为其他用户解决了这个问题(在 umbraco 论坛上)。我现在要接受这个答案,如果我发现它不起作用,我会报告。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    • 2019-11-20
    相关资源
    最近更新 更多