diff --git a/src/BootstrapBlazor.Server/Components/Samples/TreeViews.razor.cs b/src/BootstrapBlazor.Server/Components/Samples/TreeViews.razor.cs index 90988a57ab6..b887c2a04b0 100644 --- a/src/BootstrapBlazor.Server/Components/Samples/TreeViews.razor.cs +++ b/src/BootstrapBlazor.Server/Components/Samples/TreeViews.razor.cs @@ -3,8 +3,6 @@ // See the LICENSE file in the project root for more information. // Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone -using DocumentFormat.OpenXml.Spreadsheet; - namespace BootstrapBlazor.Server.Components.Samples; /// @@ -121,7 +119,20 @@ private Task OnDragItemEndAsync(TreeViewDragContext context) source.ParentId = context.IsChildren ? target.Id : target.ParentId; } } - DraggableItems = TreeFoo.CascadingTree(items); + + Action>? cb = null; + if (context.IsChildren) + { + // 自动展开目标节点 + cb = item => + { + if (item.Value.Id == context.Target.Value.Id) + { + item.IsExpand = true; + } + }; + } + DraggableItems = TreeFoo.CascadingTree(items, cb); DraggableItems[0].IsExpand = true; if (DraggableItems.Count > 1) { @@ -184,9 +195,9 @@ private static List GetDraggableItems() new() { Text = "Item D", Id = "4", ParentId = "1", Icon = "fa-solid fa-font-awesome" }, new() { Text = "Item E", Id = "5", ParentId = "1", Icon = "fa-solid fa-font-awesome" }, - new() { Text = "Item B (Drop inside blocked)", Id = "2", Icon = "fa-solid fa-font-awesome" }, + new() { Text = "Item B", Id = "2", Icon = "fa-solid fa-font-awesome" }, new() { Text = "Item F", Id = "6", ParentId = "2", Icon = "fa-solid fa-font-awesome" }, - new() { Text = "Item G (Can not move out)", Id = "9", ParentId = "2", Icon = "fa-solid fa-font-awesome" }, + new() { Text = "Item G", Id = "9", ParentId = "2", Icon = "fa-solid fa-font-awesome" }, new() { Text = "Item C", Id = "3", Icon = "fa-solid fa-font-awesome" }, new() { Text = "Item H", Id = "7", ParentId = "3", Icon = "fa-solid fa-font-awesome" }, diff --git a/src/BootstrapBlazor.Server/Data/TreeFoo.cs b/src/BootstrapBlazor.Server/Data/TreeFoo.cs index 7903740c814..8ee4da04619 100644 --- a/src/BootstrapBlazor.Server/Data/TreeFoo.cs +++ b/src/BootstrapBlazor.Server/Data/TreeFoo.cs @@ -177,10 +177,11 @@ public static List> GetCheckedTreeItems(string? parentId = /// 树状数据层次化方法 /// /// 数据集合 - public static List> CascadingTree(IEnumerable items) => items.CascadingTree(null, (foo, parent) => foo.ParentId == parent?.Value.Id, foo => new TreeViewItem(foo) + /// 节点状态回调方法 + public static List> CascadingTree(IEnumerable items, Action>? treeviewItemCallback = null) => items.CascadingTree(null, (foo, parent) => foo.ParentId == parent?.Value.Id, foo => new TreeViewItem(foo) { Text = foo.Text, Icon = foo.Icon, IsActive = foo.IsActive - }); + }, treeviewItemCallback); } diff --git a/src/BootstrapBlazor/Extensions/ExpandableNodeExtensions.cs b/src/BootstrapBlazor/Extensions/ExpandableNodeExtensions.cs index 7470b50abc9..ead5ae7bfb0 100644 --- a/src/BootstrapBlazor/Extensions/ExpandableNodeExtensions.cs +++ b/src/BootstrapBlazor/Extensions/ExpandableNodeExtensions.cs @@ -152,13 +152,16 @@ public static void SetParentExpand(this TNode node, bool expand) w /// 父级节点 /// 查找子节点 Lambda 表达式 /// - public static List> CascadingTree(this IEnumerable items, TreeViewItem? parent, Func?, bool> predicate, Func> valueFactory) => items - .Where(i => predicate(i, parent)) - .Select(i => + /// 节点是否展开回调方法 默认 null 未设置 + public static List> CascadingTree(this IEnumerable items, TreeViewItem? parent, Func?, bool> predicate, Func> valueFactory, Action>? treeViewItemCallback = null) => [.. items.Where(i => predicate(i, parent)).Select(i => + { + var item = valueFactory(i); + item.Items = items.CascadingTree(item, predicate, valueFactory, treeViewItemCallback); + item.Parent = parent; + if (treeViewItemCallback != null) { - var item = valueFactory(i); - item.Items = items.CascadingTree(item, predicate, valueFactory); - item.Parent = parent; - return item; - }).ToList(); + treeViewItemCallback(item); + } + return item; + })]; } diff --git a/test/UnitTest/Components/TreeViewTest.cs b/test/UnitTest/Components/TreeViewTest.cs index 8649f58d873..5939b90d2d2 100644 --- a/test/UnitTest/Components/TreeViewTest.cs +++ b/test/UnitTest/Components/TreeViewTest.cs @@ -742,7 +742,7 @@ public void CascadeSetCheck_Ok() new() { Text = "Test3", Id = "03", ParentId = "02" } }; - var node = TreeFoo.CascadingTree(items).First(); + var node = TreeFoo.CascadingTree(items, i => i.IsExpand = true).First(); // 设置当前几点所有子项选中状态 var cache = new TreeNodeCache, TreeFoo>(comparer);