-
Notifications
You must be signed in to change notification settings - Fork 460
[DataGrid] Add AutoFit feature to size columns automatically as good as possible #2496
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
Conversation
Introduced the AutoFit property to the FluentDataGrid component, enabling columns to auto-fit the grid width using JavaScript. Updated XML documentation and added a new demo section in DataGridPage.razor to showcase this feature. Created DataGridAutoFit.razor component for demonstration purposes. Modified FluentDataGrid.razor and FluentDataGrid.razor.cs to implement AutoFit functionality, including invoking a JavaScript function to adjust column widths. Simplified SortByColumnAsync methods using ternary operators. Updated CSS in FluentDataGrid.razor.css and FluentDataGridCell.razor.css to support AutoFit, ensuring proper styling and visibility of grid cells. Added autoFitGridColumns JavaScript function in FluentDataGrid.razor.js. Removed redundant CSS rules and adjusted formatting for consistency.
|
Can you explain how do you compute the column width? And why do you use a JavaScript code to do that, and why not use the Flex `auto' property? One problem with using a JS is there are some side effects, like "the columns are not resized with the browser resize" or "the column sizes are recalculated when the user clicks on the col header to sort", ... |
My skill in CSS is limited, so I don't know much about Flex or other CSS abilities which is why I didn't go that route. Also, there is the Here's how my solution works: I start by overriding some CSS with the Here's the JS pseudo code:
Scenario:
For me, this was a simple way to make that first render better. This could be broadened to execute on resize pretty easily. I don't think it's too necessary on sort/pagination as the content typically would be similar in length. This also does not interfere with column resizing by the UI. It simply sets the widths, and the user can resize columns as they see fit. I implemented this in my project and thought the community could start with this and make it better. If you, or anyone else, has suggestions, I'm happy to learn and would apply changes that make it better. |
|
That's interesting, I think you should add this explanation above the examples, to explain how these column widths are calculated:
Next, you need to find a way to avoid "duplicating the configuration": you should only use <FluentDataGrid Items="@people" GridTemplateColumns="auto-fit" AutoFit="true">I also wonder how this "calculation with cells temporarily rendered invisible" functionality will behave on platforms such as MAUI, or with the Virtualization feature. Could you check these two cases? On the other hand, there's the fit-content attrribute which might help. But I don't think it would be a viable solution. |
|
Other points.
|
I tested with virtualization and, no surprise, it doesn't work there. I've never done a MAUI project so I can't test quickly there. If I have time I can try. Maybe for now just note that it's untested in MAUI? One thing I noticed with EDIT: I also removed the need for double configuration. I didn't like that either. Should we not allow the |
As I was addressing this, I realized that there was no need to calculate the ratio and all that mess. Basically the |
Updated DataGridPage.razor to correct a typo and provide additional information about the AutoFit parameter. Removed GridTemplateColumns="auto-fit" from DataGridAutoFit.razor as it is now handled internally. Modified OnParametersSetAsync and OnAfterRenderAsync in FluentDataGrid.razor.cs to support AutoFit. Improved autoFitGridColumns function in FluentDataGrid.razor.js.
…d the `Parameter` attribute for `LoadingContent`.
…as possible (microsoft#2496) * Add AutoFit feature to FluentDataGrid component Introduced the AutoFit property to the FluentDataGrid component, enabling columns to auto-fit the grid width using JavaScript. Updated XML documentation and added a new demo section in DataGridPage.razor to showcase this feature. Created DataGridAutoFit.razor component for demonstration purposes. Modified FluentDataGrid.razor and FluentDataGrid.razor.cs to implement AutoFit functionality, including invoking a JavaScript function to adjust column widths. Simplified SortByColumnAsync methods using ternary operators. Updated CSS in FluentDataGrid.razor.css and FluentDataGridCell.razor.css to support AutoFit, ensuring proper styling and visibility of grid cells. Added autoFitGridColumns JavaScript function in FluentDataGrid.razor.js. Removed redundant CSS rules and adjusted formatting for consistency. * Provide better comment for the AutoFit feature and fix merge conflict artifacts. * Add AutoFit parameter to FluentDataGrid component Updated DataGridPage.razor to correct a typo and provide additional information about the AutoFit parameter. Removed GridTemplateColumns="auto-fit" from DataGridAutoFit.razor as it is now handled internally. Modified OnParametersSetAsync and OnAfterRenderAsync in FluentDataGrid.razor.cs to support AutoFit. Improved autoFitGridColumns function in FluentDataGrid.razor.js. * Place `Parameter` attribute on separate line for `AutoFit`. Also fixed the `Parameter` attribute for `LoadingContent`. --------- Co-authored-by: Kevin Becker <[email protected]> Co-authored-by: Denis Voituron <[email protected]> Co-authored-by: Vincent Baaij <[email protected]>

Pull Request
📖 Description
Introduced the AutoFit property to the FluentDataGrid component, enabling columns to auto-fit the grid width using JavaScript. Updated XML documentation and added a new demo section in DataGridPage.razor to showcase this feature. Created DataGridAutoFit.razor component for demonstration purposes.
Modified FluentDataGrid.razor and FluentDataGrid.razor.cs to implement AutoFit functionality, including invoking a JavaScript function to adjust column widths. Simplified SortByColumnAsync methods using ternary operators.
Updated CSS in FluentDataGrid.razor.css and FluentDataGridCell.razor.css to support AutoFit, ensuring proper styling and visibility of grid cells. Added autoFitGridColumns JavaScript function in FluentDataGrid.razor.js. Removed redundant CSS rules and adjusted formatting for consistency.
🎫 Issues
#2495
👩💻 Reviewer Notes
Not much to this.
📑 Test Plan
This is mostly client side code.
✅ Checklist
General
Component-specific
⏭ Next Steps