Datagrid checkboxes and scrollbars have run amok

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
cusingerBUSCw5N
Posts: 339
Joined: Wed Jul 11, 2012 9:24 pm

Datagrid checkboxes and scrollbars have run amok

Post by cusingerBUSCw5N » Thu Jan 17, 2013 3:50 am

I have several datagrid forms with checkboxes. I have just realized that when you check on a checkbox and scroll down, a check appears in another box. For example,suppose there are 6 records and you can only see 3 of them. You check record 1, and then scroll down to see records 4 through 6 - record 4 will have check in it as well.

As you scroll around and check or uncheck items, if you scroll to to see other items, those items have been changed as well.

It is happening on 2 datagrid forms.... Here is my code for the row behavior on one of the datagrids.

Code: Select all

on CalculateFormattedHeight pDataArray
   if pDataArray["expanded"] then
      return 99
   else
      return 57
   end if
end CalculateFormattedHeight     
  on FillInData pDataArray
    set the text of field "firstname" of me to pDataArray["label 1"]
   set the text of field "label" of me to pDataArray["label 2"]
   set the label of btn "contact_email" of me to pDataArray["label 3"]
   set the label of btn "webname" of me to pDataArray["label 4"]
   put pDataArray["label 5"] into field "text_home" of me
   
   set the label of btn "button_cell" of me to pDataArray["label 6"]
   put pDataArray["label 7"] into field "text_cell" of me
   set the text of field "relationship" of me to pDataArray["label 8"]
   set the text of field "company" of me to pDataArray["label 9"]
   set the text of field "street" of me to pDataArray["label 10"]
   set the text of field "city" of me to pDataArray["label 11"]
   set the text of field "state" of me to pDataArray["label 12"]
   set the text of field "zip" of me to pDataArray["label 13"]
   if pDataArray["label 14"] is "yes" then
      set the hilite of button "OK" of me to true
   else
      set the hilite of button "OK" of me to false
   end if
   
   if pDataArray["expanded"] is "" then
      put false into pDataArray["expanded"]
   else
   end if
   set the hilite of button "change" of me to false
   set the visible of field "text_home" of me to false
   set the visible of field "text_cell" of me to false
   set the visible of graphic "ArrowExpanded" of me to pDataArray["expanded"]
   set the visible of graphic "arrowcontracted"  of me to not pDataArray["expanded"]
   
   set the visible of btn "contact_email" of me to pDataArray["expanded"] 
   set the visible of btn "webname" of me to pDataArray["expanded"] 
   set the visible of btn "button_cell" of me to pDataArray["expanded"] 
   set the visible of field "relationship" of me to pDataArray["expanded"] 
   set the visible of field "company" of me to pDataArray["expanded"] 
   set the visible of field "street" of me to pDataArray["expanded"] 
   set the visible of field "city" of me to pDataArray["expanded"]  
   set the visible of field "state" of me to pDataArray["expanded"]  
   set the visible of field "zip" of me to pDataArray["expanded"]  
    
end FillInData


