:: Forum >>

How to create myData 2-dimensional array dynamically for a grid?

Hi, guys, I want to create a grid based on the data of an object array. Now I can only create a string for myData. I think it's not right. But in javascript, no 2-dimensional array can be used, right? My following code doesn't work, and computer complain the object is null. I think the myData is not created correctly. Thanks.

<script>

var info=new Array();

function accountdata(accName, tableID )
{
this.accName=accName ;
this.tableID=tableID;
}

info[0]=new accountdata("14210462", "tablecontent0" );

info[1]=new accountdata("14210470", "tablecontent1" );

info[2]=new accountdata("14210488", "tablecontent2" );

////////////creating myData.......////////////////////////////
var myData="";
for(i=0; i<3; i++)
{
myData=myData+
"["+
"\"" + info[i].accName + "\"," +
"\"" + info[i].tableID + "\"," +
"],";
}

myData=myData.substring(0,myData.length-1);
myData="["+myData+"]";

var myColumns = ["accountName", "TableID"];
var obj = new Active.Controls.Grid;
obj.setRowProperty("count", 20);
obj.setColumnProperty("count", 2);
obj.setDataProperty("text", function(i, j){return myData[i][j]});
obj.setColumnProperty("text", function(i){return myColumns[i]});
obj.setRowHeaderWidth("28px");
obj.setColumnHeaderHeight("20px");
obj.setAction("click", function(src){window.status = src.getItemProperty("text")});
document.write(obj);

</script>
Jack
Monday, August 23, 2004
This code will not work.

var myVar1 = "Hello";
var myVar2 = "World";
myArray = "['" + myVar1 + "','" + myVar2 + "']";
alert(myArray[0] + " " +myArray[1]);


Rather this will do what you want to do

var myVar1 = "Hello";
var myVar2 = "World";
var tempCode = "myArray = ['" + myVar1 + "','" + myVar2 + "']";
eval(tempCode);
alert(myArray[0] + " " +myArray[1]);


Hope this helps.

To solve your problem, what I would do is -

obj.getDataText = function(i, j){
switch(j) {
case 0:
return info[i].accName;
case 1:
return info[i].tableID;
default:
alert('beat me, coz it should never run...');
}
}

Sudhaker Raj
Monday, August 23, 2004
Thank you, Sudhaker. But I don't know how to set the myData, which is still empty. You mean myData=tempCode?
jack
Monday, August 23, 2004
should do it like this:
var myVar1 = "Hello";
var myVar2 = "World";
var myData = "['" + myVar1 + "','" + myVar2 + "']";
eval(myData);

It doesn't work
jack
Monday, August 23, 2004
I got it. thanks a lot.
jack
Monday, August 23, 2004

I'll suggest you using code with switch/case block. I also suggest making 'info' a member of grid object. That way, info will be protected from accidental modification.

For ref purpose - complete code is included.

function accountdata(accName, tableID )
{
this.accName=accName ;
this.tableID=tableID;
}

obj.info = new Array();

obj.addAccount = function(accName, tableID) {

// push the object in data holder
this.info.push(new accountdata(accName, tableID));
this.rowCountChanged();
};

obj.rowCountChanged = function() {

// set data count
var newCount = this.gridData.length;
this.setDataProperty("count", newCount);
// clear internal row state
var rowValues = [];
for(var i=0; i < newCount; ++i) { rowValues.push(i); }
this.setRowProperty("values", rowValues);
this.setSortProperty("index", null);
// clear selection - if required
//this.setSelectionProperty("index", -1);
//this.setSelectionProperty("values", []);

this.refresh();
};

obj.getDataText = function(i, j){
switch(j) {
case 0:
return this.info[i].accName;
case 1:
return this.info[i].tableID;
default:
alert('beat me, coz it should never run...');
}
}

obj.setDataText = function(value, i, j){
switch(j) {
case 0:
this.info[i].accName = value;
break;
case 1:
this.info[i].tableID = value;
break;
default:
alert('beat me, coz it should never run...');
}
}

.....
.....
Sudhaker Raj
Monday, August 23, 2004

This topic is archived.


Back to support forum

Forum search