Browse Source

Added Filtering to DynamicGridTreeUIComponent

frogsoftware 1 year ago
parent
commit
2a2588cdb1

+ 7 - 0
inabox.wpf/DynamicGrid/DynamicGrid.cs

@@ -707,6 +707,13 @@ public abstract class DynamicGrid<T> : DynamicGrid, IDynamicGridUIComponentParen
         OnRowsDragStart(rows);
     }
 
+    void IDynamicGridUIComponentParent<T>.UIFilterChanged(object sender) => DoFilterChanged();
+
+    protected virtual void DoFilterChanged()
+    {
+        
+    }
+
     #endregion
 
     protected virtual DynamicGridRowStyleSelector<T> GetRowStyleSelector()

+ 18 - 4
inabox.wpf/DynamicGrid/DynamicSelectorGrid.cs

@@ -39,7 +39,21 @@ public class DynamicSelectorGrid<T> : DynamicDataGrid<T>
     {
         return new Guid[] { id };
     }
-    
+
+    protected IEnumerable<Guid> VisibleGuids => GetVisibleRows().Select(r => r.Get<T, Guid>(x => x.ID));
+
+    protected override void DoFilterChanged()
+    {
+        base.DoFilterChanged();
+        DoSelectionChanged();
+    }
+
+    private void DoSelectionChanged()
+    {
+        var ids = SelectedIDs.Intersect(VisibleGuids).ToHashSet();
+        SelectionChanged?.Invoke(ids);
+    }
+
     private bool Selected_Click(CoreRow? row)
     {
         if (row is null)
@@ -74,7 +88,7 @@ public class DynamicSelectorGrid<T> : DynamicDataGrid<T>
             }
         }
 
-        SelectionChanged?.Invoke(SelectedIDs);
+        DoSelectionChanged();
 
         InvalidateRow(row);
         return false;
@@ -83,14 +97,14 @@ public class DynamicSelectorGrid<T> : DynamicDataGrid<T>
     private void DeselectAll_Click()
     {
         SelectedIDs.Clear();
-        SelectionChanged?.Invoke(SelectedIDs);
+        DoSelectionChanged();
         InvalidateGrid();
     }
 
     private void SelectAll_Click()
     {
         SelectedIDs = Data.Rows.Select(x => x.Get<T, Guid>(x => x.ID)).ToHashSet();
-        SelectionChanged?.Invoke(SelectedIDs);
+        DoSelectionChanged();
         InvalidateGrid();
     }
 

+ 1 - 0
inabox.wpf/DynamicGrid/UIComponent/DynamicGridGridUIComponent.cs

@@ -499,6 +499,7 @@ public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
         {
             _filterpredicates[e.Column.MappingName] = Serialization.Serialize(e.FilterPredicates, true);
         }
+        Parent.UIFilterChanged(this);
 
         UpdateRecordCount();
     }

+ 34 - 9
inabox.wpf/DynamicGrid/UIComponent/DynamicGridTreeUIComponent.cs

@@ -20,6 +20,7 @@ using System.Windows.Data;
 using System.Windows.Input;
 using System.Windows.Media;
 using System.Windows.Media.Imaging;
+using Syncfusion.UI.Xaml.TreeGrid.Filtering;
 
 namespace InABox.DynamicGrid;
 
@@ -229,9 +230,14 @@ public class DynamicGridTreeUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
         cellStyle.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.White)));
         _tree.RowStyle = cellStyle;
 
+        var filterstyle = new Style(typeof(TreeGridFilterControl));
+        filterstyle.Setters.Add(new Setter(TreeGridFilterControl.SortOptionVisibilityProperty, Visibility.Collapsed));
+        filterstyle.Setters.Add(new Setter(TreeGridFilterControl.ImmediateUpdateColumnFilterProperty, false));
+        _tree.FilterPopupStyle = filterstyle;
+        
         _tree.FilterChanged += _tree_FilterChanged;
         _tree.FilterItemsPopulating += _tree_FilterItemsPopulating;
-        
+        _tree.FilterLevel = FilterLevel.Extended;
         _tree.SelectionForeground = DynamicGridUtils.SelectionForeground;
         _tree.SelectionBackground = DynamicGridUtils.SelectionBackground;
         
