Grid filter windows size

Dear Syncfusion-Team,

is it possible to make the filter window resizable? Or maybe match the width of the column?




4 Replies

MS Monisha Saravanan Syncfusion Team August 30, 2022 02:03 PM UTC

Hi Szoke,


Greetings from Syncfusion support.


Query: “is it possible to make the filter window resizable? Or maybe match the width of the column?”


We don’t have support to resize the filter window. Instead we suggest you to achieve your requirement by applying below CSS based on conditions. Kindly check the attached sample and code snippet for your reference.



 

<SfGrid ID= "Grid" DataSource="@Orders" AllowFiltering="true">

    <GridEvents OnActionBegin="OnActionBegin" TValue="Order"></GridEvents>

  <GridFilterSettings Type ="Syncfusion.Blazor.Grids.FilterType.Excel"></GridFilterSettings>

   <GridColumns>

….

   </GridColumns>

</SfGrid>

 

@if(IsLarge)

{

<style>

    #Grid .e-excelfilter.e-popup.e-popup-open {

      

        width: 350px !important;

    }

    </style>

}

@if(IsSmall)

{

     <style>

    #Grid .e-excelfilter.e-popup.e-popup-open {

      

        width: 120px !important;

    }

    </style>

   

}

 

 

@code{

    public List<Order> Orders { get; set; }

    public bool IsLarge { get; set; } = false;

    public bool IsSmall { get; set; } = false;

    public void OnActionBegin(ActionEventArgs<Order> Args)

    {

        if (Args.RequestType == Syncfusion.Blazor.Grids.Action.FilterBeforeOpen)

        {

            if (Args.ColumnName == "CustomerID")

            {

                IsLarge = true;

                IsSmall = false;

            }

           

            else

            {

                IsLarge = false;

                IsSmall = false;

 

            }

        }

    }

 

}


Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/BlazorApp11387933525.zip


Kindly get back to us if you have further queries.


Regards,

Monisha



SZ Szoke replied to Monisha Saravanan March 23, 2023 12:31 PM UTC

In this way, we can increase the width of the window, but the text filter does not follow:


filter.png


Thank you in advance for your help!





PS Prathap Senthil Syncfusion Team March 27, 2023 04:47 AM UTC

Hi Szoke,


We have checked your query and we suggest you to use the below highlighted code snippet to increase the width of the context menu along with the excel dialog filter popup. Kindly check the attached code snippet for your reference.


@if (IsLarge)

 

{

 

    <style>

 

        #Grid .e-excelfilter.e-popup.e-popup-open {

            width: 350px !important;

        }

 

        .e-excelfilter .e-contextmenu-container ul:not(.e-ul){

            max-width: 350px;

        }

 

    </style>

 

}

 

 


Please let us know if you have any concerns.




AN Andrey November 3, 2025 09:59 AM UTC

Solution: Dynamic SfGrid Filter Popup Width Based on Column Width


I've implemented a solution that dynamically adjusts sfGrid filter popup width and position based on the actual column width. This works even when columns are reordered and handles viewport overflow gracefully.


Complete Working Example:


FilterPopupExample.razor
  
