Przeglądaj źródła

Fixed filtering of Tree grids

Kenric Nugteren 2 tygodni temu
rodzic
commit
67868576f2

+ 5 - 0
InABox.Core/CoreUtils.cs

@@ -2587,6 +2587,11 @@ namespace InABox.Core
 
         #region IEnumerable Utilities
 
+        public static IEnumerable<T> NewEmpty<T>(this IEnumerable<T> enumerable)
+        {
+            yield break;
+        }
+
         /// <summary>
         /// Returns <paramref name="enumerable"/> as a <see cref="List{T}"/>;
         /// if it is already a <see cref="List{T}"/>, it is directly returned, instead of copying.

+ 2 - 2
inabox.wpf/DynamicGrid/Grids/BaseDynamicGrid.cs

@@ -1085,9 +1085,9 @@ public abstract class BaseDynamicGrid : ContentControl, IDynamicGridUIComponentP
                             {
                                 ProcessData(null);
                             }
-                            catch (Exception)
+                            catch (Exception e)
                             {
-
+                                CoreUtils.LogException("", e);
                             }
                             DoAfterRefresh();
                             bRefreshing = false;

+ 47 - 3
inabox.wpf/DynamicGrid/UIComponent/DynamicGridTreeUIComponent.cs

@@ -27,6 +27,7 @@ using System.Windows.Controls.Primitives;
 using NPOI.OpenXmlFormats.Dml;
 using System.Windows.Navigation;
 using NavigationMode = Syncfusion.UI.Xaml.Grid.NavigationMode;
+using javax.sql.rowset;
 
 namespace InABox.DynamicGrid;
 
@@ -452,7 +453,7 @@ public class DynamicGridTreeUIComponent<T, TKey> : IDynamicGridUIComponent<T>, I
         // Syncfusion has given us the row index, so it also will give us the correct row, after sorting.
         // Hence, here we use the syncfusion DataGrid.GetRecordAtRowIndex, which *should* always return a DataRowView.
         var row = _tree.GetNodeAtRowIndex(rowIndex);
-        return row.Item as CoreTreeNode<TKey>;
+        return row?.Item as CoreTreeNode<TKey>;
     }
 
     private CoreRow? GetRowFromIndex(int rowIndex)
@@ -1122,6 +1123,49 @@ public class DynamicGridTreeUIComponent<T, TKey> : IDynamicGridUIComponent<T>, I
         }
         return true;
     }
+    private IEnumerable<CoreRow> FilterRows(IEnumerable<CoreRow> rows)
+    {
+        var rowList = rows.Select(x => new
+        {
+            Row = x,
+            ID = GetIDKey(x),
+            Parent = GetParentKey(x)
+        }).ToArray();
+
+        var nullKey = new object();
+
+        var allRows = rowList.ToDictionary(x => x.ID ?? nullKey);
+        var lastAdded = rowList.Where(x => FilterRow(x.Row)).ToHashSet();
+        var allFilteredRows = lastAdded.ToHashSet();
+        var allFilteredRows1 = lastAdded.ToHashSet();
+
+        bool updated = true;
+        while (updated)
+        {
+            updated = false;
+
+            var currentSet = lastAdded.NewEmpty().ToHashSet();
+            foreach(var row in lastAdded)
+            {
+                if(allRows.TryGetValue(row.Parent ?? nullKey, out var parent))
+                {
+                    updated = allFilteredRows.Add(parent) || updated;
+                    currentSet.Add(parent);
+                }
+            }
+            foreach(var row in rowList)
+            {
+                if(allRows.TryGetValue(row.Parent ?? nullKey, out var parent) && allFilteredRows1.Contains(parent))
+                {
+                    updated = allFilteredRows1.Add(row) || updated;
+                }
+            }
+
+            lastAdded = currentSet;
+        }
+
+        return rowList.Where(x => allFilteredRows.Contains(x) || allFilteredRows1.Contains(x)).Select(x => x.Row);
+    }
 
     private void SetFilterUIButton(TreeGridColumn gridColumn, DynamicColumnBase column)
     {
@@ -1715,7 +1759,7 @@ public class DynamicGridTreeUIComponent<T, TKey> : IDynamicGridUIComponent<T>, I
                 });
         }
 
-        foreach (var row in data.Rows.Where(FilterRow))
+        foreach (var row in FilterRows(data.Rows))
         {
             var newRow = _innerTable.NewRow();
             ProcessRow(newRow, row);
@@ -1752,7 +1796,7 @@ public class DynamicGridTreeUIComponent<T, TKey> : IDynamicGridUIComponent<T>, I
             _rowMap.Clear();
         }
 
-        foreach(var row in rows.Where(FilterRow))
+        foreach(var row in FilterRows(rows))
         {
             var newRow = _innerTable.NewRow();
             ProcessRow(newRow, row);