3.2.0

having problem creating a jsArray from Data returned from CGI script...

I have data that gets returned from a CGI script like below.

'A','A1','A2'
'B','B1','B2'
'C','C1','C2'
'D','D1','D2'

I am needing to put it into a js array for the variable myData:

var myData = [
["A","A1", "A2"],
["B","B1", "B2"],
["C","C1", "C2"],
["D","D1", "D2"],
];


From this CGI script I am trying to create the array for myData by first splitting the data into an array of substrings.

// return value from CGI script
ctext = http.responseText;
ctext = ctext.split("\n");

Then I take that array of substrings and try to create the myData array.

var test = [];

for (var i=0; i<ctext.length-1; i++){
test += "[" + ctext[i] + "],\n";
}

var myData = test;

// data source
var myData = test;


// create grid object
var obj = new Active.Controls.Grid;

// set number of columns/rows
obj.setColumnCount(1);
obj.setRowCount(Dlen);

// link to cell text
obj.setDataText(function(i,j){return myData[i][j]});

document.write(obj);


When I run it and it goes through the rest of my code it gives me the error:

'mydata[...]' is null or not an object

on this line: obj.setDataText(function(i,j){return myData[i][j]});

Can anyone help me? I am confused.
Anthony
August 24,
In your sample there are 4 elements 0,1,2,3 so:
for (var i=0; i<ctext.length; i++){
but not
for (var i=0; i<ctext.length-1; i++){
August 24,
I am using an existing CGI script that will return me a list of data from MySQL.

Here is how the CGI returns the data.
print "\"$str\"\n";

My javascript picks up that data....
ctext = http.responseText;


So the data (ctext) looks like this when it is returned....
"AAA","BBB","CCC"
"DDD","EEE","FFF"

I need to take each one of those rows in the string (ctext) and put them into an array.
So I did it like this....
ctext = ctext.split("\n");

Now I need to take that array of strings I created and make it into a multidimensional array similar to the multidimensional array for AW....

var myData [
["AAA","BBB","CCC"],
["DDD","EEE","FFF"]
];


So I am not sure if I am doing this right or not, or if this is the best way to go about this.

I cannot modify the CGI script cause it is used for something else, so I am just trying to use it's existing returned value to create the multidimensional array out of it within my Javascript to work with AW.

I am getting closer, but not sure if this is correct yet. It seems to make each row but put it all into the first column.

column1 column2 column3
row1 AAA,BBB,CCC
row2 DDD,EEE,FFF


Can anyone let me know if going in the right direction to what I need to accomplish from my explanation in the above post?


for (var i=0; i<ctext.length; i++){
Xarray[i] = new Array(ctext[i]);
}


or is this not how I need to get the returned data from the CGI script into the first array??
ctext = ctext.split("\n");


I am thinking that I need to refigure out how to get the returned data from the CGI script in to an array instead of a string of array. So that ctext[0] would be "AAA", and ctext[1] would be "BBB", and so on. Because these will be in the columns within my final table in the Active Widget scripts. Then my second deminsional array will put those columns into the rows.

column1 column2 column3
row1 AAA BBB CCC
row2 DDD EEE FFF


I am still confused as ever..... how to get my CGI script output to end up looking like the output that is needed to work in the AW.....
Anthony
August 25,
I think you were close with your first attempt. The problem is that your 'array' temp ended up not being an array, it ended up being a string. Also, I have had issues assigning an array to an undeclared var, the var does not always react as an array. If you declare it first, then assign it things work better. I suggest the following changes to your first code:

Change this:
var test = [];

for (var i=0; i<ctext.length-1; i++){
test += "[" + ctext[i] + "],\n";
}

var myData = test;

// data source
var myData = test;


to:
var myData = new Array();

for (var i=0; i<ctext.length; i++){  // you had an "off by one" error here
  eval("myData.push([" + ctext[i] + "]);");
}


give that a try
Jim Hunter
August 25,
figured it out.......


ctext = ctext.split("\n");
for (i=0; i<ctext.length; i++){
var x = ctext[i];
x = x.split(",");
ctext[i] = x;
}
Anthony
August 25,
Hi,

I am using trial version like the product just stuck on this;

I need to pick out the text returned in the r.response() and insert it into an array called myCells. I have looked at examples and think I am close, just not sure how to put returned data into the array. How do I do place the response into my array? When I view it using alert(data) it looks fine, but I suspect its a string. In the alert window it looks fine, like this;

[
["row1c1","row1c2",row1c3"],
["row2c1","row2c2","row2c3"],
["row3c1","row3c2","row3c3"],
]


********here is my code*******

var r = new AW.HTTP.Request;
r.setURL("myreturnarray.cgi");
r.setRequestMethod("POST");
r.setParameter("column", column);
r.setParameter("row", row);
r.setParameter("text", text);
r.request();

r.response = function(data){
alert(data); // process response data

var myCells = r.response();

}
********end of code************


I have tried the following also

ctext = r.response()

var myCells = ctext;

Nothing seems to work.

Jeff
Jeff
April 27,
You should use eval() function to parse the string into js data structure, for example,

var x = eval("[1, 2]");
alert(x[0]);

Alex (ActiveWidgets)
April 27,
Alex,

I Can do this okay;

rs.response = function(data){
var x = eval(data);
alert(x[3][3]);
}
This displays the proper element of the sub array in the alert window. Played with various elements of array, No problem here. Its when I try to use x a few lines down, see last line of code below. I am new to js and expect that x is out of scope. Is there a way to pass x into function(data,x) above and also return x from this function so it does not go out of scope. The code below shows the headers fine but shows no cell data.

var myHeaders = ["Pen", "Gender", "Feed", "Treatment"];
var obj = new AW.UI.Grid;
obj.setCellText(x);
obj.setHeaderText(myHeaders);
obj.setColumnCount(4);
obj.setRowCount(20);

in the alert window the array is listed exactly like you see below in one long string if I use eval(data);

r1c1,r1c2,r1c3,r1c4,r2c1,r2c2,r2c3,r2c4,r3c1,r3c2,r3c3,....

if i use no eval() function then the code below

var x = data;

displays in the alert window text exactly like this

[
["r1c1","r1c2","r1c3","r1c4"],
...
["r2c1","r2c2","r2c3","r2c4"]
]

So how do I get either

var x = eval(data)

or

x = data;

to properly load and display in the grid, as shown below

obj.setCellText(x);

any advice is appreciated.

do I have to do the split thing?


Jeff
April 27,
Maybe this way

rs.response = function(data){
var cellsArray = eval(data);
obj.setCellText(cellsArray);
}
Alex (ActiveWidgets)
April 28,
That did it it. May have had it before but may not have been refreshing properly.

thanks...great library

We will be purchasing product.
Jeff
April 28,

This topic is archived.

See also:


Back to support forum