@@ -379,9 +385,22 @@ public class DynamicGridTreeUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
     private void _tree_FilterItemsPopulating(object? sender, Syncfusion.UI.Xaml.TreeGrid.Filtering.TreeGridFilterItemsPopulatingEventArgs e)
     {
         var col = _tree.Columns.IndexOf(e.Column);
-        if (GetColumn(col) is DynamicActionColumn column && column.Filters is not null)
-            e.ItemsSource = column.Filters.Select(x => new FilterElement
-            { DisplayText = x, ActualValue = x, IsSelected = column.SelectedFilters is null || column.SelectedFilters.Contains(x) });
+        if (GetColumn(col) is DynamicActionColumn dac && dac.Filters is not null)
+        {
+            e.ItemsSource = dac.Filters.Select(x => new FilterElement
+            {
+                DisplayText = x, ActualValue = x,
+                IsSelected = dac.SelectedFilters is null || dac.SelectedFilters.Contains(x)
+            });
+        }
+        else if (GetColumn(col) is DynamicGridColumn dgc)
+        {
+            var preds = e.Column.FilterPredicates.Select(x => x.FilterValue).ToArray();
+            var data = Parent.Data.Rows.Select(r => r.Get<String>(dgc.ColumnName)).OrderBy(x=>x);
+            
+            e.ItemsSource = data.Select(x => new FilterElement()
+                { DisplayText = x ?? "(Blanks)", ActualValue = x,  IsSelected = !preds.Any() || preds.Contains(x) });
+        }
     }
 
     private void _tree_FilterChanged(object? sender, Syncfusion.UI.Xaml.TreeGrid.Filtering.TreeGridFilterChangedEventArgs e)
@@ -413,6 +432,8 @@ public class DynamicGridTreeUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
         {
             FilterPredicates[e.Column.MappingName] = Serialization.Serialize(e.FilterPredicates, true);
         }
+        
+        Parent.UIFilterChanged(this);
 
         UpdateRecordCount();
     }
@@ -591,7 +612,7 @@ public class DynamicGridTreeUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
         {
             filterstyle.Setters.Add(new Setter(Control.BackgroundProperty, DynamicGridUtils.FilterBackground));
             column.ImmediateUpdateColumnFilter = true;
-            column.ColumnFilter = ColumnFilter.Value;
+            column.ColumnFilter = ColumnFilter.DisplayText;
             column.AllowBlankFilters = true;
             column.AllowSorting = isactioncolumn
                 ? false
@@ -646,7 +667,7 @@ public class DynamicGridTreeUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
                     newcol.ColumnSizer = TreeColumnSizer.None;
                     newcol.HeaderText = column.HeaderText;
 
-                    ApplyFilterStyle(newcol, true, true);
+                    ApplyFilterStyle(newcol, false, true);
 
                     newcol.ShowToolTip = column.ToolTip != null;
                     newcol.ShowHeaderToolTip = column.ToolTip != null;
@@ -710,9 +731,11 @@ public class DynamicGridTreeUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
                     newcol.Width = column.Width;
                     newcol.ColumnSizer = TreeColumnSizer.None;
                     newcol.HeaderText = column.HeaderText;
-                    newcol.AllowFiltering = column.Filters != null && column.Filters.Any();
+                    //newcol.AllowFiltering = column.Filters != null && column.Filters.Any();
                     newcol.AllowSorting = false;
                     newcol.ShowHeaderToolTip = column.ToolTip != null;
+                    
+                    ApplyFilterStyle(newcol, false, true);
 
                     var headstyle = new Style(typeof(TreeGridHeaderCell));
                     headstyle.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Gainsboro)));
@@ -743,10 +766,12 @@ public class DynamicGridTreeUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
                     newcol.Width = tmplCol.Width;
                     newcol.ColumnSizer = TreeColumnSizer.None;
                     newcol.HeaderText = column.HeaderText;
-                    newcol.AllowFiltering = false;
+                    //newcol.AllowFiltering = false;
                     newcol.AllowSorting = false;
                     newcol.ShowToolTip = false;
                     newcol.ShowHeaderToolTip = false;
+                    
+                    ApplyFilterStyle(newcol, false, true);
 
                     var headstyle = new Style(typeof(TreeGridHeaderCell));
                     headstyle.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Gainsboro)));
@@ -1000,7 +1025,7 @@ public class DynamicGridTreeUIComponent<T> : IDynamicGridUIComponent<T>, IDynami
 
     public CoreRow[] GetVisibleRows()
     {
-        return _tree.View.Nodes.Select(x => MapRow((x.Item as CoreTreeNode)?.Row)).NotNull().ToArray();
+        return _tree.View?.Nodes.Select(x => MapRow((x.Item as CoreTreeNode)?.Row)).NotNull().ToArray() ?? new CoreRow[] { };
     }
 
     public void InvalidateRow(CoreRow row)

+ 3 - 1
inabox.wpf/DynamicGrid/UIComponent/IDynamicGridUIComponent.cs

@@ -42,6 +42,8 @@ public interface IDynamicGridUIComponentParent<T> : IDynamicGrid<T>
     void DragOver(object sender, DragEventArgs e);
     void Drop(object sender, DragEventArgs e);
     void DragStart(object? sender, CoreRow[] rows);
+
+    void UIFilterChanged(object sender);
 }
 
 public interface IDynamicGridUIComponent<T>
@@ -50,7 +52,7 @@ public interface IDynamicGridUIComponent<T>
     IDynamicGridUIComponentParent<T> Parent { set; }
 
     FrameworkElement Control { get; }
-
+    
     CoreRow[] SelectedRows { get; set; }
 
     double RowHeight { get; set; }