3.2.0

Best way to do?

I want to associate a row within the grid to a unique number (i.e. database id). I could create a column with this id, but then I would need to hide it - is there a better way?

Also, once I associate a row with an Id, can I retrieve this Id within the action "click" (I know I could use property "selection/index" after the user selects a row, but I do not want a function called if I preselect a row.

Thanks Steve
December 15,
You can use your unique IDs instead of default row indices (0 to row/count-1). You just need to pass array of your IDs to row/values property array:

obj.setRowValues([id1, id2, id3, ... ]);

In this case when the grid will be requesting data - it will provide your ID as a first argument. Same when getting or setting selection.

obj.setSelectionValues([id2, id3]);

Note the difference:
row/index property refers to datasource index (ID)
row/order property refers to display position

The only requirement to ID - it should only contain alphanumeric characters and be unique within this grid.

Good luck,
Alex
Alex (ActiveWidgets)
December 15,
The same is valid for column IDs. If you want to use your database field names instead of numbers:

obj.setColumnValues(["field1", "field2", ....]);

Then your getCoulmnText function should also respond to fieldN name, and not number anymore. And same with CSS formatting :-)

.active-column-field1 {width: 200px}
.active-column-field2 {text-align: right}

Again, limited to alphanumeric characters only (no spaces, dots etc) and should be unique.

Alex
Alex (ActiveWidgets)
December 15,
Alex,

Do you have an example of "setRowValues" - when I call it I get error "myData" is null or not an object. "myData" is the text for the grid.

Thanks
December 15,
If you want to use your own IDs instead of the row numbers - you cannot use the same data access myData[i][j] as before because "i" is your ID and not numeric index anymore.

Possible solution is to use associative array (collection):

myData["id1"] = ["text1-1", "text1-2" ...];
myData["id2"] = ["text2-1", "text2-2" ...];

or compact syntax:

var myData = {
"id1" : ["text1-1", "text1-2" ...],
"id2" : ["text2-1", "text2-2" ...]
}

(attention! - no comma at the end of the last line)

if you redefine your data object this way - you can keep simple data access method myData[i][j]

otherwise your getDataText function should be able to retrieve data using ID instead of row number.

P.S.
obj.setRowCount(n) is no longer necessary if you supply array of IDs with setRowValues.

I'll try to publish an example how to do this.
Alex
Alex (ActiveWidgets)
December 15,
Here is simple example:

var myData = {
"11111" : ["MSFT","Microsoft Corporation", "314,571.156", "32,187.000", "55000"],
"22222" : ["ORCL", "Oracle Corporation", "62,615.266", "9,519.000", "40650"],
"33333" : ["SAP", "SAP AG (ADR)", "40,986.328", "8,296.420", "28961"],
"44444" : ["CA", "Computer Associates Inter", "15,606.335", "3,164.000", "16000"],
"55555" : ["ERTS", "Electronic Arts Inc.", "14,490.895", "2,503.727", "4000"]
};

var myRows = ["11111", "22222", "33333", "44444", "55555"];




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

// set rows ids
obj.setRowValues(myRows);

// set number of columns
obj.setColumnCount(5);

// provide cells and headers text
obj.setDataText(function(i, j){return myData[i][j]});

// write grid html to the page
document.write(obj);



Alex (ActiveWidgets)
December 15,
Alex,

I got it to work when I redefined myData - Thanks. I did notice I get the same error "is null or not an object" when I click on a column header to sort the column - is this a bug?

Steve
December 15,
Yes, this is a bug :-(

Here is a patch for the sort function:

<script>

Active.Controls.Grid.patch = function(){

var obj = this.prototype;

obj.sort = function(index){
var a = {}, direction = "ascending";
var rows = this.getRowProperty("values");

if (this.getSortProperty("index") == index) {
if (this.getSortProperty("direction") == "ascending") {direction = "descending"}
rows.reverse();
}
else {
for (var i=0; i<rows.length; i++) {
var text = "" + this.getDataProperty("value", rows[i], index);
var value = Number(text.replace(/[ ,%$]/gi, "").replace(/((.*))/, "-$1"));
a[rows[i]] = isNaN(value) ? text.toLowerCase() + " " : value;
}
rows.sort(function(x,y){return a[x] > a[y] ? 1 : (a[x] == a[y] ? 0 : -1)});
}

this.setRowProperty("values", rows);
this.setSortProperty("index", index);
this.setSortProperty("direction", direction);
};

};

Active.Controls.Grid.patch();

</script>
Alex (ActiveWidgets)
December 15,
Hi Alex!

How can I user this ID script when I get the data from an external XML file?

Mike
March 2,
With the current implementation of XML model that might be difficult. I would rather suggest defining an additional hidden column which would hold a unique ID.
Alex (ActiveWidgets)
March 2,
Hi Alex,

I tried to sort the column in ascending order by default, somehow, it always set to descending order.
Here are the codes that I used.
obj.setProperty("sort/index", 1);
obj.setProperty("sort/direction", "ascending");

Thanks,
Monica
Monica
March 4,

This topic is archived.

See also:


Back to support forum