3.2.0

Help getting new row data for csv write.

Hi, I'm a newbie to AW, so please forgive me if this is dumb question.

After adding a row (in what seems to be a standard method taken from add/delete row example), I'm trying to write the data back to the CSV file I loaded the table from. When I do, the newly edited data is not there, and my save function writes empty fields ("") into csv. If I reload the page, then edited data in the new row is written fine.

Is there something I'm missing when it comes to refreshing the grid after a row add/delete -- perhaps something as simple as the order things occur?

I won't bother you with the process_grid.php script (which just opens the output file and write $content into it), but in my main script I'm using these functions (this happens with or without obj.refresh(); in the add() function):

Thanks!

function savegrid() {
var content=processgrid();
var filecsv="sites.csv";
save.setURL("process_grid.php");
save.setRequestMethod("POST");
save.setParameter("filename", filecsv);
save.setParameter("content", content);
save.request();
var doc="NORESPONSE";
save.response = function(doc) {
if (doc == "SUCCESS") {
document.getElementById("savebutton").disabled=true;
alert("Done \n" + filecsv + "\nnot sure of success");
}
}
}

function processgrid() {
var testIndices = obj.getRowIndices();
if (testIndices.length == 0) {
var rows = [];
for (i=0; i < obj.getRowCount(); i++) {
rows.push(i)
}
obj.setRowIndices(rows);
}

var columns = obj.getColumnCount();
var rows = obj.getRowCount();
// be sure that content is empty
content='';

for(var row=0; row<rows; row++){
var currrow='';
var currcel='';
for(var col=0; col<columns; col++){
currcel=obj.getCellText(col, row);
// fills empty cells
if (currcel=="'" || currcel=="")
currcel='""';
else
currcel='"' + currcel + '"';

// build the CSV data format
if (currrow=='')
currrow=currcel;
else
currrow=currrow + ',' + currcel;
}
content = content + currrow + '\n';
}
return content;
}

function add(){
obj.addRow(serial++);
}

function del(){
var i = obj.getCurrentRow();
obj.deleteRow(i);
}

Justin (Boston)
April 13,
Sorry, I wasn't very clear.
When I add a row, it gets written (upon save) to the csv filke, but the new line has all empty strings ("","","",...). So, it seems like the data written by

obj.onRowAdded = function(row){
window.status = "Row added: " + row;
this.setCellText("new", 0, row);
}

as well as the immediate edits on that new row, are not in whatever array is being written out by the POST. Again, if I refresh the web page in the browser, which performs a new table.setURL() and table.request(), then subsequent row edits stick.

Any suggestion on adding a refresh, new AW.UI.Grid, document.write(obj); or rearanging things?
Justin (Boston)
April 13,

Nobody?

Well, if I've crossed the etiquette line somewhere then sorry. I too am evaluating the collection. BTW, what comes with the package? Is it similar to the eval (a collection of libraries and example files), or is it a toolkit environment? I couldn't find a description anywhere.

Also, would I be able to store vaules in a CSV encrypted?

Thanks!
Justin (Boston)
April 17,
In the current version the CSV table is read-only. You have to add setData() method to the table (which writes modifications back to CSV) and add onCellValidated event handler which actually calls setData().

table.setData = function(value, col, row){
    this._data[row][col] = value;
} 


// after validation update 'data' and remove 'text' properties
obj.onCellValidated = function(text, col, row){
    var format = this.getCellFormat(col, row);
    var data = format ? format.textToData(text): text;
    this.setCellData(data, col, row);
    this.setCellText(undefined, col, row); // remove edited text
}


As for your second question - the content of the licensed version is the same as the trial + source code.
Alex (ActiveWidgets)
April 17,
Do you mean that table.setURL() is read-only? Is that why I had to use TMTisFree's external php script?

I'm sorry, I have no idea where to put two blocks you just gave me.

I'm experimenting with
table.setURL("blah.csv");
table.request();

So now I have a table? Then var obj = new AW.UI.Grid; and the six obj's build a grid, then document.write(obj); writes it to the browser page, right?

So, upon obj.onRowAdded = function(row) and this.setCellText("new", 0, row) I get a new row in the table? Do I need to rewrite the new table into the grid before I call the external script to rewrite the csv?

I tried adding your table.setData block to obj.onRowAdded = function(row){...}, and defining obj.onCellValidated bloack as obj.onRow* are, but I get the same behavior as before, which is that new data is lost only after a obj.addRow()
Justin (Boston)
April 17,
You have to add setData method to CSV datasource -

table.setURL("blah.csv");
table.setData = function(value, col, row){...}
table.request();


When the user edits the grid - the changes are reflected in cellText property, you have to decide when you move cellText into cellData. I would suggest using onCellValidated event.

// after validation update 'data' and remove 'text' properties
obj.onCellValidated = function(text, col, row){
    var format = this.getCellFormat(col, row);
    var data = format ? format.textToData(text): text;
    this.setCellData(data, col, row);
    this.setCellText(undefined, col, row); // remove edited text
}


If you want to initialize the new row - call setCellData() method, not setCellText() -

obj.onRowAdded = function(row){
    this.setCellData("new", 0, row); 
}
Alex (ActiveWidgets)
April 17,
Ok, but I don't understand why, since loading and changing cells works fine [except immediately after an addRow()] I need to change the initial table/grid build process. You suggest adding "table.setData = function(value, col, row){...}" between table.setURL and table.request.

maybe the new rows are being saved becasue the row indices aren't proper -- the serial=1000 means that new rows start at 1000. I tried using obj.addRow(rows); instead of serial++ but that doesn't work.

And...

obj.onRowAdded = function(row){
    this.setCellData("new", 0, row); 
}

Here, using this.setCellData in place of this.setCellText prevents the new row from showing on the browser. Like before, running the savegrid function and refreshing the page (rebuilding the grid) shows that the rows are added, but at least before I had an idication that the new rows were present and I could insert text into them (but writing to the cvs produced blank fields).

Oh well, thanks for the suggestions anyway Alex! I think maybe I'm just not knowledgeable enough to make good use of this stuff :/
Justin (Boston)
April 17,

This topic is archived.

See also:


Back to support forum