Expand my Community achievements bar.

Create, or populate table with an array

Avatar

Level 2

Hi,

I have an array of data that I want to pass from a button to a function to create a table.

I just started using LiveCycle two hours ago.

I am very good with JavaScript and Acrobat.

But this has me stumped.

I found a page with a function, but it takes JSON and so I'm trying to decode it to use an array and allong the lines it's getting stopped up and I'm getting this error

GeneralError: Operation failed.

XFAObject._Row:33:XFA:form1[0]:#subform[0]:Button1[0]:click

Invalid property get operation; subform doesn't have property '_Row'

Here's the page I found

http://blogs.adobe.com/acdc/2009/02/dynamically_creating_a_table_w.html

and here's the code that I have modified.

form1.#variables[0].createTable - (JavaScript, client)

/**

*          Create rows and columns based on the number of data items and number of attributes

*/

function createTable(product, table)

{

// Evaluate JSON string into a array of JavaScript objects

// See the StringSO script object for the prototype function

//var data = jsonString.evalJSON();

//var attributes = new Array();

//var attributeCounter = 0;

var row = table.somExpression + ".Row";

var pageWidth = 8; // inches;

var cellWidth;

var columnWidthsString = "";

// Get all attributes

/*

for(var a in data[0])

{

attributes[attributeCounter++] = a;

}

*/

// Calculate the cell width based on number of columns and page width

cellWidth = .5 //pageWidth/attributes.length;

// Create first column for all rows

for(i = 0; i < 10; i++)//consider. product.length, was data.length

{

table._Row.addInstance(0);

}

// creating columns

for(i = 0; i < attributes.length; i++)

{

for(var n = 0; n <= data.length; n++)

{

var column = row + "[" + n + "].Column[" + i + "]";

var cell = column + ".Cell";

// Set the value in the cell

if(n == 0)

{

xfa.resolveNode(cell).rawValue = attributes[i];

}

else

{

xfa.resolveNode(cell).rawValue = data[n-1][attributes[i]] + "";

}

if(i < attributes.length - 1)

{

var t = row + "[" + n + "]";

xfa.resolveNode(t)._Column.addInstance(0);

}

// Set cell width

xfa.resolveNode(cell).w = cellWidth + "in";

}

// Building column widths string

columnWidthsString += cellWidth + "in ";

}

// Set column widths

table.columnWidths = columnWidthsString;

// Make the table visible

table.presence = "visible";

}

I basically started commenting stuff out to figure out what does what and I was going to let the errors in the console show me the way, but I'm stuck on _Row. It's throwing an error, even though this looks like a built in property.

So I'm thinking maybe I set up my table and form wrong from the start.

If anyone has a way to do this that doesn't involve scripting I'd be open to that as well. Thanks.

2 Replies

Avatar

Level 7

Do you need to create the table from the button? Could you just create the table in your design, then hide it and make it visible when you click the button? Do you know how many columns will be in the table beforehand? Or is the array two-dimensional and varying?

If you can go ahead and create the table first, then hide it in your design, this code will make the table visible and add items to the rows. I don't know where your array is coming from, so I just threw one in the script.

var stuff = new Array("tom", "dick", "harry");

Table1.presence = "visible";

for (i=0; i< stuff.length; i++){

    if (i>0) Table1.Row1.instanceManager.addInstance();

    xfa.resolveNode("Table1.Row1["+i+"].TextField1").rawValue = stuff[i];

}

Avatar

Level 2

Thanks Jasotastic,

That's basically what I ended up doing. Had to change the code quite a bit though as the sample I found was older and the syntax was not correct for the newer version. What I ended up doing was making several buttons which on click would then define an object. I then passed that object to a function and that did the work. But yes, the table just sits on the page, I don't even need to hide it, but it adjusts the number of rows based on each object passed.