Displaying one record in a DataGrid at a time

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
stevenwhyte
Posts: 14
Joined: Mon Jan 12, 2015 7:55 pm

Displaying one record in a DataGrid at a time

Post by stevenwhyte » Sun Sep 06, 2015 7:37 pm

Hi,

I have created a program which displays some graphics cards into a datagrid.

I was wanting to just display one record from the data grid at a time and use a back and forward button to navigate between the records. Is this possible? If so, how do I go about doing it?

I've uploaded the stack that I am working on and was hoping for some help with this if possible...

Many thanks in anticipation,

Steven
Attachments
8_ExtremeTech (Working).livecode.zip
LiveCode file
(53.29 KiB) Downloaded 200 times

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

Re: Displaying one record in a DataGrid at a time

Post by Klaus » Sun Sep 06, 2015 8:27 pm

Hi Steven,

sure this is possible! :D

Out of my head:
1. Store the array with all records in a custom property of the card or stack: e.g. cCompleteArray
2. Use this syntax to display the first record in the datagrid
...
put the cCompletArray of this cd into tArrayStored
put tArrayStored[1] into tDisplayArray[1]
set the dgdata of grp "datagrid" to tDisplayArray
...
3. Store the current displayed index so you can use this to navigate:
Pseudo pseudo code :D

Code: Select all

command navigateback
  if the cCurrentIndex of this cd = 1 then
    beep
    exit to top
end if
   put the cCurrentIndex of this cd - 1 into tNewIndex
   set the cCurrentIndex of this cd to tNewIndex ## !!
   put the cCompletArray of this cd into tArrayStored
   put tArrayStored[tNewIndex] into tDisplayArray[1]
   set the dgdata of grp "datagrid" to tDisplayArray
end naviback
I'll leave the navigateforth handler up to you, you get the picture! :D


Best

Klaus

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

Re: Displaying one record in a DataGrid at a time

Post by sritcp » Mon Sep 07, 2015 4:18 am

Hi Steven:

If you are going to display only one record at a time, you may choose to use a Basic Table for display purposes. (You can still store the entire data in a datagrid).
You can, for instance,

Code: Select all

put the dgText of group "Datagrid1" into tList
put line 1 of tList into line 2 of field "BasicTable"
(so you can display the column names on line 1 of the basic table).

You can code "Forward" and "Backward" buttons to display each line in tList, one by one.
(You can also use the dgProp["columns"] of group "Datagrid1" to directly load the column names into line 1 of the basic table; but this list is line delimited, so you need to convert it to tab delimited before you feed it to the basic table).

Regards,
Sri

stevenwhyte
Posts: 14
Joined: Mon Jan 12, 2015 7:55 pm

Re: Displaying one record in a DataGrid at a time

Post by stevenwhyte » Mon Sep 07, 2015 12:43 pm

Many thanks for your kind replies. You are always so helpful on here.

I will give both techniques a try.

Thanks again!

Steven

stevenwhyte
Posts: 14
Joined: Mon Jan 12, 2015 7:55 pm

Re: Displaying one record in a DataGrid at a time

Post by stevenwhyte » Tue Sep 08, 2015 8:26 am

Ok, having a little issue with the code that you supplied me with. Probably something I am doing wrong. I have created a table field and called it "BasicTable" and I've added the following code to a button which includes the two lines at the bottom that you supplied above.

Any chance you could take a look?

Code: Select all

//  Allow access to global arrays and variables Setup in main card.
Global arrayName, arrayRam, arrayClockSpeed, arrayCost, MaxCards


On mouseUp
   display_cards arrayName, arrayRam, arrayClockSpeed, arrayCost, MaxCards  
End mouseUp


On display_cards arrayName, arrayRam, arrayClockSpeed, arrayCost, MaxCards
   //  Setup local variables
   Local theGraphicsCardInfo, CardCount, tList
   
   //  Clear text from the fields
   Put empty into field "Total Found"
   
   //  Display the heading
   Put "Displaying All Graphics Cards" into field "Sub Heading"
   
   //  Zero number of graphics cards found
   Put 0 into CardCount
   
   //  Display all graphics cards
   Repeat with loop = 1 to MaxCards
      
      //  Increment the number of graphics cards displayed.  
      //  This is also used to determine which records will appear in the data grid.
      Add 1 to CardCount
      
      // Copy graphics card data from array to data grid
      Put arrayName[loop] into theGraphicsCardInfo[CardCount]["name"]
      Put arrayRam[loop] into theGraphicsCardInfo[CardCount]["memory"]
      Put arrayClockSpeed[loop] into theGraphicsCardInfo[CardCount]["speed"]
      Put arrayCost[loop] into theGraphicsCardInfo[CardCount]["cost"]
   End Repeat
   
   Set the dgData of Group "DataGrid 1" to theGraphicsCardInfo  //  Copy list to data grid
   Put CardCount into field "Total Found"  // Display number of graphics cards found
   
   put the dgData of Group "Datagrid 1" into tList
   put line 1 of tList into line 1 of field "BasicTable"
End display_cards

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

Re: Displaying one record in a DataGrid at a time

Post by Klaus » Tue Sep 08, 2015 12:27 pm

