/// <summary> /// 展开某个节点 /// </summary> /// <param name="treeItem"></param> public async Task ExpandAsync(TreeItemBase treeItem, bool autoRefresh = true) { if (treeItem.Expanded) { return; } treeItem.Expanded = true; if (LoadFullTree != null && LoadFullTree.Value) { var children = FindChildren(treeItem); await ExpandAsync(children, false); if (autoRefresh) { Refresh(); } return; } if (!string.IsNullOrWhiteSpace(Url)) { EnsureUrl(); var responseObj = (IEnumerable)await httpClient.GetFromJsonAsync(Url + treeItem.Id, typeof(List <>).MakeGenericType(ItemType)); var newTreeNodes = responseObj.Cast <TreeItemBase>().ToArray(); DataSource.RemoveAll(x => newTreeNodes.Any(y => y.Id == x.Id)); DataSource.AddRange(newTreeNodes); var rootNodes = FindChildren(new TreeItemModel() { Children = items }); Initilize(rootNodes, null); await AutoExpandAllAsync(); } Refresh(); }
private async Task LoadChildrenAsync(TreeItemBase row, bool autoExpand) { row.Expanded = true; row.IsLoading = true; Refresh(); var newDataSource = await FetchChildrenAsync(row); row.IsLoading = false; var enumerbale = DataSource as IEnumerable; var startIndex = 0; foreach (var item in enumerbale) { if (item == row) { break; } startIndex++; } var oldDataSource = enumerbale.Cast <object>().ToList(); oldDataSource.InsertRange(startIndex + 1, (newDataSource as IEnumerable).Cast <object>()); DataSource = oldDataSource; await InitlizeDataSourceAsync(row.Level.Value + 1, "up", autoExpand); if (DataSourceChanged.HasDelegate) { await DataSourceChanged.InvokeAsync(DataSource); } SyncFieldValue(); }
/// <summary> /// 展开某个节点,如果这个节点当前没有子节点,则动态加载子节点 /// </summary> /// <param name="item"></param> /// <param name="forceLoad">是否强制加载新数据</param> public async Task ExpandAsync(TreeItemBase item, bool forceLoad = false) { if (string.IsNullOrWhiteSpace(Url)) { ExceptionHelper.Throw(ExceptionHelper.ExpandOnlyHasUrl, "展开某个节点只支持异步加载"); } ResetSelectAllStatus(); item.Expanded = true; var finalChildren = GetAllChildren(item); if (forceLoad || !finalChildren.Any()) { if (!forceLoad) { item.HasChildren = true; } if (finalChildren.Count != rows.RemoveAll(finalChildren.Contains)) { ExceptionHelper.Throw(ExceptionHelper.ClearChildFailure, "清空子节点失败"); } DataSource = rows; await LoadChildrenAsync(item, AutoExpandAll); Refresh(); return; } LocalExpand(item, finalChildren); Refresh(); }
private List <TreeItemBase> FindChildren(TreeItemBase treeItem) { switch (DataFormat) { case DataFormat.List: return(items.Where(x => x.ParentId == treeItem.Id).ToList()); case DataFormat.Children: return(treeItem.Children ?? new List <TreeItemBase>()); } return(new List <TreeItemBase>()); }
private List <TreeItemBase> FindChildren(TreeItemBase child) { var results = new List <TreeItemBase>(); var treeItems = rows.Cast <TreeItemBase>().Where(x => x.ParentId == child.Id).ToArray(); foreach (var childTreeItem in treeItems) { results.Add(childTreeItem); results.AddRange(FindChildren(childTreeItem)); } return(results); }
private async Task ToggleAsync(TreeItemBase item) { if (item.Expanded) { item.Expanded = false; Refresh(); } else { await ExpandAsync(item); } item.Expanded = !item.Expanded; }
private void Initilize(List <TreeItemBase> nodes, TreeItemBase parentNode) { foreach (var node in nodes) { node.Level = (parentNode?.Level ?? -1) + 1; node.TextPaths = parentNode?.TextPaths == null ? new Dictionary <int, string>() : new Dictionary <int, string>(parentNode.TextPaths); if (!node.TextPaths.ContainsKey(node.Id)) { node.TextPaths.Add(node.Id, node.Text); } Initilize(FindChildren(node), node); } }
private List <TreeItemBase> GetAllChildren(TreeItemBase treeRow) { var treeRows = rows.Cast <TreeItemBase>().ToArray(); var children = treeRows.Where(x => x.ParentId == treeRow.Id).ToList(); var finalChildren = children.ToList(); foreach (var item in children) { finalChildren.AddRange(FindChildren(item)); } return(finalChildren); }
private async Task ForceExpandAsync(TreeItemBase item) { if (string.IsNullOrWhiteSpace(Url)) { ExceptionHelper.Throw(ExceptionHelper.ExpandOnlyHasUrl, "展开某个节点只支持异步加载"); } item.Expanded = true; var finalChildren = GetAllChildren(item); item.HasChildren = true; rows.RemoveAll(finalChildren.Contains); await LoadChildrenAsync(item, AutoExpandAll); Refresh(); }
private async Task SelectNodeAsync(TreeItemBase treeNode) { selectedNodes.Add(treeNode); if (SelectedNodesChanged.HasDelegate) { _ = SelectedNodesChanged.InvokeAsync(selectedNodes); } if (Select != null) { await Select.OnInternalSelectAsync(new SelectResultModel <int?>() { Text = treeNode.TextPath, Key = treeNode.Id }); } }
private async Task <IEnumerable> FetchChildrenAsync(TreeItemBase treeRow) { treeRow.Direction = "right"; if (!Url.EndsWith("/")) { Url += "/"; } var newDataSource = (IEnumerable)await httpClient.GetFromJsonAsync(Url + treeRow.Id, typeof(List <>).MakeGenericType(DataType)); if (!newDataSource.Cast <object>().Any()) { treeRow.HasChildren = false; } return(newDataSource); }
private void CalculateLevel(List <TreeItemBase> treeItems, TreeItemBase parentNode, TreeItemBase[] allTreeItems) { foreach (var treeRow in treeItems) { if (treeRow.Level != null) { return; } treeRow.Level = (parentNode?.Level ?? -1) + 1; treeRow.TextPaths = parentNode?.TextPaths == null ? new Dictionary <int, string>() : new Dictionary <int, string>(parentNode.TextPaths); if (!treeRow.TextPaths.ContainsKey(treeRow.Id)) { treeRow.TextPaths.Add(treeRow.Id, treeRow.Text); } var children = allTreeItems.Where(x => x.ParentId == treeRow.Id).ToList(); CalculateLevel(children, treeRow, allTreeItems); } }
private void LocalExpand(TreeItemBase treeRow, List <TreeItemBase> finalChildren) { hiddenRows.RemoveAll(finalChildren.Contains); treeRow.Direction = "right"; treeRow.Expanded = true; }
public async Task RefreshNodeAsync(TreeItemBase item) { await ExpandAsync(item, true); }