LiveCode Lessons » LiveCode Data Grid » How Do I Export Data From A Data Grid?

How Do I Export Data From A Data Grid?

This lesson will show you how to get data out of a data grid.

The Example Data Grid

Media_1247315771098

Here is what the data grid looks like that I will be exporting data from.

The Handler For Populating Data Grid

command uiPopulatePeople
   put "images/" into theImageFolder
   
   put "Lucky" into theDataA[1]["FirstName"]
   put "Day" into theDataA[1]["LastName"]
   put "Three Amigo" into theDataA[1]["Title"]
   put theImageFolder & "monkey.jpg" into theDataA[1]["Image URL"]
   
   put "Dusty" into theDataA[2]["FirstName"]
   put "Bottoms" into theDataA[2]["LastName"]
   put "Three Amigo" into theDataA[2]["Title"]
   put theImageFolder & "monkey.jpg" into theDataA[2]["Image URL"]
   
   put "Ned" into theDataA[3]["FirstName"]
   put "Nederlander" into theDataA[3]["LastName"]
   put "Three Amigo" into theDataA[3]["Title"]
   put theImageFolder & "monkey.jpg" into theDataA[3]["Image URL"]
   
   put "Jane" into theDataA[4]["FirstName"]
   put "Blue" into theDataA[4]["LastName"]
   put "Secret Agent" into theDataA[4]["Title"]
   put theImageFolder & "monkey.jpg" into theDataA[4]["Image URL"]
   
   put "Jefferson" into theDataA[5]["FirstName"]
   put "Blue" into theDataA[5]["LastName"]
   put "Secret Agent" into theDataA[5]["Title"]
   put theImageFolder & "monkey.jpg" into theDataA[5]["Image URL"]
   
   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

This is the code that was used to populate the data grid. This shows you the keys that each record has (FirstName, LastName, Title and Image URL).

Export Handler

command uiExportData
   ## Export data to XML
   
   ## Get Data Grid Array
   put the dgData of group "DataGrid 1" into theDataA
   
   ## Get indexes in proper order
   put the dgIndexes of group "DataGrid 1" into theIndexes
   
   ## Prefix XML
   put "<people>" & cr into theXML
   
   ## Loop through data, putting into XML format
   repeat for each item theIndex in theIndexes
      put "<person>" & cr after theXML
      put "<first_name>" & theDataA[theIndex]["FirstName"] & "</first_name>" & cr after theXML
      put "<last_name>" & theDataA[theIndex]["LastName"] & "</last_name>" & cr after theXML
      put "<title>" & theDataA[theIndex]["Title"] & "</title>" & cr after theXML
      put "</person>" & cr after theXML
   end repeat
   
   ## Close root XML tag
   put "</people>" after theXML
   
   put theXML
end uiExportData

This is an example of how to get data out of a data grid. I begin by getting the dgData array and the dgIndexes. The indexes are a comma delimited list of the keys of the dgData in the proper order.

After you have the array and the ordered list of indexes you can loop through each record in the array. In this example I'm just wrapping the data in XML tags.

Example Output

Media_1247315800246

This is what the output for my example looks like in the message box.

Prev: How Do I Determine If the Data Grid Has Focus? Next: How Do I Work with Checkboxes in a Data Grid?

Comments (0)

Add your comment




E-Mail me when someone replies to this comment

Are you human?