loading data into DataGrid

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

loading data into DataGrid

Post by cusingerBUSCw5N » Thu Jul 26, 2012 8:10 pm

I am accessing data - from a website and want to load it into a DataGrid. I have partially succeeded - my data is being loaded into the Titles part of the DataGrid...

so, I need it to just put the titles there, and put the data below.

Here is the code I am using

on mouseUp
put URL "myurl" into tFileContents
put line 1 of tFileContents into tColumnTitles
replace tab with return in tColumnTitles
set the dgProp[ "columns" ] of group "text" to tColumnTitles
set the dgText[true] of group "text" to tFileContents
end mouseUp

Here is an example of the fileContents (column titles and two lines)

Link_address topic
website address1 Custom market research services from Export.Gov
website address2 How to complete a NAFTA Certificate of Origin form

Thanks!

townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm

Re: loading data into DataGrid

Post by townsend » Thu Jul 26, 2012 11:25 pm

Good job getting the data off the web. As for putting it into a datagrid,
there's a lot of good code in this example stack. I think you'll find a of answers here.
CRUD SQLite example

Image

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

Re: loading data into DataGrid

Post by cusingerBUSCw5N » Sat Jul 28, 2012 4:49 am

OK - I'm getting a lot closer. LiveCode has loaded the columns correctly, loaded information for the first column, but the second column is blank.

Here is my code: (I am bringing in an html file so I have to switch <br> with return and usetab with tab so that it can be read as tab delimited information). I used the code from LiveCode's How do I create a Datagrid dynamically from tab delimited data


on mouseUp
put url ("website address for my data") into theData
replace "<br>" with return in theData
replace "usetab" with tab in theData
lock screen --when doing visual stuff, this helps to speed things up
--make sure the group does not exist already
if there is a group "my Datagrid" then
delete group "my Datagrid"
end if
--copy the template group
copy group "DataGrid" of group "Templates" of stack "revDataGridLibrary" to this card
set the name of it to "my Datagrid"
set the dgProp["style"] of group "my Datagrid" to "table"
--position it nicely
set the rectangle of group "my Datagrid" to 0,the bottom of me + 16,the width of this card, the height of this card

--create row template
put the name of this stack into theMainstack
put "Data Grid Templates" && the seconds into theName
create invisible stack theName
set the mainStack of stack theName to theMainstack
go stack theName
create card
create field "label"
create graphic "Background"
group field 1 and graphic 1
set the name of last group to "Row Template"
go stack theMainstack
--point datagrid to template
set the dgProp["Row Template"] of group "my Datagrid" to the long id of group "Row Template" of stack theName

--create columns
put line 1 of theData into theColumns
--no empty column names are allowed, so I insert a space when that happens
replace tab & tab with tab & " " & tab in theColumns
replace tab with return in theColumns
set the dgProp["columns"] of group "my DataGrid" to theColumns

--add data
put true into firstLineAreNames
set the dgText[firstLineAreNames] of group "my DataGrid" to theData
end mouseUp
end mouseUp

Why won't my second column appear?

Here is sample data (columns link_address2 and topic) plus four data records after it.

Link_address2 usetab topic <br> http://build.export.gov/main/mrktresear ... etabCustom market research services from Export.Gov
<br> http://export.gov/logistics/eg_main_018131.aspusetabHow to complete a NAFTA Certificate of Origin form
<br> http://texastrade.org/training/online-i ... usetabWant to import? Global Sourcing Essentials
<br> http://www.sbdcglobal.com/about-sbdcglo ... bQualified trade leads in Mexico and Colombia through SBDCGlobal

Thanks

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: loading data into DataGrid

Post by snm » Sat Jul 28, 2012 7:49 am

Maybe I don't understand you corectly, but what do you expect in second column? There is no data for second column.

Marek.

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

Re: loading data into DataGrid

Post by cusingerBUSCw5N » Sat Jul 28, 2012 4:04 pm

The second column is found after "usetab"

