Data Grid Question (Again!)

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
KennyR
Posts: 256
Joined: Thu Jan 19, 2012 4:25 am

Data Grid Question (Again!)

Post by KennyR » Thu May 30, 2013 2:50 pm

Okay, I have learned quite a bit about how DG's work, but am stumped here on this issue. Believe me, this is my last resort asking on the forum...I have read and looked everywhere. Here it goes... I am using the DG helper along with a single DG in my application. I am using the "Form" style of the DG. I have a few fields and buttons residing inside the DG template, one being a button that allows the user to add comments to a field. When each row is created, I am able to add unique comments each field inside each row, but when I navigate away from the card and come back, the field is blank. Each of the other rows data is still there, and I assume because the "fillInData" handler takes the array data and puts it back in its place when the DG is created each time. Now if I place a button OUTSIDE the row template and do something like this and then add the appropriate script to the behavior script, it works fine. (but obviously it populates all the rows with the same data, which I don't want).

Code: Select all

on mouseUP
put fld "comments" into theDataA["comments"]
end mouseUp
The problem I have is adding data to the array when the data is being added from a handler from inside the behavior script. I will attach my behavior script so anyone can take a look. I hope I have been clear, if not please punch me in the head!

Code: Select all


local vLine
on mouseDown
   put the dgIndex of me into vLine
end mouseDown
on FillInData pDataArray

set the text of field "Label" of me to pDataArray["label 2"]
set the text of field "name" of me to pDataArray["name"]
set the text of field "commentField" of me to pDataArray["commentField"]
set the text of field "order" of me to pDataArray["order"]
set the text of field "total" of me to pDataArray["total"]

end FillInData

command EditValue
switch the short name of the target
case "Label"
EditFieldText the long id of field "Label" of me, the dgIndex of me,"label 2"
break
case "name"
EditFieldText the long id of field "name" of me, the dgIndex of me,"label 3"
break
case "commentField"
EditFieldText the long id of field "commentField" of me, the dgIndex of me,"label 4"
break
case "order"
EditFieldText the long id of field "order" of me, the dgIndex of me,"label 5"
break
case "total"
EditFieldText the long id of field "total" of me, the dgIndex of me,"label 8"
break
end switch
end EditValue

on mouseDoubleUp pMouseBtnNum
    if pMouseBtnNum is 1 then
        if (word 1 of the target is "field") then
            if the dgProps["allow editing"] of the dgControl of me then
                EditValue
                exit mouseDoubleUp
            end if
        end if
    end if
    
    pass mouseDoubleUp
end mouseDoubleUp

on mouseUp pTheButton
if (pTheButton is 1) then
switch the short name of the target
case "Background"
break

//This is where I add the comments to the field but they are not retained when leaving the card
//I assume this data from "it" needs to be put into the array, but I cannot seem to figure this out...
//maybe I am just wrong all together in my thinking

case "comments"
   ask "Enter any comments for your order" titled "Special Instructions"
   put it into fld "commentField" of me
break

case "delete"
send "deleteindex vLine" to grp "DataGrid" in 1
break
end switch
end if
end mouseUp

on LayoutControl pControlRect
set the rect of graphic "Background" of me to pControlRect
end LayoutControl

setprop dgHilite pBoolean
   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

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

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

Re: Data Grid Question (Again!)

Post by Klaus » Thu May 30, 2013 3:37 pm

Hi Kenny,

is the field where the user enters text part of the ROW template?
Or does it reside OUTSIDE of the datagrid?

And why do you have different names for the key of the datagroid array and for the objects in the template? 8-)


Best

Klaus

KennyR
Posts: 256
Joined: Thu Jan 19, 2012 4:25 am

Re: Data Grid Question (Again!)

Post by KennyR » Thu May 30, 2013 4:20 pm

the field resides inside the row template...I'll grab a screenshot of the row so you know what I am talking about....as for your next question....I assume you mean pDataArray and theDataA? If this is what you mean, I am not sure....I was just going by what the examples showed me....remember.....DG are foreign to me and I am still learning....

[img]
http://forums.runrev.com/download/file. ... ew&id=2018
[/img]
Attachments
boondocks.tiff
boondocks.tiff (52.11 KiB) Viewed 6085 times

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

Re: Data Grid Question (Again!)

Post by Klaus » Thu May 30, 2013 4:35 pm

