Datagrid (FORM)

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
lemodizon
Posts: 218
Joined: Thu Apr 05, 2018 3:33 pm

Datagrid (FORM)

Post by lemodizon » Fri Jul 12, 2024 6:48 pm

Hi everyone,

Good day.

I'm practicing the datagrid (FORM) I'm trying to display all the data from my database using datagrid form but it display only the 1st record.

i tried using repeat i think there is wrong in my code. hope you can help me thanks in advance

here is my code

Code: Select all

command uiPopulatePeople
   global gConnectionID
   local tSQLStatement, tlist
   
   put "C:\Users\CodeLemz\Documents\Zigzag project\ZigzagWeb\Picture/" into theImageFolder
   
   put "SELECT * FROM sbcust " into tSQLStatement
   put revDataFromQuery(tab,return, gConnectionID, tSQLStatement) into tlist
   
   set the itemdel to tab
   
   
   repeat for each line tLine in tlist
      
      if item 1 of tlist is a number  then
        
         put item 2 of tlist into theDataA[1]["FirstName"]
         put item 3 of tlist into theDataA[1]["Title"]
         put theImageFolder & "close_window_48px.png" into theDataA[1]["Image URL"]
      end if
      
   end repeat
   
   	
   lock screen
   set the dgData of group "DataGrid 1" to theDataA
   	
   ## Hilite first row
   set the dgHilitedLines of group "DataGrid 1" to 1
   unlock screen
end uiPopulatePeople

Attachments
datagrid_form.png
datagrid_form.png (8.5 KiB) Viewed 3812 times
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

stam
Posts: 3061
Joined: Sun Jun 04, 2006 9:39 pm

Re: Datagrid (FORM)

Post by stam » Fri Jul 12, 2024 6:59 pm

I think the problem is with your repeat loop.

Code: Select all

repeat for each line tLine in tlist
      if item 1 of tlist is a number  then
        
         put item 2 of tlist into theDataA[1]["FirstName"]
         put item 3 of tlist into theDataA[1]["Title"]
         put theImageFolder & "close_window_48px.png" into theDataA[1]["Image URL"]
      end if
      
end repeat
All the data is written into the same key: theDataA[1]. So you're continuously overwriting the same key with the the loop data.

You should have an inrememtal variable fore the the root key.
This should work:

Code: Select all

local x // initially empty / zero
repeat for each line tLine in tlist
      if item 1 of tlist is a number  then
         add 1 to x  // will start from 1 and increment every loop
         put item 2 of tlist into theDataA[x]["FirstName"]
         put item 3 of tlist into theDataA[x]["Title"]
         put theImageFolder & "close_window_48px.png" into theDataA[x]["Image URL"]
      end if
end repeat

lemodizon
Posts: 218
Joined: Thu Apr 05, 2018 3:33 pm

Re: Datagrid (FORM)

Post by lemodizon » Sat Jul 13, 2024 4:54 am

stam wrote:
Fri Jul 12, 2024 6:59 pm
I think the problem is with your repeat loop.

Code: Select all

repeat for each line tLine in tlist
      if item 1 of tlist is a number  then
        
         put item 2 of tlist into theDataA[1]["FirstName"]
         put item 3 of tlist into theDataA[1]["Title"]
         put theImageFolder & "close_window_48px.png" into theDataA[1]["Image URL"]
      end if
      
end repeat
All the data is written into the same key: theDataA[1]. So you're continuously overwriting the same key with the the loop data.

You should have an inrememtal variable fore the the root key.
This should work:

Code: Select all

local x // initially empty / zero
repeat for each line tLine in tlist
      if item 1 of tlist is a number  then
         add 1 to x  // will start from 1 and increment every loop
         put item 2 of tlist into theDataA[x]["FirstName"]
         put item 3 of tlist into theDataA[x]["Title"]
         put theImageFolder & "close_window_48px.png" into theDataA[x]["Image URL"]
      end if
end repeat


Hi Stam,

Thanks it works question how can i display the other records from my tlist coz it only display the 1st record the rest it did not displayed.

datagrid_form1.png
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

stam
Posts: 3061
Joined: Sun Jun 04, 2006 9:39 pm

Re: Datagrid (FORM)

Post by stam » Sat Jul 13, 2024 6:18 pm

How have you set up the fillInData handler in the row template's behavior script?

This looks like you've not used "of me" when assigning values to fields.

