我有一个虚拟文件夹结构列表。我需要将该列表绑定到JsTreeView。列表可能包含以下内容。
商品清单
Dh
Dh\Sub
Dh\Sub\Another
Dh1
Dh1\Sub1
Dh1\Sub1\Another1
Dh1\Sub1\Another2
所需输出
Dh
|______ Sub
|_____Another
Dh1
|______ Sub1
|_____ Another1
|_____ Another2
因为,我尝试过使用递归的逻辑。但是,没有成功。任何人都能告诉我如何实现这一目标。任何对这个问题的帮助都将受到高度赞赏。
由于
答案 0 :(得分:0)
我使用以下类将DB数据序列化为JSON并加载到JS Tree View
/// <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属性包含子节点。填充它们取决于您的app逻辑。
以及后来的控制器
public virtual ContentResult LoadTreeData()
{
var treeData = Repository.GetTreeData();
return Content(JsonConvert.SerializeObject(treeData), "application/json");
}
希望这有帮助