on LayoutControl pControlRect
    local theFieldRect
    
    put the height of this stack into tstackheight
          if the environment is "mobile" and tstackheight >700 then
         set the width of graphic "arrowcontracted"   of me to 56
         set the height of graphic "arrowcontracted"  of me to 60
         set the width of graphic "arrowexpanded"  of me to 60
         set the height of graphic "arrowexpanded" of me to 56
      else if the environment is "mobile"  and tstackheight <=700 then
         set the width of graphic "arrowcontracted"   of me to 30
         set the height of graphic "arrowcontracted"    of me  to 36
         set the width of graphic "arrowexpanded"  of me to 36
         set the height of graphic "arrowexpanded" of me to 20
      else
              set the width of graphic "arrowcontracted"   of me to 36
         set the height of graphic "arrowcontracted"    of me  to 42
         set the width of graphic "arrowexpanded"  of me to 42
         set the height of graphic "arrowexpanded" of me to 36
     end if
     
   put the width of scrollbar "dgScrollbar" of group "dataGrid 22" into scrollbarwidth
   put the width of this stack into tstackwidth
   put (tstackwidth*.95-scrollbarwidth)*.97 into tavailable
   if the environment is "mobile" and tstackheight >700 then
            set the textsize of field "firstname" of me to 44
      set the width of field "firstname" of me to 100
      set the height of field "firstname" of me to 28
      set the textsize of field "label" of me to 18
      set the width of field "label" of me to 166
      set the height of field "label" of me to 32
      set the textsize of button "contact_email" of me to 18
      set the width of button "contact_email" of me to 278
      set the height of button "contact_email" of me to 36
      set the textsize of field "street" of me to 18
      set the width of field "street" of me to 282
      set the height of field "street" of me to 32
      set the textsize of field "city" of me to 18
            set the width of field "street" of me to 212
      set the height of field "street" of me to 32
      set the textsize of field "state" of me to 18
            set the width of field "state" of me to  72
      set the height of field "state"of me to 32
      set the textsize of field "zip" of me to 18
            set the width of field "zip" of me to 230
      set the height of field "zip" of me to 32
      set the textsize of field "relationship" of me to 18
            set the width of field "relationship" of me to 127
      set the height of field "relationship" of me to 44
      set the textsize of field "company" of me to 18
            set the width of field "company" of me to 161
      set the height of field "company" of me to 32
      set the textsize of button "webname" of me to 18
            set the width of button "webname" of me to 168
      set the height of button "webname" of me to  36
      set the textsize of button "button_cell" of me to 18
            set the width of button "button_cell" of me to 168
            set the height of button "button_cell" of me to 36
            set the textsize of button "OK" of me to 18
            set the textsize of button "change" of me to 18
            
   else if the environment is "mobile"  and tstackheight <=700 then
      set the textsize of field "firstname" of me to 12
      set the width of field "firstname" of me to 100
      set the height of field "firstname" of me to 28
      set the textsize of field "label" of me to 12
      set the width of field "label" of me to 166
      set the height of field "label" of me to 32
      set the textsize of button "contact_email" of me to 12
      set the width of button "contact_email" of me to tavailable*.50
      set the height of button "contact_email" of me to 36
      set the textsize of field "street" of me to 12
      set the width of field "street" of me to tavailable*.50
      set the height of field "street" of me to 32
      set the textsize of field "city" of me to 12
            set the width of field "city" of me to tavailable*.22
      set the height of field "city" of me to 32
      set the textsize of field "state" of me to 12
            set the width of field "state" of me to  tavailable*.10
      set the height of field "state"of me to 32
      set the textsize of field "zip" of me to 12
            set the width of field "zip" of me to tavailable*.18
      set the height of field "zip" of me to 32
      set the textsize of field "relationship" of me to 12
            set the width of field "relationship" of me to tavailable*.25
      set the height of field "relationship" of me to 44
      set the textsize of field "company" of me to 12
            set the width of field "company" of me to tavailable*.75
      set the height of field "company" of me to 32
      set the textsize of button "webname" of me to 12
            set the width of button "webname" of me to tavailable*.35
      set the height of button "webname" of me to  36
      set the textsize of button "button_cell" of me to 12
            set the width of button "button_cell" of me to tavailable*.35
      set the height of button "button_cell" of me to 36
       set the textsize of button "OK" of me to 12
            set the textsize of button "change" of me to 12
   else
      
            set the textsize of field "firstname" of me to 18
      set the width of field "firstname" of me to 100
      set the height of field "firstname" of me to 28
      set the textsize of field "label" of me to 18
      set the width of field "label" of me to 166
      set the height of field "label" of me to 32
      set the textsize of button "contact_email" of me to 18
      set the width of button "contact_email" of me to 278
      set the height of button "contact_email" of me to 36
      set the textsize of field "street" of me to 18
      set the width of field "street" of me to 282
      set the height of field "street" of me to 32
      set the textsize of field "city" of me to 18
            set the width of field "street" of me to 212
      set the height of field "street" of me to 32
      set the textsize of field "state" of me to 18
            set the width of field "state" of me to  72
      set the height of field "state"of me to 32
      set the textsize of field "zip" of me to 18
            set the width of field "zip" of me to 230
      set the height of field "zip" of me to 32
      set the textsize of field "relationship" of me to 18
            set the width of field "relationship" of me to 127
      set the height of field "relationship" of me to 44
      set the textsize of field "company" of me to 18
            set the width of field "company" of me to 161
      set the height of field "company" of me to 32
      set the textsize of button "webname" of me to 18
            set the width of button "webname" of me to 168
      set the height of button "webname" of me to  36
      set the textsize of button "button_cell" of me to 18
            set the width of button "button_cell" of me to 168
            set the height of button "button_cell" of me to 36
             set the textsize of button "OK" of me to 18
            set the textsize of button "change" of me to 18
      end if

    put item 1 of pControlRect into ttry
    
     put the left of graphic "ArrowExpanded" of me - 6 into item 1 of theFieldRect
   
     put the top of field "firstname" of me into item 2 of theFieldRect
     
     set the left of field "firstname" of me to ttry + 55
     put the formattedwidth of field "firstname" of me into tusewidth

    set the left of field "Label" of me to ttry + 55 + tusewidth + 3
    set the top of button "contact_email" of me to the bottom of field "firstname" of me + 25
    set the left of button "contact_email" of me to ttry + 20
    set the top of button "OK" of me to the top of field "Label" of me 
    put the width of this stack into tstackwidth
