【问题标题】:Pass JsTreeView data from database从数据库传递 JsTreeView 数据
【发布时间】:2014-12-09 07:59:22
【问题描述】:

我有一个虚拟文件夹结构列表。我需要将该列表绑定到 JsTreeView。该列表可能包含如下内容。

项目列表

Dh
Dh\Sub
Dh\Sub\Another
Dh1
Dh1\Sub1
Dh1\Sub1\Another1
Dh1\Sub1\Another2

期望的输出

Dh
  |______ Sub
             |_____Another
Dh1
  |______ Sub1
             |_____ Another1
             |_____ Another2

因为,我尝试过使用递归的逻辑。但是,并没有成功。谁能告诉我如何实现这一目标。对此问题的任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: asp.net-mvc c#-4.0 jstree


    【解决方案1】:

    我正在使用以下类将 DB 数据序列化为 JSON 并加载到 JS 树视图中

    /// <summary>
    /// Model which represents data for js tree view
    /// </summary>
    public class JsTreeViewNode
    {
        private int int_id;
    
        [JsonProperty(PropertyName = "id")]
        public string Id { get; set; }
    
        [JsonProperty(PropertyName = "text")]
        public string Text { get; set; }
    
        [JsonProperty(PropertyName = "children")]
        public List<JsTreeViewNode> Children { get; set; }
    
        [JsonProperty(PropertyName = "a_attr")]
        public AnchorAttribute AAttributes { get; set; }
    
        [JsonIgnore]
        public int CurrentLevel { get; set; }
    
        [JsonIgnore]
        public int SortOrder { get; set; }
    
        public JsTreeViewNode()
        {
            AAttributes = new AnchorAttribute();
        }
    
        public JsTreeViewNode(int id)
            : this()
        {
            int_id = id;
            this.Id = int_id.ToString();
            AAttributes.Id = this.Id;
        }
    
        public int GetNodeId()
        {
            return int_id;
        }
    
        public JsTreeViewNode Clone()
        {
            var clone = (JsTreeViewNode)this.MemberwiseClone();
            clone.Children = new List<JsTreeViewNode>();
            clone.AAttributes = this.AAttributes.Clone();
            return clone;
        }
    }
    
    public class AnchorAttribute
    {
        [JsonProperty(PropertyName = "draggable")]
        public string Draggable = "false";
    
        [JsonProperty(PropertyName = "id")]
        public string Id;
    
        [JsonProperty(PropertyName = "class", NullValueHandling = NullValueHandling.Ignore)]
        public string CssClassname;
    
        public virtual AnchorAttribute Clone()
        {
            var clone = (AnchorAttribute)this.MemberwiseClone();
            return clone;
        }
    }
    

    Children 属性包含子节点。填充它们取决于您的应用逻辑。

    后来在控制器中

    public virtual ContentResult LoadTreeData()
    {
      var treeData = Repository.GetTreeData();
      return Content(JsonConvert.SerializeObject(treeData), "application/json");
    }
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2018-02-20
      • 1970-01-01
      • 2013-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-25
      • 2021-12-17
      • 1970-01-01
      相关资源
      最近更新 更多