Using a check box in a datagrid column header

zryip theSlug zryip.theslug at gmail.com
Thu Dec 1 18:16:29 EST 2011


On Thu, Dec 1, 2011 at 7:06 PM, Glen Bojsza <gbojsza at gmail.com> wrote:
> I was wondering if it is possible to use a check box  as a column header
> for a datagrid.
>
> This is where the user can select or deselect all lines in the datagrid
> instead of having to do it on the individual basis.

Theoretically the datagrid library contains code for managing an
header template group.
Unfortunately, this part seems unfinished and some code missing for
having something basically functional.

The question was asked in the developer list, so in case it could be
useful for someone in the list, here is the replied I sent month ago:

<quote>
1) In the row template group, you have to add a group named with the
column name + space + "[Header]".
For example to have a customized header for a column "First col", you
must create a group named "First Col [Header]" in the row template
group.

This special header group must contain:
- a field (for the header label)
- the "sort arrow" button
- left and right dividers

2) Change the _table.CreateHeaders script in the datagrid library.
Here, I can't substitute myself to our talented friend Trevor, I can
just try to propose a quick solution.

To have something working decently, two parts missing in the
_table.CreateHeaders handler for custom header template:
- the reference to the template group
- the behavior for the custom column header.

Here is the handler I modified. It seems doing the job for me.

private command _table.CreateHeaders
  put _ResourceStack() into theResourceStack
  put the dgProps["row template"] of me into theTemplateGroup -- line added
  --   put _CardOf() into theDGCard
  put the dgProps["columns"] of me into theColumns
  put the long ID of group "dgHeaderMask" of group
"dgHeaderComponents" of me into theHeaderGroup

  put the rect of theHeaderGroup into theHeaderRect
  put item 2 of theHeaderRect + the height of theHeaderGroup into
item 4 of theHeaderRect

  ## Clear out existing headers that don't exist
  set the wholeMatches to true
  repeat for each key theColumn in sTableObjectsA["columns"]
     if theColumn is not among the lines of theColumns and
sTableObjectsA["columns"][theColumn]["header"]["group"] is not empty
then
        put sTableObjectsA["columns"][theColumn]["header"]["group"]
into theControl
        delete theControl
        delete local sTableObjectsA["columns"][theColumn]["header"]
     end if
  end repeat

  ## Create header groups
  put the dgProps["column properties"] of me into theColsA
  put the dgProps["sort by column"] of me into sortByThisColumn -- Line added
  repeat for each line theColumn in theColumns
     ## Column
     if sTableObjectsA["columns"][theColumn]["header"]["group"] is empty then
        if not sTableObjectsA["columns"][theColumn]["header"]["uses
custom template"] then
           _table.CreateDefaultHeaderGroup theColumn, theHeaderRect
           put the result into theGroup
        else
           ## Use custom template provided by user...
           copy control (theColumn && "[Header]") of theTemplateGroup
to group "dgHeader" of me
           put it into theGroup
           set the name of theGroup to theColumn
           set the lockloc of theGroup to true
           put "control id" && word 3 of theGroup && "of me" into
sTableObjectsA["columns"][theColumn]["header"]["group"]
           put item 1 to 2 of theHeaderRect, item 1 of theHeaderRect
+ 10, item 4 of theHeaderRect into theRect
           set the rect of theGroup to theRect
        end if

       -- Lines moved
        if the dgProps["default header behavior"] of me is empty then
           set the behavior of theGroup to the long ID of button
"Default Header" of theResourceStack
        else
           set the behavior of theGroup to the dgProps["default
header behavior"] of me
        end if
        -- end of lines moved

        unlock messages
        if theColsA[theColumn]["label"] is not empty then
           set the dgLabel [theColsA[theColumn]["encoding"]] of
theGroup to theColsA[theColumn]["label"]
        else
           set the dgLabel of theGroup to theColumn
        end if
        set the dgTooltip of theGroup to theColsA[theColumn]["tooltip"]
        set the dgHilite of theGroup to theColumn is sortByThisColumn
        set the dgAlignment of theGroup to
theColsA[theColumn]["header"]["alignment"]
        lock messages
     end if
  end repeat
end _table.CreateHeaders

3) Add a default header behavior

4) Change the mouseUp handler script in the default header behavior,
for managing the header checkbox.
</quote>

Better to do the change in a copy of your library for usage in a final
application.


Having possibility to define header templates could be a cool datagrid feature.
Trevor, if maybe you are reading this, please consider the message as
a request for a possible datagrid update. 8-)


Best regards,
-- 
-Zryip TheSlug- wish you the best! 8)
http://www.aslugontheroad.co.cc




More information about the use-livecode mailing list