set the left of button "OK" of me to tstackwidth*.55
       set the top of button "change" of me to the bottom of button "OK" of me
set the left of button "change" of me to tstackwidth*.55
       
    set the bottom of button "webname" of me to the bottom of button "contact_email" of me
    set the left of button "webname" of me to the right of button "contact_email" of me + 10
   
 set the top of field "street" of me to the bottom of button "contact_email" of me + 10
    set the left of field "street" of me to ttry +10
  
   set the left of button "button_cell" of me to the left of button "webname" of me
   set the bottom of button "button_cell" of me to the bottom of field "street" of me
 
    put the right of button "button_cell" of me + 10 into tright
  
    put tright into item 3 of theFieldRect
    
    set the left of field "city" of me to ttry + 10
    set the top of field "city" of me to the bottom of field "street" of me 

    put the formattedwidth of field "city" of me into tusewidth2
    
    set the bottom of field "state" of me to the bottom of field "city" of me
    set the bottom of field "zip" of me to the bottom of field "city" of me
    set the left of field "state" of me to ttry + 10 + tusewidth2 + 5
    set the left of field "zip" of me to the right of field "state" of me + 5
  
    
    set the top of field "relationship" of me to the bottom of field "state" of me + 10
    set the left of field "relationship" of me to ttry + 10

    set the bottom of field "company" of me to the bottom of field "relationship" of me
    set the left of field "company" of me to the right of field "relationship" of me + 10
  put the bottom of field "relationship" of me into tbottom


put tbottom into item 4 of theFieldRect
put the label of button "contact_email" of me into tone
    if tone is "contact_email" or tone is "" then
       set the vis of button "contact_email" of me to false
    end if
    put the label of button "webname" of me into ttwo
  if ttwo is "webname"  or ttwo is "" then
     set the vis of button "webname" of me to false

  end if
  put the label of button "button_cell" of me into tthree
      if tthree is  "button_cell"  or tthree is "" then
         set the vis of button "button_cell" of me to false

    end if
    
    
     if the visible of field "relationship" of me then
      put the bottom of field "relationship" of me + 10 into item 4 of theFieldRect
   else
      put the bottom of field "firstName" of me + 28 into item 4 of theFieldRect
end if                                                                                                                                      
   
    set the rect of graphic "Background" of me to theFieldRect

end LayoutControl

on ResetData
    -- Sent when data is being emptied because the control is no longer being used to display data
    set the text of field "Label" of me to empty
end ResetData


on PreFillInData
    -- Sent right before new data is going to replace existing data in the control
end PreFillInData


setprop dgHilite pBoolean
    -- This custom property is set when the highlight of your custom template has
    -- changed. By default the "Background" graphic will be highlighted for you. 
    -- You only add script here if you want to further customize the highlight.
    
    -- Example:
 
 set the dgProp["hilited text color"] of the dgControl of me to "black"
   if pBoolean then

          set the foregroundcolor of me to the dgProp["hilited text color"] of the dgControl of me

    else
        set the foregroundColor of me to empty
     end if

end dgHilite


getprop dgDataControl
    -- Required by library so that it can locate your control.
    return the long ID of me
end dgDataControl


on mouseDoubleUp pMouseBtnNum
    local theKey
    
    -- Example of how to edit the contents of a field.
    -- By passing the index of the record associated with copy of this template being displayed and
    -- a key (array key) the data grid will automatically save the changes the user
    -- makes and refresh the UI by calling FillInData and resizeControl.
    if pMouseBtnNum is 1 then
        if the dgProps["allow editing"] of the dgControl of me then
            switch the short name of the target
                case "Label"
                    put "Label 1" into theKey
                    EditFieldText the long ID of the target, the dgIndex of me, theKey
                    break                 
            end switch
        end if
    end if
    
    pass mouseDoubleUp
