Index is reset in arrays after filtering
Hello,
thanks to this forum I got a solution for filtering data with paging. Having done this I wanted to output some of the data from a selected line to a text field. This is being done using a function:
var message = function(){
window.status = document.inquiryForm.ticker.value=myData[this.getSelectionProperty("index")][0];
}
The above function works fine so long as the data is not filtered. But after filtering returns the data from the second array GlobData and not the array myData which is being displayed. By adding alerts at various points in the code I have found out that the FilterGrid function is correctly setting new index values to the myData array after filtering. After leaving the FilterGrid function it seems that the index values of myData are reset to those in GlobData. The complete code demonstrating this can be found below - any ideas?
<html>
<head>
<title>ActiveWidgets Grid</title>
<style> body, html {margin:0px; padding: 0px; overflow: hidden;} </style>
<!-- ActiveWidgets stylesheet and scripts -->
<link href="../runtime/styles/xp/grid.css" rel="stylesheet" type="text/css" ></link>
<script src="../runtime/lib/grid.js"></script>
<!-- Include patches here -->
<script src="../patches/paging1.js"></script>
<!-- grid format -->
<style>
.active-controls-grid {height: 310px; font: menu;}
.active-column-0 {width: 80px;}
.active-column-1 {width: 200px; background-color: threedlightshadow;}
.active-column-2 {text-align: right;}
.active-column-3 {text-align: right;}
.active-column-4 {text-align: right;}
.active-grid-column {border-right: 1px solid threedshadow;}
.active-grid-row {border-bottom: 1px solid threedlightshadow;}
</style>
<!-- grid data -->
<script>
var message = function(){
window.status = "index=" + this.getSelectionProperty("index") + GlobData[this.getSelectionProperty("index")][0]; document.inquiryForm.ticker.value=myData[this.getSelectionProperty("index")][0];
}
var Datalen = 0;
var myData =[];
var GlobData =[];
var rowidValues = [];
var resfilter = "ALL";
var restot = "ALL";
var myColumns = ["Ticker", "Company Name", "Market Cap.", "$ Sales", "Employees"];
// create ActiveWidgets data model - text-based table
var table = new Active.Text.Table;
// provide data URL - plain text comma-separated file
table.setURL("../data/companies.csv");
// create javascript object
var obj = new Active.Controls.Grid;
obj.setId("grid1");
obj.setModel("row", new Active.Rows.Page);
var defaultResponse = table.response;
//begin table response
table.response = function(data){
defaultResponse.call(table, data);
Datalen=table.getCount();
//load CSV and convert to array
for(var x=0; x< Datalen; x++) {
GlobData.push([table.getText( x, 0), table.getText( x, 1), table.getText( x, 2), table.getText( x, 3), table.getText( x, 4)]);
}
//Clone array (GlobData allways stores all csv items)
myData = GlobData;
restot = Datalen;
obj.setRowProperty("count", Datalen);
obj.setColumnProperty("count", 5);
obj.getDataText = function(i, j){return myData[i][j]};
obj.setDataText = function(value, i, j){myData[i][j] = value};
obj.setColumnProperty("text", function(i){return myColumns[i]});
obj.setProperty("row/pageSize", 15);
obj.setAction("selectionChanged", message);
// SET TIMEOUT ( )
window.setTimeout(function(){
goToPage(0);
obj.refresh();
}, 0);
} //(end of tableresponse method)
// start asyncronous data retrieval
table.request();
function populateListBox() {
for(var c=0; c<myColumns.length; c++) {
document.write("<option value="+c+">"+myColumns[c]+"</option>");
}
}
//*********************************************
function FilterGrid()
{
var myData =[];
resetRowValuesGrid1()
var colToBeSearched = document.forms['gridSearchForm'].colSearchName.value;
var toSearch = document.forms['gridSearchForm'].keyword.value;
var rowidValues = [];
var res = 0;
for(var x=0; x<GlobData.length; x++)
{
if((GlobData[x][colToBeSearched].indexOf(toSearch)) >= 0) {
myData.push(GlobData[x]);
res++;
}
}
obj.sort(0, "ascending");
resfilter = res;
obj.setModel("row", new Active.Rows.Page);
obj.setRowProperty("count", resfilter);
obj.setColumnProperty("count", 5);
obj.getDataText = function(i, j){return myData[i][j]};
obj.setDataText = function(value, i, j){myData[i][j] = value};
obj.setColumnProperty("text", function(i){return myColumns[i]});
obj.setProperty("row/pageSize", 15);
obj.setAction("selectionChanged", message);
goToPage(0);
document.getElementById('result').innerHTML = "<b>Number of matches : "+resfilter+" of: " + restot +"</b>";
alert("myData:" + myData[0] + "\n" + "GlobData:" + GlobData[0] );
}
///***************************************
//**************************************
// RELOAD ROW-VALUES (AFTER ROWCOUNT IS NEEDED)
//*************************************
function resetRowValuesGrid1()
{
obj.setRowCount(myData.length);
var rowValues = [];
for(var i=0; i < myData.length; ++i) { rowValues.push(i);}
obj.setRowProperty("values", rowValues);
obj.setSortProperty("index", null);
}
//********************************
</script>
</head>
<body>
<script>
// write grid html to the page
document.write(obj);
</script>
<!-- bottom page control buttons -->
<div>
<button onclick='goToPage(-Infinity)'><<</button>
<button onclick='goToPage(-1)'><</button>
<span id='pageLabel'></span>
<button onclick='goToPage(1)'>></button>
<button onclick='goToPage(Infinity)'>>></button>
</div>
<script>
function goToPage(delta){
var count = obj.getProperty("row/pageCount");
var number = obj.getProperty("row/pageNumber");
number += delta;
if (number < 0) {number = 0}
if (number > count-1) {number = count-1}
document.getElementById('pageLabel').innerHTML = "Page " + (number + 1) + " of " + count + " ";
obj.setProperty("row/pageNumber", number);
obj.refresh();
}
</script>
<form name=gridSearchForm id=gridSearchForm>
<input type=text name=keyword>
<select name=colSearchName>
<script>
populateListBox();
</script>
</select><input type=button value=" Filter " onclick="javascript:FilterGrid();">
</form>
<div id=result>
</div>
<script>
document.getElementById('result').innerHTML = "<b>Number of matches : "+resfilter+" OF: " + restot +"</b>";
</script>
<form name=inquiryForm id=inquiryForm>
Ticker: <input type=text name=ticker>
<input type=button value = "Get" onclick='javascript:alert("myData:" + myData[0] + "\n" + "GlobData:" + GlobData[0] );'>
</form>
</body>
</html>
thanks to this forum I got a solution for filtering data with paging. Having done this I wanted to output some of the data from a selected line to a text field. This is being done using a function:
var message = function(){
window.status = document.inquiryForm.ticker.value=myData[this.getSelectionProperty("index")][0];
}
The above function works fine so long as the data is not filtered. But after filtering returns the data from the second array GlobData and not the array myData which is being displayed. By adding alerts at various points in the code I have found out that the FilterGrid function is correctly setting new index values to the myData array after filtering. After leaving the FilterGrid function it seems that the index values of myData are reset to those in GlobData. The complete code demonstrating this can be found below - any ideas?
<html>
<head>
<title>ActiveWidgets Grid</title>
<style> body, html {margin:0px; padding: 0px; overflow: hidden;} </style>
<!-- ActiveWidgets stylesheet and scripts -->
<link href="../runtime/styles/xp/grid.css" rel="stylesheet" type="text/css" ></link>
<script src="../runtime/lib/grid.js"></script>
<!-- Include patches here -->
<script src="../patches/paging1.js"></script>
<!-- grid format -->
<style>
.active-controls-grid {height: 310px; font: menu;}
.active-column-0 {width: 80px;}
.active-column-1 {width: 200px; background-color: threedlightshadow;}
.active-column-2 {text-align: right;}
.active-column-3 {text-align: right;}
.active-column-4 {text-align: right;}
.active-grid-column {border-right: 1px solid threedshadow;}
.active-grid-row {border-bottom: 1px solid threedlightshadow;}
</style>
<!-- grid data -->
<script>
var message = function(){
window.status = "index=" + this.getSelectionProperty("index") + GlobData[this.getSelectionProperty("index")][0]; document.inquiryForm.ticker.value=myData[this.getSelectionProperty("index")][0];
}
var Datalen = 0;
var myData =[];
var GlobData =[];
var rowidValues = [];
var resfilter = "ALL";
var restot = "ALL";
var myColumns = ["Ticker", "Company Name", "Market Cap.", "$ Sales", "Employees"];
// create ActiveWidgets data model - text-based table
var table = new Active.Text.Table;
// provide data URL - plain text comma-separated file
table.setURL("../data/companies.csv");
// create javascript object
var obj = new Active.Controls.Grid;
obj.setId("grid1");
obj.setModel("row", new Active.Rows.Page);
var defaultResponse = table.response;
//begin table response
table.response = function(data){
defaultResponse.call(table, data);
Datalen=table.getCount();
//load CSV and convert to array
for(var x=0; x< Datalen; x++) {
GlobData.push([table.getText( x, 0), table.getText( x, 1), table.getText( x, 2), table.getText( x, 3), table.getText( x, 4)]);
}
//Clone array (GlobData allways stores all csv items)
myData = GlobData;
restot = Datalen;
obj.setRowProperty("count", Datalen);
obj.setColumnProperty("count", 5);
obj.getDataText = function(i, j){return myData[i][j]};
obj.setDataText = function(value, i, j){myData[i][j] = value};
obj.setColumnProperty("text", function(i){return myColumns[i]});
obj.setProperty("row/pageSize", 15);
obj.setAction("selectionChanged", message);
// SET TIMEOUT ( )
window.setTimeout(function(){
goToPage(0);
obj.refresh();
}, 0);
} //(end of tableresponse method)
// start asyncronous data retrieval
table.request();
function populateListBox() {
for(var c=0; c<myColumns.length; c++) {
document.write("<option value="+c+">"+myColumns[c]+"</option>");
}
}
//*********************************************
function FilterGrid()
{
var myData =[];
resetRowValuesGrid1()
var colToBeSearched = document.forms['gridSearchForm'].colSearchName.value;
var toSearch = document.forms['gridSearchForm'].keyword.value;
var rowidValues = [];
var res = 0;
for(var x=0; x<GlobData.length; x++)
{
if((GlobData[x][colToBeSearched].indexOf(toSearch)) >= 0) {
myData.push(GlobData[x]);
res++;
}
}
obj.sort(0, "ascending");
resfilter = res;
obj.setModel("row", new Active.Rows.Page);
obj.setRowProperty("count", resfilter);
obj.setColumnProperty("count", 5);
obj.getDataText = function(i, j){return myData[i][j]};
obj.setDataText = function(value, i, j){myData[i][j] = value};
obj.setColumnProperty("text", function(i){return myColumns[i]});
obj.setProperty("row/pageSize", 15);
obj.setAction("selectionChanged", message);
goToPage(0);
document.getElementById('result').innerHTML = "<b>Number of matches : "+resfilter+" of: " + restot +"</b>";
alert("myData:" + myData[0] + "\n" + "GlobData:" + GlobData[0] );
}
///***************************************
//**************************************
// RELOAD ROW-VALUES (AFTER ROWCOUNT IS NEEDED)
//*************************************
function resetRowValuesGrid1()
{
obj.setRowCount(myData.length);
var rowValues = [];
for(var i=0; i < myData.length; ++i) { rowValues.push(i);}
obj.setRowProperty("values", rowValues);
obj.setSortProperty("index", null);
}
//********************************
</script>
</head>
<body>
<script>
// write grid html to the page
document.write(obj);
</script>
<!-- bottom page control buttons -->
<div>
<button onclick='goToPage(-Infinity)'><<</button>
<button onclick='goToPage(-1)'><</button>
<span id='pageLabel'></span>
<button onclick='goToPage(1)'>></button>
<button onclick='goToPage(Infinity)'>>></button>
</div>
<script>
function goToPage(delta){
var count = obj.getProperty("row/pageCount");
var number = obj.getProperty("row/pageNumber");
number += delta;
if (number < 0) {number = 0}
if (number > count-1) {number = count-1}
document.getElementById('pageLabel').innerHTML = "Page " + (number + 1) + " of " + count + " ";
obj.setProperty("row/pageNumber", number);
obj.refresh();
}
</script>
<form name=gridSearchForm id=gridSearchForm>
<input type=text name=keyword>
<select name=colSearchName>
<script>
populateListBox();
</script>
</select><input type=button value=" Filter " onclick="javascript:FilterGrid();">
</form>
<div id=result>
</div>
<script>
document.getElementById('result').innerHTML = "<b>Number of matches : "+resfilter+" OF: " + restot +"</b>";
</script>
<form name=inquiryForm id=inquiryForm>
Ticker: <input type=text name=ticker>
<input type=button value = "Get" onclick='javascript:alert("myData:" + myData[0] + "\n" + "GlobData:" + GlobData[0] );'>
</form>
</body>
</html>
Andrew
August 17,