CSV to Array
I try to load a CSV file into Array , but seems that, the way I did it myData does't exist out of "table.response = function" , any Ideas ? or someone do it in a diferent way?
Thanks
var Datalen = 0;
var myData =[];
// create ActiveWidgets data model - text-based table
var table = new Active.Text.Table;
// provide data URL - plain text comma-separated file
table.setURL("companies.csv");
var defaultResponse = table.response;
table.response = function(data){
defaultResponse.call(table, data);
Datalen=table.getCount();
for(var x=0; x< Datalen; x++) {
fieldcolum0 = table.getText( x, 0);
fieldcolum1 = table.getText( x, 1);
fieldcolum2 = table.getText( x, 2);
fieldcolum3 = table.getText( x, 3);
fieldcolum4 = table.getText( x, 4);
myData.push([fieldcolum0, fieldcolum1, fieldcolum2, fieldcolum3, fieldcolum4]);
}
alert(myData[0][0]);
}
// start asyncronous data retrieval
table.request();
// create javascript object
var obj = new Active.Controls.Grid;
// set unique id
obj.setId("grid1");
obj.setRowProperty("count", Datalen);
obj.setColumnProperty("count", myColumns.lenght);
obj.setDataProperty("text", function(i, j){return myData[i][j]});
obj.setColumnProperty("text", function(i){return myColumns[i]});
alert(myData[1][2]);
Carlos
October 25,
Sorry typing mistake, "lenght" instead of "length", works, but the issue now is ... sometimes need to refresh manually to show the data .
How could I implement a Timeout in this example?
Thanks
Carlos
October 26,
OK, solved, Posting full code to be executed in .../examples/grid/
Thanks
Carlos
<html>
<head>
<title>ActiveWidgets Grid :: Examples</title>
<style> body, html {margin:0px; padding: 0px; overflow: hidden;} </style>
<!-- ActiveWidgets stylesheet and scripts -->
<link href="../../runtime/styles/classic/grid.css" rel="stylesheet" type="text/css" ></link>
<script src="../../runtime/lib/grid.js"></script>
<!-- grid format -->
<style>
.active-controls-grid {height: 100%; 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>
</head>
<body>
<script>
// 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");
var Datalen = 0;
var myData =[];
var defaultResponse = table.response;
table.response = function(data){
defaultResponse.call(table, data);
Datalen=table.getCount();
for(var x=0; x< Datalen; x++) {
myData.push([table.getText( x, 0), table.getText( x, 1), table.getText( x, 2), table.getText( x, 3), table.getText( x, 4)]);
}
}
// start asyncronous data retrieval
table.request();
// define Timeout
setTimeout('obj.refresh()', 1000);
// define column labels
var columns = ["Ticker", "Company Name", "Market Cap.", "$ Sales", "Employees"];
// create javascript object
var obj = new Active.Controls.Grid;
obj.setRowProperty("count", Datalen);
obj.setColumnProperty("count", myColumns.length);
obj.setDataProperty("text", function(i, j){return myData[i][j]});
obj.setColumnProperty("text", function(i){return myColumns[i]});
// write grid html to the page
document.write(obj);
</script>
</body>
</html>
//***********************
October 26,
Sorry posting ...again
//*************************************************
<html>
<head>
<title>ActiveWidgets Grid :: Examples</title>
<style> body, html {margin:0px; padding: 0px; overflow: hidden;} </style>
<!-- ActiveWidgets stylesheet and scripts -->
<link href="../../runtime/styles/classic/grid.css" rel="stylesheet" type="text/css" ></link>
<script src="../../runtime/lib/grid.js"></script>
<!-- grid format -->
<style>
.active-controls-grid {height: 100%; 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>
</head>
<body>
<script>
// 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");
var Datalen = 0;
var myData =[];
var myColumns = ["Ticker", "Company Name", "Market Cap.", "$ Sales", "Employees"];
// create javascript object
var obj = new Active.Controls.Grid;
var defaultResponse = table.response;
table.response = function(data){
defaultResponse.call(table, data);
Datalen=table.getCount();
for(var x=0; x< Datalen; x++) {
myData.push([table.getText( x, 0), table.getText( x, 1), table.getText( x, 2), table.getText( x, 3), table.getText( x, 4)]);
}
// load data javascript object
obj.setRowProperty("count", Datalen);
obj.setColumnProperty("count", myColumns.length);
obj.setDataProperty("text", function(i, j){return myData[i][j]});
obj.setColumnProperty("text", function(i){return myColumns[i]});
}
// start asyncronous data retrieval
table.request();
// define Timeout
setTimeout('obj.refresh()', 1000);
// write grid html to the page
document.write(obj);
</script>
</body>
</html>
//***********************
Carlos
October 26,
Carlos,
the table.request() is executed asynchronously, so you have to wait until the .response() method runs and refresh the grid.
Alex (ActiveWidgets)
October 27,
Thank you Alex , helps a lot.
(will do it in a new post) wainting for ....
Is it possible to know how many "Items" (total or per row) inside a CSV file?
Carlos
October 27,