-
Notifications
You must be signed in to change notification settings - Fork 80
Added new kb article grid-kb-style-filtered-columns #2617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dimodi
merged 6 commits into
master
from
new-kb-grid-kb-style-filtered-columns-a758283dec3346e5970379897ec439e8
Dec 13, 2024
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0d09f54
Added new kb article grid-kb-style-filtered-columns
f69558f
Update knowledge-base/grid-kb-style-filtered-columns.md
dimodi 5cd4f61
Update knowledge-base/grid-kb-style-filtered-columns.md
dimodi b6c9ff5
Update knowledge-base/grid-kb-style-filtered-columns.md
dimodi 17d9b37
Update knowledge-base/grid-kb-style-filtered-columns.md
dimodi f2fb36a
kb(grid): Polish KB
dimodi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| --- | ||
| title: Change Filtered Columns Color in Grid for Blazor | ||
| description: Learn how to change the background color of filtered columns in the Grid for Blazor for better visibility using CSS. | ||
| type: how-to | ||
| page_title: How to Style Filtered Columns in Telerik Grid for Blazor | ||
| slug: grid-kb-style-filtered-columns | ||
| tags: grid, blazor, styling, filtered columns, css, oncellrender, onstatechanged | ||
dimodi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| res_type: kb | ||
| ticketid: 1672604 | ||
| --- | ||
|
|
||
| ## Environment | ||
|
|
||
| <table> | ||
| <tbody> | ||
| <tr> | ||
| <td>Product</td> | ||
| <td>Grid for Blazor</td> | ||
| </tr> | ||
| </tbody> | ||
| </table> | ||
|
|
||
| ## Description | ||
|
|
||
| I want to style filtered columns in the Grid for Blazor using RGBA for better visibility. Specifically, when a filter is applied, I want the background color of column cells to change. | ||
|
|
||
| This knowledge base article answers the following questions: | ||
| - How to apply custom styles to filtered columns in a Blazor Grid? | ||
| - How to enhance the visibility of filtered columns in Blazor Grid using CSS? | ||
|
|
||
| ## Solution | ||
|
|
||
| To style filtered columns in a Telerik Grid for Blazor: | ||
| 1. Utilize the [`OnCellRender`]({%slug grid-column-events%}#oncellrender) and [`OnStateChanged`]({%slug grid-state%}#onstatechanged) events | ||
| 2. Apply a custom CSS class to the filtered column when a filter is active | ||
|
|
||
| >caption Grid with styled filtered column. | ||
|
|
||
| `````CSHTML | ||
| @using Telerik.DataSource.Extensions | ||
| @using Telerik.DataSource | ||
dimodi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| <style> | ||
| .highlighted-column { | ||
| background-color: rgba(255, 0, 0, 0.2); /* Change to your desired RGBA */ | ||
| color: #191970; | ||
| } | ||
| </style> | ||
|
|
||
| <TelerikGrid TItem="@Employee" | ||
| OnRead="@ReadItems" | ||
dimodi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| FilterMode="@GridFilterMode.FilterRow" | ||
| Sortable="true" | ||
| Pageable="true" | ||
| OnStateChanged="@OnGridStateChanged"> | ||
| <GridColumns> | ||
| <GridColumn Field="@nameof(Employee.ID)" Filterable="false" Title="ID" /> | ||
| <GridColumn Field="@nameof(Employee.Name)" OnCellRender="@OnNameColumnCellRender" Title="Name" /> | ||
| <GridColumn Field="@nameof(Employee.HireDate)" OnCellRender="@OnHireDateColumnCellRender" Title="Hire Date" /> | ||
| </GridColumns> | ||
| </TelerikGrid> | ||
|
|
||
| @code { | ||
| private List<Employee> SourceData { get; set; } | ||
| private string FilteredColumn { get; set; } | ||
|
|
||
| private void OnNameColumnCellRender(GridCellRenderEventArgs args) | ||
| { | ||
| if (FilteredColumn == nameof(Employee.Name)) | ||
| { | ||
| args.Class = "highlighted-column"; // Apply the custom CSS class | ||
| } | ||
| } | ||
|
|
||
| private void OnHireDateColumnCellRender(GridCellRenderEventArgs args) | ||
| { | ||
| if (FilteredColumn == nameof(Employee.HireDate)) | ||
| { | ||
| args.Class = "highlighted-column"; // Apply the custom CSS class | ||
| } | ||
| } | ||
|
|
||
| private async Task OnGridStateChanged(GridStateEventArgs<Employee> args) | ||
| { | ||
| if (args.PropertyName == "FilterDescriptors") | ||
| { | ||
| if (args.GridState.FilterDescriptors.Any()) | ||
| { | ||
| var filterDescriptors = new List<IFilterDescriptor>(args.GridState.FilterDescriptors); | ||
| var filterDescriptor = filterDescriptors[0] as CompositeFilterDescriptor; | ||
| var filteredColumn = filterDescriptor.FilterDescriptors[0] as FilterDescriptor; | ||
|
|
||
| // Capture the filtered column's field name | ||
| FilteredColumn = filteredColumn.Member; | ||
dimodi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| else | ||
| { | ||
| FilteredColumn = null; // Reset when no filters are applied | ||
| } | ||
|
|
||
| StateHasChanged(); // Re-render the grid | ||
| } | ||
| } | ||
|
|
||
| protected async Task ReadItems(GridReadEventArgs args) | ||
| { | ||
| // Simulate network delay | ||
| await Task.Delay(100); | ||
|
|
||
| var datasourceResult = SourceData.ToDataSourceResult(args.Request); | ||
|
|
||
| args.Data = datasourceResult.Data; | ||
| args.Total = datasourceResult.Total; | ||
| } | ||
|
|
||
| protected override void OnInitialized() | ||
| { | ||
| SourceData = GenerateData(); | ||
| } | ||
|
|
||
| private List<Employee> GenerateData() | ||
| { | ||
| var result = new List<Employee>(); | ||
| var rand = new Random(); | ||
dimodi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for (int i = 0; i < 100; i++) | ||
dimodi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| result.Add(new Employee() | ||
| { | ||
| ID = i, | ||
| Name = "Name " + i, | ||
| HireDate = DateTime.Now.Date.AddDays(rand.Next(-20, 20)) | ||
dimodi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| public class Employee | ||
| { | ||
| public int ID { get; set; } | ||
| public string Name { get; set; } | ||
| public DateTime HireDate { get; set; } | ||
| } | ||
| } | ||
| ````` | ||
| ## See Also | ||
|
|
||
| - [Grid Columns](https://docs.telerik.com/blazor-ui/components/grid/columns/bound) | ||
| - [OnCellRender Event](https://docs.telerik.com/blazor-ui/components/grid/columns/events) | ||
| - [OnStateChanged Event](https://docs.telerik.com/blazor-ui/components/grid/state#onstatechanged) | ||
| - [Override the Theme or Apply Custom CSS Styles]({%slug themes-override%}) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.