Search code examples
datagridlivecode

How do I create an expanding field editor in a LiveCode DataGrid?


I need to create a field editor in a LiveCode DataGrid that grows as the user types to fit the formattedHeight of the field. The rest of the underlying row control needs to resize too along with shifting any subsequent row controls down.


Solution

  • Answering my own question as it may be useful to others.

    Copy the field editor behavior button from the revDataGridLibrary stack to the card with the row template on it.

    Edit the script of the field to be edited as follows (note you will need to fix the behavior button reference to be the long id of your new field edtior behaviour):

    on preOpenFieldEditor pEditor
       set the behavior of pEditor to the long id of button id 1023 of card id 1010 of stack "Data Grid Templates 1362091650635"
       set the uRowControl of pEditor to the long id of the owner of me
    end preOpenFieldEditor
    

    Edit the field editor behavior script adding the following:

    local sHeight,sRowControl
    
    setProp uRowControl pRowControl
       put pRowControl into sRowControl
    end uRowControl
    
    on openField
       put the formattedHeight of me into sHeight
       pass openField
    end openField
    
    on textChanged
       local tHeight,tRect
       lock screen
       put the formattedHeight of me into tHeight
       if sHeight <> tHeight then
          put the rect of me into tRect
          put item 2 of tRect+ tHeight into item 4 of tRect
          set the rect of me to tRect
          put tHeight into sHeight
          dispatch "UpdateRow" to sRowControl with the long id of me
       end if 
       unlock screen
       pass textChanged
    end textChanged
    

    Now edit the row template behaviour adding the following handler (Note that in this case the field being edited is named "note" so you will want to change that for your use case):

    on UpdateRow pFieldEditor
       set the rect of graphic "Background" of me to the rect of pFieldEditor
       set the rect of fld "note" of me to the rect of pFieldEditor
       set the rect of me to the formattedRect of me
       put the uScriptLocal["sTableObjectsA"] of me into tTableObjectsA
       repeat for each line tControl in tTableObjectsA["all row controls"]
          delete word -2 to -1 of tControl -- of me
          put tControl into tControlA[the dgIndex of tControl]
       end repeat
       put the bottomLeft of me into tTopLeft
       repeat for each item tIndex in tTableObjectsA["current"]["indexes"]
          if tIndex > the dgIndex of me then
             set the topLeft of tControlA[tIndex] to tTopLeft
             put the bottomLeft of tControlA[tIndex] into tTopLeft
          end if
       end repeat
    end UpdateRow