/// <summary> /// Create Json for tree view /// </summary> /// <param name="service">folder service</param> /// <param name="userID">user id</param> /// <returns>Json string</returns> public static string ToTreeJson(this IFolderService service, long userID) { JsonTreeFolder jsonArr = new JsonTreeFolder { id = 0, text = "root" }; foreach (var folder in service.GetFoldersByPredicate(fldr => fldr.Level == 1 && (fldr.OwnerID == userID))) { JsonTreeFolder jf = folder.ToJsonFolder(); AddChlds(service, jf); jsonArr.item.Add(jf); } return(new JavaScriptSerializer().Serialize(jsonArr)); }
/// <summary> /// Create Json for tree view /// </summary> /// <param name="enumeration">enumeration which to be converted</param> /// <param name="service">folder service</param> /// <returns>Json string</returns> public static string ToTreeJson(this IEnumerable <DtoFolder> enumeration, IFolderService service) { JsonTreeFolder jsonArr = new JsonTreeFolder { id = 0, text = "root" }; foreach (var folder in enumeration) { JsonTreeFolder jf = folder.ToJsonFolder(); AddChlds(service, jf); jsonArr.item.Add(jf); } return(new JavaScriptSerializer().Serialize(jsonArr)); }
private static void AddChlds(IFolderService service, JsonTreeFolder folder) { var chlds = service.GetNextLevelChildNodes(service.GetById(folder.id)).ToList(); folder.item = new List <JsonTreeFolder>(chlds.Select(item => item.ToJsonFolder())); if (folder.item.Any()) { foreach (var chld in folder.item) { var posibleChlds = service.GetNextLevelChildNodes(service.GetById(chld.id)).Select(item => item.ToJsonFolder()); if (posibleChlds.Any()) { AddChlds(service, chld); } } } }