So - for this record
http://build.export.gov/main/mrktresear ... etabCustom market research services from Export.Gov

(this is truncating the middle part of the record - which is ...mrktresearch/eg_main_018208usetabCustom....)

I have LiveCode replace usetab with tab to create tab-delimited information.

so the second column is "Custom market research services from Export.Gov"

but only the http://build.export.gov/main/mkrtresear ... ain_018208 is showing up

Image

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

Re: loading data into DataGrid

Post by cusingerBUSCw5N » Mon Jul 30, 2012 4:04 pm

I DID IT! I don't know why, but I changed the line:

put true into firstLineAreNames

and switched it to false

ALL THE DATA APPEARED! - including the first line that I had set up as titles. So, I manually named the columns and eliminated the titles in my data and it absolutely works.

I would be nice to know why Put true into FirstLineAreNames didn't work for me - but if I never know, the workaround will do it for me. Here is my final script:

on mouseUp

put url ("page_from_my_cold_fusion_website_with_data_only") into theData
replace "<br>" with return in theData
replace "usetab" with tab in theData
lock screen --when doing visual stuff, this helps to speed things up
--make sure the group does not exist already
if there is a group "my Datagrid" then
delete group "my Datagrid"
end if
--copy the template group
copy group "DataGrid" of group "Templates" of stack "revDataGridLibrary" to this card
set the name of it to "my Datagrid"
set the dgProp["style"] of group "my Datagrid" to "table"
--position it nicely
set the rectangle of group "my Datagrid" to 0,the bottom of me + 16,the width of this card, the height of this card

--create row template
put the name of this stack into theMainstack
put "Data Grid Templates" && the seconds into theName
create invisible stack theName
set the mainStack of stack theName to theMainstack
go stack theName
create card
create field "label"
create graphic "Background"
group field 1 and graphic 1
set the name of last group to "Row Template"
go stack theMainstack
--point datagrid to template
set the dgProp["Row Template"] of group "my Datagrid" to the long id of group "Row Template" of stack theName

--create columns
put "Link Address" & tab & "Topic" into tcolumntitle
put tcolumntitle into theColumns
--no empty column names are allowed, so I insert a space when that happens
replace tab & tab with tab & " " & tab in theColumns
replace tab with return in theColumns
set the dgProp["columns"] of group "my DataGrid" to theColumns

--add data
put false into firstLineAreNames
set the dgText[firstLineAreNames] of group "my DataGrid" to theData
end mouseUp
end mouseUp

Here is some data from the page:
(three lines - two columns usetab separates the columns)

http://build.export.govusetabCustom market research services
http://export.gov/usetabHow to complete a NAFTA Certificate of Origin form
http://texastrade.orgusetabWant to import? Global Sourcing Essentials

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

Re: loading data into DataGrid

Post by Klaus » Mon Jul 30, 2012 5:16 pm

Hiy cusingerBUSCw5N,
cusingerBUSCw5N wrote:... I changed the line:
put true into firstLineAreNames
and switched it to false
ALL THE DATA APPEARED! - including the first line that I had set up as titles. So, I manually named the columns and eliminated the titles in my data and it absolutely works....
This sounds as if there is no CR/Return after your titles, means your line ONE is not a sepearate line somehow,
check the last character of that "line", or maybe there is no "usetab" there? Or maybe other HTML "remnants"?
No idea without looking at the sourcetext.

Anyway:
...
set the dgText[TRUE] of group "my DataGrid" to theData
...
ususally works for me! :D


Best

Klaus

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

Re: loading data into DataGrid

Post by Klaus » Mon Jul 30, 2012 5:19 pm

Another hint:
...
set the dgText[TRUE] of group "my DataGrid" to theData
...
will ONLY work if the number of TABS/items of the first line really fit exactly the number of items of the other lines!


Best

Klaus

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

Re: loading data into DataGrid

Post by cusingerBUSCw5N » Mon Jul 30, 2012 11:48 pm

got it... problem was html remnants thanks!

Post Reply