2 tips:
1. Look at custom properties of the datagrid - use the dropdown menu in the property inspector in the "custom" tab and select "dgCache". This will show you the array of the datagrid with the data by index and the sequence of records. Open the disclosure triangle and inspect the array to ensure the correct records are there.

2. If the correct records are in the data grid's array as seen in its dgCache, then the issue is how you are showing this data and you you should inspect the fillInData handler in the row's behavior script.
When putting values into fields in that handler, you *must* specify <field name> of me. The 'of me' tells LC you're referring to the field of that specific row. If you don't it will only populate with the field of the first row.

I think that's may be happening, but let us know...
Obviously, if the issue is that the dgCache contains wrong data (ie 3 duplicates) you need to review the code that populates the dgData again...

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7389
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Datagrid (FORM)

Post by jacque » Sat Jul 13, 2024 6:27 pm

I think you need to use the built-in increments like this:

Code: Select all

repeat for each line tLine in tlist
      if item 1 of tLine is a number  then
         add 1 to x  // will start from 1 and increment every loop
         put item 2 of tLine into theDataA[x]["FirstName"]
         put item 3 of tLine into theDataA[x]["Title"]
         put theImageFolder & "close_window_48px.png" into theDataA[x]["Image URL"]
      end if
end repeat
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

stam
Posts: 3061
Joined: Sun Jun 04, 2006 9:39 pm

Re: Datagrid (FORM)

Post by stam » Sat Jul 13, 2024 7:16 pm

jacque wrote:
Sat Jul 13, 2024 6:27 pm
I think you need to use the built-in increments like this:

Code: Select all

repeat for each line tLine in tlist
      if item 1 of tLine is a number  then
         add 1 to x  // will start from 1 and increment every loop
         put item 2 of tLine into theDataA[x]["FirstName"]
         put item 3 of tLine into theDataA[x]["Title"]
         put theImageFolder & "close_window_48px.png" into theDataA[x]["Image URL"]
      end if
end repeat
Hi Jacque, Lemodizon did exactly after I posted this handler above. Now there is the correct number of rows, but all have the first value.
I think this issue is due to the fillInData handler:

Code: Select all

on FillInData pDataArray
   set the text of field "<field name>" of me to pDataArray["<field name key>"]
end FillInData
I've had this issue before: where I forget to refer to the field name as field <field name> of me - without the 'of me' all fields display the first value even though the array contains each value, because the 'of me' localises the field to the row in question...

My usual process in similar situations is to first verify the data grid's array contains the correct data by inspecting its dgCache in the custom properties section of the property inspector, and if that is the case the next step is to inspect the fillInData handler - I think the last issue lies there.

S.

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

Re: Datagrid (FORM)

Post by Klaus » Sat Jul 13, 2024 8:07 pm

The problem is or was using tList instead of tLine in the loop:

Code: Select all

...
repeat for each line tLine in tlist
      if item 1 of tlist is a number  then
...
Tha is what Jacque had fixed in her script!

stam
Posts: 3061
Joined: Sun Jun 04, 2006 9:39 pm

Re: Datagrid (FORM)

Post by stam » Sat Jul 13, 2024 8:20 pm

Klaus wrote:
Sat Jul 13, 2024 8:07 pm
The problem is or was using tList instead of tLine in the loop:

Code: Select all

...
repeat for each line tLine in tlist
      if item 1 of tlist is a number  then
...
Tha is what Jacque had fixed in her script!
Quite right, I stand corrected. And well spotted Jacque :)
I hadn't spotted the OP was referencing the container tList instead of the line tLine inside the loop.

The process to troubleshoot is the same and if I was actually debugging this, I would have spotted that the dgCache contained incorrect data. That would always be step 1 before looking at the fillInData handler... but always more difficult to do if troubleshooting in my mind ;)

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7389
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Datagrid (FORM)

Post by jacque » Sat Jul 13, 2024 9:33 pm

stam wrote:
Sat Jul 13, 2024 8:20 pm
Quite right, I stand corrected. And well spotted Jacque :)
I hadn't spotted the OP was referencing the container tList instead of the line tLine inside the loop.
It was easy to miss, there's only 2 characters difference. The brain tends to fill in the rest. Human brains are capricious.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

lemodizon
Posts: 218
Joined: Thu Apr 05, 2018 3:33 pm

Re: Datagrid (FORM)

Post by lemodizon » Sun Jul 28, 2024 5:01 am

Hi everyone,

Thanks everyone for the response.
Thank you & God Bless Everyone :wink:

Regards,
lemodizon

Post Reply