Setting automatically column width of datagrid... any tip ?

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
titobal
Posts: 31
Joined: Thu Aug 06, 2009 1:51 pm

Setting automatically column width of datagrid... any tip ?

Post by titobal » Sun Aug 09, 2009 11:32 am

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

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
Joined: Sat Apr 08, 2006 3:06 pm
Contact:

Post by trevordevore » Wed Aug 12, 2009 4:02 pm

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

titobal
Posts: 31
Joined: Thu Aug 06, 2009 1:51 pm

Amazing ! Many Thanks !

Post by titobal » Wed Aug 12, 2009 9:37 pm

Amazing ! Many Thanks !

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Re: Setting automatically column width of datagrid... any tip ?

Post by BarrySumpter » Tue Aug 09, 2011 1:48 am

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.

LC4iOS
Posts: 88
Joined: Tue Dec 03, 2013 8:15 pm

Re: Setting automatically column width of datagrid... any ti

Post by LC4iOS » Mon Dec 16, 2013 6:22 am

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. :oops:


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

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Setting automatically column width of datagrid... any ti

Post by Klaus » Mon Dec 16, 2013 1:54 pm

Quick question: Do you also EXECUTE your "routineFigureOutWidth" handler at any time? 8)

LC4iOS
Posts: 88
Joined: Tue Dec 03, 2013 8:15 pm

Re: Setting automatically column width of datagrid... any ti

Post by LC4iOS » Mon Dec 16, 2013 9:47 pm

Only if you want it to work.
:mrgreen:
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

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Setting automatically column width of datagrid... any ti

Post by Klaus » Mon Dec 16, 2013 10:20 pm

Ah, sorry, I had the expression that it did not work somehow 8)

LC4iOS
Posts: 88
Joined: Tue Dec 03, 2013 8:15 pm

Re: Setting automatically column width of datagrid... any ti

Post by LC4iOS » Tue Dec 17, 2013 10:34 am

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. :oops:

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

LC4iOS
Posts: 88
Joined: Tue Dec 03, 2013 8:15 pm

Re: Setting automatically column width of datagrid... any ti

Post by LC4iOS » Sun Dec 22, 2013 10:03 am

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

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

Post Reply