@page "/filter-popup-example"
  @implements IDisposable
  @inject IJSRuntime JSRuntime


  <PageTitle>Filter Popup Width Example</PageTitle>


  <h3>Dynamic Filter Popup Width Demo</h3>


  <SfGrid @ref="Grid" DataSource="@Employees" AllowFiltering="true" AllowReordering="true">
      <GridFilterSettings Type="Syncfusion.Blazor.Grids.FilterType.Excel"></GridFilterSettings>
      <GridColumns>
          <GridColumn Field="@nameof(Employee.EmployeeID)" HeaderText="ID" Width="100"></GridColumn>
          <GridColumn Field="@nameof(Employee.Name)" HeaderText="Name" Width="200"></GridColumn>
          <GridColumn Field="@nameof(Employee.Email)" HeaderText="Email" Width="300"></GridColumn>
          <GridColumn Field="@nameof(Employee.Department)" HeaderText="Department" Width="150"></GridColumn>
          <GridColumn Field="@nameof(Employee.Salary)" HeaderText="Salary" Width="120"></GridColumn>
      </GridColumns>
      <GridEvents TValue="Employee" OnActionBegin="OnActionBegin"></GridEvents>
  </SfGrid>


  @if (FilterPopupWidth.HasValue && FilterPopupLeft.HasValue)
  {
      <style>
          .e-dialog.e-filter-popup.e-excelfilter.e-popup.e-popup-open {
              width: @(FilterPopupWidth.Value)px !important;
              left: @(FilterPopupLeft.Value)px !important;
          }


          .e-dialog.e-filter-popup.e-excelfilter .e-contextmenu-container ul:not(.e-ul) {
              max-width: @(FilterPopupWidth.Value)px !important;
          }
      </style>
  }


  @code {
      private SfGrid<Employee> Grid;
      private List<Employee> Employees = new();
      private IJSObjectReference _jsModule;


      public int? FilterPopupWidth { get; set; }
      public int? FilterPopupLeft { get; set; }


      protected override void OnInitialized()
      {
          // Sample data
          Employees = new List<Employee>
          {
              new Employee { EmployeeID = 1, Name = "John Doe", Email = "[email protected]", Department =
  "IT", Salary = 75000 },
              new Employee { EmployeeID = 2, Name = "Jane Smith", Email = "[email protected]", Department =
   "HR", Salary = 65000 },
              new Employee { EmployeeID = 3, Name = "Bob Johnson", Email = "[email protected]", Department
   = "Sales", Salary = 55000 },
              new Employee { EmployeeID = 4, Name = "Alice Williams", Email = "[email protected]",
  Department = "IT", Salary = 80000 },
              new Employee { EmployeeID = 5, Name = "Charlie Brown", Email = "[email protected]",
  Department = "Marketing", Salary = 60000 }
          };
      }


      protected override async Task OnAfterRenderAsync(bool firstRender)
      {
          if (firstRender)
          {
              try
              {
                  _jsModule = await JSRuntime.InvokeAsync<IJSObjectReference>("import",
  "./Pages/FilterPopupExample.razor.js");
              }
              catch (Exception ex)
              {
                  Console.WriteLine($"Failed to load JS module: {ex.Message}");
              }
          }
      }


      public async void OnActionBegin(ActionEventArgs<Employee> args)
      {
          if (args.RequestType == Syncfusion.Blazor.Grids.Action.FilterBeforeOpen)
          {
              FilterPopupWidth = null;
              FilterPopupLeft = null;


              if (_jsModule != null && !string.IsNullOrEmpty(args.ColumnName))
              {
                  try
                  {
                      var gridId = Grid.ID;
                      var columnHeaderText = GetColumnHeaderText(args.ColumnName);


                      if (!string.IsNullOrEmpty(columnHeaderText))
                      {
                          var dimensions = await _jsModule.InvokeAsync<ColumnDimensions>("getColumnDimensions",
  gridId, columnHeaderText);


                          if (dimensions != null && dimensions.Width > 0)
                          {
                              // Calculate popup width: min 250px, max 800px, based on column width
                              var calculatedWidth = (int)Math.Ceiling(dimensions.Width);
                              FilterPopupWidth = Math.Max(250, Math.Min(800, calculatedWidth));


                              // Position popup aligned with column left edge (grid-relative)
                              var desiredLeft = (int)Math.Round(dimensions.LeftRelativeToGrid);
                              var rightEdgePadding = 20;


                              // Handle viewport overflow - shift popup left if needed
                              if (dimensions.Left + FilterPopupWidth.Value > dimensions.ViewportWidth -
  rightEdgePadding)
                              {
                                  var overflowAmount = (dimensions.Left + FilterPopupWidth.Value) -
  (dimensions.ViewportWidth - rightEdgePadding);
                                  desiredLeft = (int)(desiredLeft - overflowAmount);
                                  desiredLeft = Math.Max(10, desiredLeft); // Minimum 10px from left edge
                              }


                              FilterPopupLeft = desiredLeft;
                          }
                      }
                  }
                  catch (Exception ex)
                  {
                      Console.WriteLine($"Failed to get column dimensions: {ex.Message}");
                  }
              }


              StateHasChanged();
          }
      }


      private string GetColumnHeaderText(string fieldName)
      {
          // Map field names to header text
          return fieldName switch
          {
              nameof(Employee.EmployeeID) => "ID",
              nameof(Employee.Name) => "Name",
              nameof(Employee.Email) => "Email",
              nameof(Employee.Department) => "Department",
              nameof(Employee.Salary) => "Salary",
              _ => fieldName
          };
      }


      public void Dispose()
      {
          _jsModule?.DisposeAsync();
      }


      public class Employee
      {
          public int EmployeeID { get; set; }
          public string Name { get; set; }
          public string Email { get; set; }
          public string Department { get; set; }
          public decimal Salary { get; set; }
      }


      public class ColumnDimensions
      {
          public double Width { get; set; }
          public double Left { get; set; } // Viewport-relative
          public double LeftRelativeToGrid { get; set; } // Grid-relative
          public double ViewportWidth { get; set; }
      }
  }


 FilterPopupExample.razor.js:

 export function getColumnDimensions(gridId, columnHeaderText) {
      try {
          const gridElement = document.getElementById(gridId);
          if (!gridElement) {
              console.warn(`Grid element not found: ${gridId}`);
              return null;
          }


          // Find header cell by matching header text (works even when columns are reordered)
          const headerCells = gridElement.querySelectorAll('th.e-headercell');
          let headerCell = null;


          for (const cell of headerCells) {
              const headerTextSpan = cell.querySelector('.e-headertext');
              if (headerTextSpan && headerTextSpan.textContent.trim() === columnHeaderText) {
                  headerCell = cell;
                  break;
              }
          }


          if (!headerCell) {
              console.warn(`Header cell not found for column: ${columnHeaderText}`);
              return null;
          }


          // Get bounding rectangles
          const headerRect = headerCell.getBoundingClientRect();
          const gridRect = gridElement.getBoundingClientRect();


          // Calculate position relative to grid (for popup positioning)
          const leftRelativeToGrid = headerRect.left - gridRect.left;


          // Get viewport width for overflow detection
          const viewportWidth = window.innerWidth || document.documentElement.clientWidth;


          return {
              width: headerRect.width,
              left: headerRect.left, // Viewport-relative for overflow detection
              leftRelativeToGrid: leftRelativeToGrid, // Grid-relative for positioning
              viewportWidth: viewportWidth
          };
      } catch (error) {
          console.error('Error getting column dimensions:', error);
          return null;
      }
  }


 Key Features:


  1. Dynamic Width: Popup width adapts to column width (min 250px, max 800px)

  2. Proper Alignment: Popup aligns with column's left edge using grid-relative positioning

  3. Viewport Overflow Handling: Automatically shifts popup left when it would overflow the viewport

  4. Column Reordering Support: Finds columns by header text, not position, so it works after reordering

  5. Minimal Code: Uses dynamic CSS injection instead of modifying Syncfusion internals


  How It Works:

 

 1. When FilterBeforeOpen event fires, we get the column name from args.ColumnName

  2. JavaScript finds the header cell by matching the header text content

  3. Returns column dimensions (width and position) relative to both grid and viewport

  4. C# calculates appropriate popup width and position

  5. Dynamic CSS is injected to override Syncfusion's default popup styles


Hope this helps s


Loader.
Up arrow icon