Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events

converting a table to a 2 D array

Avatar

Former Community Member

Hi,

     I want to access a table in the form and convert it to a 2 dimensional array - table consists of 2 columns country & region. There are multiple regions for a country for example we might have 10 regions for country A and 4 for B and so on.

My requirement is to read the table and create a 2 dimensional array from it.

array_object[i][j] where index 'i' represents country and index 'j' represents region.

The 2 D array will be populated in the following manner

array_object[1][0] = "United States"

array_object[1][1] = "Minnesota"           // Populated with states of United States

array_object[1][2] = "Texas"

..

array_object2][0] = "China" // Change in first index indicates change in country

array_object[2][1] = "Beijing"

array_object[2][2] = "Shanghai"    //Populated with states of China

The no of countries i.e index 'i' as well as 'j' are both variable meaning there can be variable number of regions for countries.

Therefore care should be taken to declare the array sizes based on the no of rows of the table and no of countries (i) and no of regions for each individual countries  (j).

Please let me know how to do solve this issue with a sample pdf and jave script.

Thanks

Rohit

4 Replies

Avatar

Former Community Member

The sample I posted originally (LinkedDropDowns.pdf) uses a script object to hard-code a two-dimensional array with provinces and states for Canada and the United States, respectively.

// form1.page1.#variables[0].countryScript - (JavaScript, client)

var myCountries = new Array(new Array(2), new Array(14), new Array(52));

myCountries[0][0] = " ";

myCountries[0][1] = " ";

myCountries[1][0] = "Canada";

myCountries[1][1] = "Alberta";

myCountries[1][2] = "British Columbia";

myCountries[1][3] = "Manitoba";

myCountries[1][4] = "New Brunswick";

myCountries[1][5] = "Newfoundland and Labrador";

myCountries[1][6] = "Northwest Territories";

myCountries[1][7] = "Nova Scotia";

myCountries[1][8] = "Nunavut";

myCountries[1][9] = "Ontario";

myCountries[1][10] = "Prince Edward Island";

myCountries[1][11] = "Quebec";

myCountries[1][12] = "Saskatchewan";

myCountries[1][13] = "Yukon";

myCountries[2][0] = "United States";

myCountries[2][1] = "Alabama";

myCountries[2][2] = "Alaska";

myCountries[2][3] = "Arizona";

myCountries[2][4] = "Arkansas";

myCountries[2][5] = "California";

myCountries[2][6] = "Colorado";

myCountries[2][7] = "Connecticut";

myCountries[2][8] = "Delaware";

myCountries[2][9] = "District of Columbia";

myCountries[2][10] = "Florida";

myCountries[2][11] = "Georgia";

myCountries[2][12] = "Hawaii";

myCountries[2][13] = "Idaho";

myCountries[2][14] = "Illinois";

myCountries[2][15] = "Indiana";

myCountries[2][16] = "Iowa";

myCountries[2][17] = "Kansas";

myCountries[2][18] = "Kentucky";

myCountries[2][19] = "Louisiana";

myCountries[2][20] = "Maine";

myCountries[2][21] = "Maryland";

myCountries[2][22] = "Massachusetts";

myCountries[2][23] = "Michigan";

myCountries[2][24] = "Minnesota";

myCountries[2][25] = "Mississippi";

myCountries[2][26] = "Missouri";

myCountries[2][27] = "Montana";

myCountries[2][28] = "Nebraska";

myCountries[2][29] = "Nevada";

myCountries[2][30] = "New Hampshire";

myCountries[2][31] = "New Jersey";

myCountries[2][32] = "New Mexico";

myCountries[2][33] = "New York";

myCountries[2][34] = "North Carolina";

myCountries[2][35] = "North Dakota";

myCountries[2][36] = "Ohio";

myCountries[2][37] = "Oklahoma";

myCountries[2][38] = "Oregon";

myCountries[2][39] = "Pennsylvania";

myCountries[2][40] = "Rhode Island";

myCountries[2][41] = "South Carolina";

myCountries[2][42] = "South Dakota";

myCountries[2][43] = "Tennessee";

myCountries[2][44] = "Texas";

myCountries[2][45] = "Utah";

myCountries[2][46] = "Vermont";

myCountries[2][47] = "Virginia";

myCountries[2][48] = "Washington";

myCountries[2][49] = "West Virginia";

myCountries[2][50] = "Wisconsin";

myCountries[2][51] = "Wyoming";

The updated version attached (LinkedDropdowns_.pdf) creates the two-dimensional array based upon the content of two strings. The first string "caCodeStr" contains the Canadian provinces and the second string "usCodeStr" contains the U.S. states. I create arrays from each string and then populate the two-dimensional array accordingly.

// form1.page1.#variables[0].countryScript - (JavaScript, client)

var caCodeStr = "Canada,AB,BC,MB,NB,NL,NS,NT,NU,ON,PE,QC,SK,YT";

var caCodeArray = new Array();

caCodeArray = caCodeStr.split(",");

var usCodeStr = "United States,AL,AK,AZ,AR,CA,CO,CT,DE,DC,FL,GA,HI,ID,IL,IN,IA,KS,KY,LA,ME,MT,NE,NV,NH,NJ,NM,NY,NC,ND,OH,OK,OR,MD,MA,MI,MN,MS,MO,PA,RI,SC,SD,TN,TX,UT,VT,VA,WA,WV,WI,WY";

var usCodeArray = new Array();

usCodeArray = usCodeStr.split(",");

var caCodeArrayLen = caCodeArray.length;

var usCodeArrayLen = usCodeArray.length;

var myCountries = new Array(new Array(2), new Array(caCodeArrayLen), new Array(usCodeArrayLen));

// initialize the first two items to " " so the first item in the drop-down for each country is blank

for (var i = 0; i < 2; i++) {

  myCountries[0][i] = " ";

}

// load Canadian provinces

for (var i = 0; i < caCodeArrayLen; i++) {

  myCountries[1][i] = caCodeArray[i];

}

// load U.S. states

for (var i = 0; i < usCodeArrayLen; i++) {

  myCountries[2][i] = usCodeArray[i];

}

If you can return strings from your database look-up you should be able to follow this model.
Steve

Avatar

Former Community Member

Hi Steve,

               I would like to know the array declaration if the no of countries is variable - in the sample provided you have taken 2 countries - what if the no of countries is variable and not fixed.

Thanks

Rohit

Avatar

Level 4

Depending on where you are getting the number of countries from, you can do an xfa.host.response() to have the user input the number of countries.  Or if you are populating it from a LC orchestration, you can stuff the value into a hidden field, or if you want it random, just do a random number generation and store that in a variable.  I'm assuming if you are trying to do every country, you'd have a database and would probably just make web service calls to get data from the database.

Avatar

Level 2

those linked dropdowns you posted were a massive improvement on the purchaseOrder example that came with livecycle 8.2

adding to the '2 dimentional' array was a nightmare before and the previous script had bugs - if you changed the country the state column didnt blank itself so you could be country canada state california

if only livecycle came with 20 or 30 example forms