646
Views
27
Comments
Solved
[OutSystems Data Grid] How to auto-fit columns in DataGrid?
outsystems-data-grid
Reactive icon
Forge asset by OutSystems
Application Type
Reactive

Hi,

In DataGrid, upon loading, the column's width is like this:

But upon load, I want it to auto-fit, something like this

How do I achieve this? Thank you!

2020-08-05 09-00-16
Gabriel Lundgren
Solution

Hey,


I noticed that you did not implement the JS snippet I sent earlier. That's why it's not working.

AutoFit.oml
2020-08-05 09-00-16
Gabriel Lundgren

Hello @Kenneth1,


On the Grid's on initialize event, you can call the following snippet of JS to achieve this. This will make the first column (index 0) have auto fit.

GridAPI.GridManager.GetGridById($parameters.GridWidgetId).provider.autoSizeColumn(0);


 If you want all columns to auto fit, you can call:

GridAPI.GridManager.GetGridById($parameters.GridWidgetId).provider.autoSizeColumns();


I've attached a sample where you can see it working. We'll also analyze this internally and see if it makes sense for us to create a client action for this.

AutoFit.oml
UserImage.jpg
Kenneth Alberto

I've tried your solution but I guess it autofits only based on the Column Header name, so if I have a cell value that is very long it shows ellipsis, is it possible to show the whole value?

2020-08-05 09-00-16
Gabriel Lundgren

Are you sure?


This is the result from the sample I sent you.

UserImage.jpg
Kenneth Alberto
UserImage.jpg
Kenneth Alberto

It's kinda buggy, sometimes when I refresh page it autofits correctly, sometimes its not (with ellipsis)

2020-08-05 09-00-16
Gabriel Lundgren

I've tried reproducing the issue you mentioned, but wasn't able to. Did you change anything on your sample?

UserImage.jpg
Kenneth Alberto

Nothing. Just trying out the the autoSizeColumn(0) and autoSizeColumns();

the autoSizeColumns() also does the ellipsis thing.


2023-11-01 07-27-06
Dhanushant Bishnoi

Hello Gabriel Lundgren and Outsystem developers,

The default serial number (at the start of grid) can we apply  auto fit content on the serial numbers as well.
Thanks and Regards
Dhanushant

UserImage.jpg
Daniele Betti

I confirm the same issue!

2022-11-12 11-28-30
Gonçalo Martins
Staff

Hi @Kenneth1 

Could you please share the oml where we could reproduce that and mention the Data Grid version?

Thanks

UserImage.jpg
Kenneth Alberto

This is the oml (the one you shared), Data grid version is 2.6.3

AutoFit.oml
UserImage.jpg
Daniele Betti

Mine is 2.6.2 and I reproduced the same behaviour with the oml just shared by @Kenneth1 .

It is enough to refresh the page several times to get the wrnog behaviour.

I tested it also with another application with the same result.

2020-08-05 09-00-16
Gabriel Lundgren

Hey,


Please update your JS node as follows:

setTimeout(function(){

    GridAPI.GridManager.GetGridById($parameters.GridWidgetId).provider.autoSizeColumn(0);

}, 0);


Please let me know if this helps!

UserImage.jpg
Daniele Betti

Nope :-( Same issue

2022-11-12 11-28-30
Gonçalo Martins
Staff

Hello.
I tried with the latest version and I'm not able to reproduce that use case.
Have you tried the latest version?

UserImage.jpg
Daniele Betti

Updated to 2.7.0 but same behavior

notAutoSize.png
2025-08-07 06-30-56
Amit J
Champion

Hi,

It is really strange, But It is working fine.


UserImage.jpg
Daniele Betti

I don't know how to help in reproducing the issue, I used your oml... just refresh the page several times and then I get the issue..

UserImage.jpg
Daniele Betti

Please check the behaviour at https://personal-7bxoeiru.outsystemscloud.com/AutoFit/Grid?_ts=637919512389921254

Try to refresh several times and you'll get the wrong auto-size.

2020-08-05 09-00-16
Gabriel Lundgren
Solution

Hey,


I noticed that you did not implement the JS snippet I sent earlier. That's why it's not working.

AutoFit.oml
UserImage.jpg
Daniele Betti

Sorry, I published an old oml.

Now I published the last oml you sent with the correct snippet... same issue: after two or three refresh the size is fitted to the headers.

Give it a try: https://personal-7bxoeiru.outsystemscloud.com/AutoFit/Grid?_ts=637919615607416074



UserImage.jpg
Praveen kadbhane

i tried same API working fine but this make my page laggy and slow any specific reason?

2022-11-12 11-28-30
Gonçalo Martins
Staff

Hi @Praveen kadbhane 

Which version are you using? Can you please share a sample oml and explain better what do you mean by the page being slow (how slow, when that happens, the volume of data and in which situations)? 

2023-04-11 13-26-29
Reinaldo Macedo Soares Pimentel

I can share with you guys that using the latest version of Datagrid, this is easily done by creating a client action with a JS widget with the following code: 

OutSystems.GridAPI.GridManager.GetGridById($parameters.GridId).provider.autoSizeColumns();

In the DataGrid properties pane, at the bottom, there is an Event called OnInitialize. Just invoke that client action there and every time your data changes the columns will have auto width.

2024-09-05 09-13-06
Gurjeet Singh Bhatia

Hi, 

I have used bellow JS but this is adjusting columns height for first page only... if i move to 2nd or 3rd page columns are not getting adjusted based on the content.

OutSystems.GridAPI.GridManager.GetGridById($parameters.ridWidgetId).provider.autoSizeColumns();

Any solution to fix this issue?

UserImage.jpg
Peter Delrue

Same issue here, all the latest version but mostly only adjusting to colums headers.

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.