Setting automatically column width of datagrid... any tip ?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Setting automatically column width of datagrid... any tip ?
Hi,
I would like to set automatically the columns widths of a datagrid
in function of the length of the texts/contents which are located in each cells,
any tip on how to perform that ?
Thanks
I would like to set automatically the columns widths of a datagrid
in function of the length of the texts/contents which are located in each cells,
any tip on how to perform that ?
Thanks
-
- VIP Livecode Opensource Backer
- Posts: 1005
- Joined: Sat Apr 08, 2006 3:06 pm
- Contact:
You will have to do this by hand. You could do something like this to calculate the widest text in each column and update the column widths in the data grid. Note that the code has not been tested and you must have a field named "FigureOutWidth" on the card that contains this script.
Code: Select all
local theColWidthsA ## when all done has a key for each column name. Value of key is max width.
put the dgData of group "DataGrid" into theDataA
put the dgIndexes of group "DataGrid" into theIndexes
put the dgProps["columns"] of group "DataGrid" into theColumns
## Loop through all records
repeat for each item theIndex in theIndexes
## Loop through each column of record
repeat for each line theColumn in theColumns
## Figure out width of this record's column
set the text of field "FigureOutWidth" to theDataA[theIndex][theColumn]
put max(the formattedwidth of field "FigureOutWidth", theColWidthsA[theColumn]) into theColWidthsA[theColumn]
end repeat
end repeat
## Reset field
set the text of field "FigureOutWidth" to empty
## Set column widths
repeat for each line theColumn in theColumns
put theColWidthsA[theColumn] & comma after theWidths
end repeat
delete the last char of theWidths
set the dgProps["column widths"] of group "DataGrid" to theWidths
## output max widths for testing purposes
repeat for each line theColumn in theColumns
put "max width for column" && theColumn & ":" && theColWidthsA[theColumn] & cr after theText
end repeat
put theText
Trevor DeVore
ScreenSteps - https://www.screensteps.com
LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder
ScreenSteps - https://www.screensteps.com
LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder
Amazing ! Many Thanks !
Amazing ! Many Thanks !
-
- Posts: 1201
- Joined: Sun Apr 24, 2011 2:17 am
Re: Setting automatically column width of datagrid... any tip ?
Nice!
All my best,
Barry G. Sumpter
Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.
Barry G. Sumpter
Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.
Re: Setting automatically column width of datagrid... any ti
Lovely.
Was just looking thru the pop library demo and have added a DataGrid following my first data grid sample lesson from RunRev.
Thanks RR
And wanted something just like this.
Thanks for posting that routine Trevor
But I don't know why the instructions are so hard to read for me.
Place a DataGrid control on a card.
Place a field named fldFigureOutWidth on that same card.
Place a routine named routineFigureOutWidth within that same cards Card Script.
Copy and paste the code above into the routine named routineFigureOutWidth.
Was just looking thru the pop library demo and have added a DataGrid following my first data grid sample lesson from RunRev.
Thanks RR
And wanted something just like this.
Thanks for posting that routine Trevor
But I don't know why the instructions are so hard to read for me.

Place a DataGrid control on a card.
Place a field named fldFigureOutWidth on that same card.
Place a routine named routineFigureOutWidth within that same cards Card Script.
Copy and paste the code above into the routine named routineFigureOutWidth.
Thanks to RunRev.
Thanks to LiveCode forum members.
LiveCode v5.5.5 - iOS Android Mac Windows - 6.5 Community
27" 2012 iMac i5, MacBook Pro, MacBook Air, iPhone 5, iPhone 4
xCode 5.0.2 - iOS7 - OS X Mavericks
Paid Apple iOS Developer Program Member
Thanks to LiveCode forum members.
LiveCode v5.5.5 - iOS Android Mac Windows - 6.5 Community
27" 2012 iMac i5, MacBook Pro, MacBook Air, iPhone 5, iPhone 4
xCode 5.0.2 - iOS7 - OS X Mavericks
Paid Apple iOS Developer Program Member
Re: Setting automatically column width of datagrid... any ti
Quick question: Do you also EXECUTE your "routineFigureOutWidth" handler at any time? 

Re: Setting automatically column width of datagrid... any ti
Only if you want it to work.


Thanks to RunRev.
Thanks to LiveCode forum members.
LiveCode v5.5.5 - iOS Android Mac Windows - 6.5 Community
27" 2012 iMac i5, MacBook Pro, MacBook Air, iPhone 5, iPhone 4
xCode 5.0.2 - iOS7 - OS X Mavericks
Paid Apple iOS Developer Program Member
Thanks to LiveCode forum members.
LiveCode v5.5.5 - iOS Android Mac Windows - 6.5 Community
27" 2012 iMac i5, MacBook Pro, MacBook Air, iPhone 5, iPhone 4
xCode 5.0.2 - iOS7 - OS X Mavericks
Paid Apple iOS Developer Program Member
Re: Setting automatically column width of datagrid... any ti
Ah, sorry, I had the expression that it did not work somehow 

