PDA

View Full Version : How to handle null values in Grid?



davida
20 Dec 2006, 9:41 AM
I am using an editable grid with JSON data that returns quite a few null values for fields (floating point numbers). When I cursor over a blank cell, its triggering the cellUpdated event because the NumberEditor is returning '' rather than null so it looks like the cell value has changed (even though the user didn't type anything in it).

Is there a way to allow the cells/editors to handle null values directly? I don't really want to translate nulls to empty strings when loading the JSON data as null is valid in my world (database value) and I'd like people to be able to clear out a field and then do a post to the server to set it to null. It would be easier if the new value for a cell after clearing it is null rather than '' since an empty string really doesn't make sense for a float.

Thanks in advance.

jack.slocum
20 Dec 2006, 2:08 PM
This will work, but you could probably do it more generically if you wanted to.


grid.setCellValue = function(value, rowIndex, colIndex){
if(colIndex == 4 && value === ''){ // fourth column is float
value = null;
}
YAHOO.ext.grid.Grid.prototype.setCellValue.call(this, value, rowIndex, colIndex);
}

davida
21 Dec 2006, 11:58 AM
I didn't get that to work but I handled it a different way: I added a pre-processor to map null floats to an empty string when loading the data and then I do the reverse mapping when a cell is edited. Then I post the data back to the server in JSON form to avoid the ambiguity.