Bug: cannot reload/change data after sort
There is a long standing bug which prevents loading different data set after the grid was sorted either by clicking on the column header or calling sort() method.
This bug is related to the fact that in some cases it is necessary to reload the same dataset to reflect changed values but keep current selection and sort order. I still don't know what is the best way to distinguish between this scenario and the one where you load different data set and want to reset/clear selection and sort order (any suggestions are welcome :-).
There were already several workarounds in the forum but I want to publish them again as the old ones seem to be difficult to find.
Changing JS array
-----------------
When changing or replacing js array source you have to clear row, sort and selection models:
Full page example:
-----------------------------------------------------
Reloading XML data
-----------------------------------------------------
When loading different XML file you have to clear row, sort and selection models. You can add this code to the Active.HTTP.Request class:
Full example (assumes you have two xml files: list1.xml and list2.xml)
This bug is related to the fact that in some cases it is necessary to reload the same dataset to reflect changed values but keep current selection and sort order. I still don't know what is the best way to distinguish between this scenario and the one where you load different data set and want to reset/clear selection and sort order (any suggestions are welcome :-).
There were already several workarounds in the forum but I want to publish them again as the old ones seem to be difficult to find.
Changing JS array
-----------------
When changing or replacing js array source you have to clear row, sort and selection models:
// clear row model
obj.setRowProperty("value", function(i){return i});
obj.setRowProperty("order", function(i){return i});
obj.setRowProperty("count", myData.length);
// clear selection model
obj.setSelectionProperty("index", -1);
// clear sort model
obj.setSortProperty("index", -1);
obj.setSortProperty("direction", "none");
obj.refresh();
Full page example:
<html>
<head>
<link href="../../runtime/styles/xp/grid.css" rel="stylesheet" type="text/css" ></link>
<script src="../../runtime/lib/grid.js"></script>
<style>
.active-controls-grid {height: 120px; border: 1px solid #ccc; font: menu;}
</style>
<script>
var myData1 = [
["MSFT","Microsoft Corporation", "314,571.156", "32,187.000", "55000"],
["ORCL", "Oracle Corporation", "62,615.266", "9,519.000", "40650"],
["SAP", "SAP AG (ADR)", "40,986.328", "8,296.420", "28961"],
["CA", "Computer Associates Inter", "15,606.335", "3,164.000", "16000"],
["ERTS", "Electronic Arts Inc.", "14,490.895", "2,503.727", "4000"]
];
var myData2 = [
["SFTBF", "Softbank Corp. (ADR)", "14,485.840", ".000", "6865"],
["VRTS", "Veritas Software Corp.", "14,444.272", "1,578.658", "5647"],
["SYMC", "Symantec Corporation", "9,932.483", "1,482.029", "4300"]
];
var myColumns = [
"Ticker", "Company Name", "Market Cap.", "$ Sales", "Employees"
];
</script>
</head>
<body>
<button onclick="setNewData(myData1)">Dataset 1</button>
<button onclick="setNewData(myData2)">Dataset 2</button>
<script>
var myData = myData1;
var obj = new Active.Controls.Grid;
obj.setRowProperty("count", myData.length);
obj.setColumnProperty("count", 5);
obj.setDataProperty("text", function(i, j){return myData[i][j]});
obj.setColumnProperty("text", function(i){return myColumns[i]});
document.write(obj);
function setNewData(dataArray){
myData = dataArray;
// clear row model
obj.setRowProperty("value", function(i){return i});
obj.setRowProperty("order", function(i){return i});
obj.setRowProperty("count", myData.length);
// clear selection model
obj.setSelectionProperty("index", -1);
// clear sort model
obj.setSortProperty("index", -1);
obj.setSortProperty("direction", "none");
obj.refresh();
}
</script>
</body>
</html>
-----------------------------------------------------
Reloading XML data
-----------------------------------------------------
When loading different XML file you have to clear row, sort and selection models. You can add this code to the Active.HTTP.Request class:
// fixing 'response' method
Active.HTTP.Request.prototype.response = function(){
var obj = this.$owner;
if (!obj) {return}
// clear row model
obj.setRowProperty("value", function(i){return i});
obj.setRowProperty("order", function(i){return i});
obj.setRowProperty("count", this.getCount());
// clear selection model
obj.setSelectionProperty("index", -1);
// clear sort model
obj.setSortProperty("index", -1);
obj.setSortProperty("direction", "none");
obj.refresh();
}
Full example (assumes you have two xml files: list1.xml and list2.xml)
<html>
<head>
<link href="../../runtime/styles/classic/grid.css" rel="stylesheet" type="text/css" ></link>
<script src="../../runtime/lib/grid.js"></script>
<style>
.active-controls-grid {height: 200px; font: menu;}
</style>
</head>
<body>
<button onclick="newData('list1.xml')">Dataset 1</button>
<button onclick="newData('list2.xml')">Dataset 2</button>
<script>
// fixing 'response' method
Active.HTTP.Request.prototype.response = function(){
var obj = this.$owner;
if (!obj) {return}
// clear row model
obj.setRowProperty("value", function(i){return i});
obj.setRowProperty("order", function(i){return i});
obj.setRowProperty("count", this.getCount());
// clear selection model
obj.setSelectionProperty("index", -1);
// clear sort model
obj.setSortProperty("index", -1);
obj.setSortProperty("direction", "none");
obj.refresh();
}
var columns = ["Ticker", "Company Name", "Market Cap.", "$ Sales", "Employees"];
var table = new Active.XML.Table;
table.setURL("list1.xml");
table.request();
var obj = new Active.Controls.Grid;
obj.setColumnProperty("texts", columns);
obj.setDataModel(table);
document.write(obj);
function newData(URL){
table.setURL(URL);
table.request();
}
</script>
</body>
</html>
Alex (ActiveWidgets)
October 19,