Re: Setting automatically column width of datagrid... any ti
Over the last few days I had read that line n kept getting confused.
Thought I had posted the script works perfectly.
Must have removed it from my numerous edits.
Was going to post a sample project
but got distracted trying to make a column color grey and a cell bold and red on the datagrid
on a simple table style (with no templates or custom behaviours).
The project is getting bit out of hand.
And needs a bit of clean up from my playing in me sandbox.
Project added.
Thought I had posted the script works perfectly.
Must have removed it from my numerous edits.

Was going to post a sample project
but got distracted trying to make a column color grey and a cell bold and red on the datagrid
on a simple table style (with no templates or custom behaviours).
The project is getting bit out of hand.
And needs a bit of clean up from my playing in me sandbox.
Project added.
- Attachments
-
- DataGridColumnWidths_20131229 1910.livecode.zip
- (8.13 KiB) Downloaded 262 times
Last edited by LC4iOS on Sun Dec 29, 2013 9:12 am, edited 2 times in total.
Thanks to RunRev.
Thanks to LiveCode forum members.
LiveCode v5.5.5 - iOS Android Mac Windows - 6.5 Community
27" 2012 iMac i5, MacBook Pro, MacBook Air, iPhone 5, iPhone 4
xCode 5.0.2 - iOS7 - OS X Mavericks
Paid Apple iOS Developer Program Member
Thanks to LiveCode forum members.
LiveCode v5.5.5 - iOS Android Mac Windows - 6.5 Community
27" 2012 iMac i5, MacBook Pro, MacBook Air, iPhone 5, iPhone 4
xCode 5.0.2 - iOS7 - OS X Mavericks
Paid Apple iOS Developer Program Member
Re: Setting automatically column width of datagrid... any ti
Weird a Time headed column of hh:mm:ss format keeps getting squashed.
Updated project.
But not really happy with logic.
Maybe the header font is different than the data font?
Another update.
the field FigureOutWidth has to be dontWrap
Updated project.
But not really happy with logic.
Maybe the header font is different than the data font?
Another update.
the field FigureOutWidth has to be dontWrap
Code: Select all
on mouseUp
local theColWidthsA ## when all done has a key for each column name. Value of key is max width.
put the dgData of group "DataGrid" into theDataA
--put the dgText[True] of group "DataGrid" into theDataA
put the dgIndexes of group "DataGrid" into theIndexes
put the dgProps["columns"] of group "DataGrid" into theColumns
## Loop through all records
repeat for each item theIndex in theIndexes
## Loop through each column of record
repeat for each line theColumn in theColumns
## Figure out width of this record's column
if the dgColumnLabel[theColumn] of group "DataGrid" is empty then
set the text of field "FigureOutWidth" to theColumn
else
set the text of field "FigureOutWidth" to the dgColumnLabel[theColumn] of group "DataGrid"
end if
put the formattedwidth of field "FigureOutWidth" onto theHeaderWidth
set the text of field "FigureOutWidth" to theDataA[theIndex][theColumn]
put the formattedwidth of field "FigureOutWidth" onto theCellWidth
if theHeaderWidth > theCellWidth then
put round(theHeaderWidth * 1.5) into theHeaderWidth
end if
if theColumn = "chk" then
put 0 into theHeaderWidth
end if
put max(theHeaderWidth, theCellWidth, theColWidthsA[theColumn]) into theColWidthsA[theColumn]
-- put max(the formattedwidth of field "FigureOutWidth", theColWidthsA[theColumn]) into theColWidthsA[theColumn]
end repeat
end repeat
## Reset field
set the text of field "FigureOutWidth" to empty
## Set column widths
repeat for each line theColumn in theColumns
put theColWidthsA[theColumn] & comma after theWidths
end repeat
delete the last char of theWidths
set the dgProps["column widths"] of group "DataGrid" to theWidths
## output max widths for testing purposes
repeat for each line theColumn in theColumns
put "max width for column" && theColumn & ":" && theColWidthsA[theColumn] & cr after theText
end repeat
put theText
end mouseUp
Thanks to RunRev.
Thanks to LiveCode forum members.
LiveCode v5.5.5 - iOS Android Mac Windows - 6.5 Community
27" 2012 iMac i5, MacBook Pro, MacBook Air, iPhone 5, iPhone 4
xCode 5.0.2 - iOS7 - OS X Mavericks
Paid Apple iOS Developer Program Member
Thanks to LiveCode forum members.
LiveCode v5.5.5 - iOS Android Mac Windows - 6.5 Community
27" 2012 iMac i5, MacBook Pro, MacBook Air, iPhone 5, iPhone 4
xCode 5.0.2 - iOS7 - OS X Mavericks
Paid Apple iOS Developer Program Member