The DGDATA of a datagrid is an ARRAY, so you cannot "put line 1 of it" anywhere :D
Sri was referring to the DGTEXT of the datagrid!

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

Re: Displaying one record in a DataGrid at a time

Post by sritcp » Tue Sep 08, 2015 2:00 pm

Hi Steven:

1. As Klaus pointed out, the DgData of a datagrid is an array, so it has no "lines" in it, only "key" and "value" for each "element".
The same info is made available to us as the DgText of that array, which is a tab-delimited list, and thus, can have lines.

2. If you need to use a datagrid to store, manipulate, and display for other reasons, it's fine.
But once you have chosen BasicTable, you don't need a datagrid simply for displaying that data.
In other words, BasicTable is a (simpler) substitute for datagrid.
So, you can put the data directly into a list. For example,

Code: Select all

set the itemDel to tab
Put arrayName[loop] into item 1 of line CardCount of tList -- here we assume "name" to be item 1, "memory" is item 2, etc
Put arrayRam[loop] into item 2 of line CardCount of tList
...............
put line 1 of tList into line 2 of field "BasicTable" -- You can use line 1 of BasicTable to display column labels such as "name", "memory", etc.
Regards,
Sri

stevenwhyte
Posts: 14
Joined: Mon Jan 12, 2015 7:55 pm

Re: Displaying one record in a DataGrid at a time

Post by stevenwhyte » Tue Sep 08, 2015 8:02 pm

Many thanks for all your help! This is working as expected. I should have known that DGData was the group. I had never used DGText before and understand how this works now.

With regards to record navigation, is there an increment command in LiveCode that I can use to automatically increment the line number of tList as I click on the forward button and then decrease the line number when I click on the back button?

I tried:

Code: Select all

Global tList

on mouseUp
   Put line 1 of tList into line 2 of field "BasicTable"
   Wait until the mouse is up
   Put line 2 of tList into line 2 of field "BasicTable"
   Wait until the mouse is up
   Put line 3 of tList into line 2 of field "BasicTable"
end mouseUp
But it goes straight to the 3rd item in the list when the button is pressed. I know this will be simple but can't think! :oops:

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

Re: Displaying one record in a DataGrid at a time

Post by Klaus » Tue Sep 08, 2015 9:38 pm

Hi Steven,
stevenwhyte wrote:...But it goes straight to the 3rd item in the list when the button is pressed.
not really, the script works as exspected!
It will display line 1, then line 2 and then ends up with line 3 in line 2 of your field.
But since in a "mouseup" handler the mouse is ALWAYS up, the script does not wait! :D

Do something like this:

Code: Select all

Global tList
local tCurrentDisplayedLine

on mouseUp
   if tCurrentDisplayedLine = EMPTY OR tCurrentDisplayedLine  > 3 then
      put 1 into tCurrentDisplayedLine
   end if
   Put line tCurrentDisplayedLine of tList into line 2 of field "BasicTable"
   add 1 to tCurrentDisplayedLine
end mouseUp
Best

Klaus

stevenwhyte
Posts: 14
Joined: Mon Jan 12, 2015 7:55 pm

Re: Displaying one record in a DataGrid at a time

Post by stevenwhyte » Wed Sep 09, 2015 7:37 am

Many thanks for your time and help :D

stevenwhyte
Posts: 14
Joined: Mon Jan 12, 2015 7:55 pm

Re: Displaying one record in a DataGrid at a time

Post by stevenwhyte » Wed Sep 09, 2015 8:07 pm

Ok - many thanks for your help. Hopefully one last question. What about the code for the back button.

I've tried:

Code: Select all

Global tList
local tCurrentDisplayedLine

on mouseUp
   if tCurrentDisplayedLine = EMPTY OR tCurrentDisplayedLine  > 6 then
         put -1 into tCurrentDisplayedLine
   end if
   Put line tCurrentDisplayedLine of tList into line 2 of field "BasicTable"
   add -1 to tCurrentDisplayedLine
end mouseUp
The graphics cards go backwards ok but once it reaches the first line, the line is blank.

Thanks in anticipation yet again!

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

Re: Displaying one record in a DataGrid at a time

Post by Klaus » Wed Sep 09, 2015 10:31 pm

Hi Steven,

huh, this is a mentally quite challenging approach! :D

Code: Select all

"OR tCurrentDisplayedLine  > 6"
tCurrentDisplayedLine is never > 6 nor even >=0, since you only have negative numbers in this variable!
I think your field only has 6 lines, but tCurrentDisplayedLine has the value of -7 at some time so you get an empty line!

I would use a less abstract approach 8)

Code: Select all

Global tList
local tCurrentDisplayedLine

on mouseUp
   if tCurrentDisplayedLine = EMPTY OR tCurrentDisplayedLine < 1 then
         put 6 into tCurrentDisplayedLine
   end if
   Put line tCurrentDisplayedLine of tList into line 2 of field "BasicTable"
   subtract 1 from tCurrentDisplayedLine
end mouseUp
Best

Klaus

stevenwhyte
Posts: 14
Joined: Mon Jan 12, 2015 7:55 pm

Re: Displaying one record in a DataGrid at a time

Post by stevenwhyte » Thu Sep 10, 2015 7:34 am

Thanks again :D

Post Reply