A table with variables inside a table

Hey,

So I’m making some kind of a restaurant app, and what I want is to retrieve values from a specific table that is inside another table. Currently looks like this:

local mainTable ={

  tab1 = {

  name=“Name1”;

  pic=“pic1.jpg”;

  };

  tab2 =

  {

  name=“Name2”;

  pic=nil;

  };

}

And it’s used when I launch for i = 1, _ do function. When i is 1, it should get the name, pic and other stuff from tab1, then it goes on and does the same for tab2. Is there a solution?

*Note: It’s my first day in Corona, formerly scripted Lua in Roblox, but everything is too different,

Thanks for help!

For a list like structure you are probably wanting a widget.newTableView for this. In our structure that tableView is its own structure and it has a method to insert rows into the tableView. So you create the tableView then you loop over your data telling it to insert records into the tableView. There is a listener function that most people name “onRowRender” that is responsible for drawing each individual row. It doesn’t know about any other rows.

Many people start off by assuming that tableView row 1 = data set row 1 and this is a bad assumption to make. You might want to allow people to rearrange the rows, delete rows, etc. at which point the rows being displayed may not be in sync any longer. The insert function allows you to pass a table of values that onRowRender can use to draw that row.

Assuming your tableView is called resturantView, you would have a loop like:

for i = 1, #mainTable do resturantView:insertRow({ isCategory = isCategory, rowHeight = rowHeight, rowColor = rowColor, lineColor = lineColor, params = { name = mainTable[i].name, pic = forecast[i].pic, id = i }, end

You will want to restructure your data to make it easier to use:

local mainTable = {} mainTable[1] =  {   name="Name1";   pic="pic1.jpg";   }; mainTable[2] = {   name="Name2";   pic=nil;   };

Rob

Thanks for useful stuff, but I managed to do it a bit differently, and ,apparently, easier for a scripting newbie like me.

In other script I inserted a table like this:

[lua]

– extTable.lua

maintab = {

{ – first table of maintab or maintab[1]

“string1”; “string2”; “string3”}; – getting these through maintab[1][1-3]

{ – second table or maintab[2]

“string1”; “string2”; “string3”}
} – end of main table

return maintab

[/lua]

And the other script has

[lua]local extTable=require(“extTable”) [/lua]

and now the extTable is the table I need. Thanks anyway, Rob!

For a list like structure you are probably wanting a widget.newTableView for this. In our structure that tableView is its own structure and it has a method to insert rows into the tableView. So you create the tableView then you loop over your data telling it to insert records into the tableView. There is a listener function that most people name “onRowRender” that is responsible for drawing each individual row. It doesn’t know about any other rows.

Many people start off by assuming that tableView row 1 = data set row 1 and this is a bad assumption to make. You might want to allow people to rearrange the rows, delete rows, etc. at which point the rows being displayed may not be in sync any longer. The insert function allows you to pass a table of values that onRowRender can use to draw that row.

Assuming your tableView is called resturantView, you would have a loop like:

for i = 1, #mainTable do resturantView:insertRow({ isCategory = isCategory, rowHeight = rowHeight, rowColor = rowColor, lineColor = lineColor, params = { name = mainTable[i].name, pic = forecast[i].pic, id = i }, end

You will want to restructure your data to make it easier to use:

local mainTable = {} mainTable[1] =  {   name="Name1";   pic="pic1.jpg";   }; mainTable[2] = {   name="Name2";   pic=nil;   };

Rob

Thanks for useful stuff, but I managed to do it a bit differently, and ,apparently, easier for a scripting newbie like me.

In other script I inserted a table like this:

[lua]

– extTable.lua

maintab = {

{ – first table of maintab or maintab[1]

“string1”; “string2”; “string3”}; – getting these through maintab[1][1-3]

{ – second table or maintab[2]

“string1”; “string2”; “string3”}
} – end of main table

return maintab

[/lua]

And the other script has

[lua]local extTable=require(“extTable”) [/lua]

and now the extTable is the table I need. Thanks anyway, Rob!