end mouseDoubleUp

on mouseUp pMouseBtnNum
   if pMouseBtnNum is 1 then
 
      switch the short name of the target
         case "ArrowExpanded"
         case "ArrowContracted"
            ## Update internal data
            SetDataOfIndex the dgIndex of me, "expanded", the short name of the target is "ArrowContracted"
            ## Redraw so LayoutControl and FillInData can update UI
            RefreshIndex the dgIndex of me
            break
            
      end switch
   end if
end mouseUp

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

Re: Datagrid checkboxes and scrollbars have run amok

Post by Klaus » Thu Jan 17, 2013 12:55 pm

Hi Carolyn,

there is nothing in this lengthy script that could cause your inconvenience!?


Best

Klaus

cusingerBUSCw5N
Posts: 339
Joined: Wed Jul 11, 2012 9:24 pm

Re: Datagrid checkboxes and scrollbars have run amok

Post by cusingerBUSCw5N » Thu Jan 17, 2013 7:01 pm

As usual - the problem is with me. When using checkboxes with a datagrid, I forgot to do two things:

1) In the datagrid's row behavior, you have to set the value - even if the initial value of the checkboxes is false. I believe that it has to be part of your data array. So since I am bringing in data from the Internet, I have to add one more field that am bringing in and set that field to false. Then in Livecode, row behavior, add this line:

Code: Select all

  set the hilited of button "email_it" of me to pDataArray["label 4"]
(label 4 is the 4th item I am bringing in). However, because I haven't figured out how to get rid of the last line in my datagrid - where the data is empty, but the buttons still show up, it generates an error - because the line of code above expects a true or false, not an empty. So, I changed the code to

Code: Select all

  if pDataArray["label 4"] is "false" or pDataArray["label 4"] is "true" then
   set the hilited of button "email_it" of me to pDataArray["label 4"]
end if
This stopped the problem of checking a box in one place and having it appear checked in multiple places.

However, I'm not done. If you stop here, then when you click a checkbox, scroll down, and then scroll back up, the checkbox is unchecked.

The second thing I forgot to do was to set the datagrid to update itself when the checkbox was clicked (I thought this was automatic, but it's not). To do that, you have to go into Row Template, then Edit group, then click on the checkbox and choose Object Script. There, you add the following code:

Code: Select all

  if word 1 of the target <> "scrollbar" then
      put the dgHilitedlines of group "DataGrid_multiple" into theline
         ## did they click on the checkbox?
   if the short name of the target is "email_it" then
## Update internal value in data grid
      SetDataOfLine the dgLine of me, "email_it", the hilite of the target
dispatch "SetDataOfLine" to group "datagrid_multiple"  with theline, "label 4", "true" 
end if
end if
(Label 4 is the 4th item that I am bringing in to the datagrid).

Now, when I scroll back, the checkbox is checked!

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

Re: Datagrid checkboxes and scrollbars have run amok

Post by Klaus » Thu Jan 17, 2013 7:43 pm

Hi Carolyn

for something like this:

Code: Select all

if pDataArray["label 4"] is "false" or pDataArray["label 4"] is "true" then
   set the hilited of button "email_it" of me to pDataArray["label 4"]
end if
Monsieur Boole is our best friend! :D

Code: Select all

...
set the hilite of btn "Check" to (pDataArray["label 4"] = TRUE)
...
Will always work, even if pDataArray["label 4"] = EMPTY or whatever! :D

Mabe you should add a "mouseup" handler to that "Check" button, so it sets the appropriate value in the datagrid if clicked!
Something like this in btn "Check" in the ROW Template:

Code: Select all

on mouseup
  put the dgIndex of me into tIndex
  put the dgDataOfIndex[tIndex] of me into tNewArray

  ## Store new value in DGData Array:
  put the hilite of me into tNewArray[tIndex]["label 4"]
  lock screen
  set the dgDataOfIndex[tIndex] of me to tNewArray
  unlock screen
end mouseup
Out of my head, untested, but you get the idea :D

Best

Klaus

Post Reply