data grid, custom sorting

Shao Sean shaosean at wehostmacs.com
Sat May 2 22:05:22 EDT 2009


Okay finally got it all figured out.. Seems the documentation could  
use a little work (perhaps one of the people who have written books  
before could tackle this ;-)

When sorting a list of files by size I thought it would be nice to  
display the sizes in more readable format (5.6 MB, 320 KB, etc) yet  
doing a sort on the size column in the data grid would sort properly  
for the numbers but would not be sorted correctly for the actual  
sizes. Rooting around in the data grid parentScript I came across  
"sortDataByKey" which allows you sort based on the actual data in the  
dgData instead of what is displayed (1024 instead of 1 KB) - that was  
the easy part.

The hard part, which took all of four hours and one kernel panic  
(which is my fourth one with the data grid), I managed to get the  
column headers correctly hiliting (the documentation clearly point  
this out as dgHilite so not too hard to figure out what command to  
use). What the docs clearly miss though is that this is not a  
property of the data grid (which would make it much easier to use  
[feature request :) ]) but a property of the column header group so  
you need to call it by its medium name (not a real feature of Rev).

tl;dr

1. replace "size" with your column
2. place this code in your data grid group script
3. ????
4. profit!



command sortDataGridColumn pColumn
   constant kColumnSize = "size"
   local tOldSortKey
   lock screen

   switch (pColumn)
     case "size"
       -- turn off the hilite for the currently hilited column
       put the dgProps["sort by column"] of me into tOldSortKey
       set the dgHilite of group tOldSortKey of group "dgHeader" to  
FALSE

       -- sort our column by the data in the array instead of what is  
displayed
       if (the dgColumnSortDirection[kColumnSize] of me =  
"ascending") then
         set the dgColumnSortDirection[kColumnSize] of me to  
"descending"
         sortDataByKey kColumnSize, "numeric", "descending", FALSE
         --@ sortDataByKey <columnName>, <sortType>, <sortDirection>,  
<caseSensitive>
       else
         set the dgColumnSortDirection[kColumnSize] of me to "ascending"
         sortDataByKey kColumnSize, "numeric", "ascending", FALSE
       end if
       -- turn on the hilite for our custom sorted column
       set the dgHilite of group kColumnSize of group "dgHeader" to TRUE
       break
     default
       -- turn off the hilite for our custom sorted column
       set the dgHilite of group kColumnSize of group "dgHeader" to  
FALSE
       pass sortDataGridColumn
       break
   end switch
end sortDataGridColumn



More information about the use-livecode mailing list