Hi Kenny,

ah, now I got it :-)

What you see in a datagrid is just the visual representation of the actual data, so you need to update this "actual" data ->the DGDATA of grp "your datagrid here"

Change the mouseup handler in the behavior like this:

Code: Select all

...
  case "comments"
      ask "Enter any comments for your order" titled "Special Instructions"
      put it into tComment
      ## ALWAYS errorchecking! ;-)
      if tComment = EMPTY then
        exit mouseup
      end if
      
      ## Get the current index
      put the dgIndex of me into tIndex
      
      ## Now get the data associated with this index, which is also an array
      put the dgDataOfIndex[tIndex] of grp "your datagrid here" into tRowData
      
      ## write user input to array
      put tComment into tRowData["comments"]
      
      ## NOW write the data back into the complete DGData and the datagrid will update itself automatically
      set the dgDataOfIndex[tIndex] of grp "your datagrid here" to tRowData
      break
...
Best

Klaus

KennyR
Posts: 256
Joined: Thu Jan 19, 2012 4:25 am

Re: Data Grid Question (Again!)

Post by KennyR » Thu May 30, 2013 4:58 pm

Klaus....You are a GOD! Thanks brother!!!

KennyR
Posts: 256
Joined: Thu Jan 19, 2012 4:25 am

Re: Data Grid Question (Again!)

Post by KennyR » Fri May 31, 2013 3:20 am

sorry for keeping this thread alive but I have another stupid question... I am trying to now export the data from the DG so I can upload it to my server but am running into issues. I used the lesson materials to compose the handler, but it is not putting any data from my DG into "theDataA". I have put a button outside the DG template and placed this code in the button. I have tried to answer the "tData" after it loops but "tData is empty. The indexes are being found but no data...

Code: Select all

on mouseUp
   put the dgData of grp "DataGrid" into theDataA
   put the dgIndexes of grp "DataGrid" into theIndexes
   
   repeat for each item theIndex in theIndexes
      put theDataA["theIndex"] ["name"] into tData
      put theDataA["theIndex"] ["order"] into tData
      put theDataA["theIndex"] ["total"] into tData
   end repeat
   put tData into fld "someField"
end mouseUp
any thoughts??
Kenny

KennyR
Posts: 256
Joined: Thu Jan 19, 2012 4:25 am

Re: Data Grid Question (Again!)

Post by KennyR » Fri May 31, 2013 5:31 am

ok...I attached a small example of what I am talking about in order to make this easier to visualize....This is a DG form and two simple fields that data needs to be retrieved from....take a look and tell me why the data is not being grabbed....thanks all
Attachments
Untitled 1.livecode.zip
(4.93 KiB) Downloaded 214 times

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

Re: Data Grid Question (Again!)

Post by Klaus » Fri May 31, 2013 11:37 am

Hi Kenny,

two problems:
1. theIndex is a variable, but you pass it as a string in QUOTES!
This way LC thinks you want to address the key NAMED "theIndex" in the array,
which does not exist!

2. In the loop you always PUT XXX INTO tData!?
This will overwrite the content ot theData every time! 8-)

Create a TAB and CR delimited list from the array, I alway use something like this:

Code: Select all

on mouseUp
     put the dgData of grp "DataGrid" into tData
  
     ## We create a TAB and CR delimited list from the array
     put empty into tData2
     repeat for each KEY tIndex in tData
          put tData[tIndex]["name"] & TAB after tData2
          put tData[tIndex]["order"] & TAB after tData2
          put tData[tIndex]["total"] & CR after tData2
     end repeat
  
  ## get rid of trailing CR
  delete char -1 of tData2
  put tData2 into fld "someField"
end mouseUp
This way you can easily create another array from this data
to "re-import" it back into your DataGrid.

Best

Klaus

KennyR
Posts: 256
Joined: Thu Jan 19, 2012 4:25 am

Re: Data Grid Question (Again!)

Post by KennyR » Fri May 31, 2013 1:59 pm

Klaus....I must have looked at that small bit of code for hours and hours checking it against the lesson material and NEVER caught that I had put quotes around "tIndex" .... "Face Palm" .....Thank you for your help once again my friend...now I can move on....

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

Re: Data Grid Question (Again!)

Post by Klaus » Fri May 31, 2013 2:03 pm

OK, move on, nothing to see here :-)

Post Reply