Table model for JSON data
I'm to lazy to deal with XML and CSV is not an option. Therefor a created a Table class to deal with JSON data I receive from the server. It is even simpler than the CSV version- no parsing - just call eval(). I havn't tested putting a format on columns though. Here you go:
Use or include in 2.0 final at your own risk ...
Use or include in 2.0 final at your own risk ...
/****************************************************************
*****************************************************************/
if (!AW.JSON) {AW.JSON = {}}
AW.JSON.Table = AW.HTTP.Request.subclass();
AW.JSON.Table.create = function(){
/****************************************************************
Table model for loading and parsing data in JSON text format.
*****************************************************************/
var obj = this.prototype;
var _super = this.superclass.prototype;
/****************************************************************
Allows to process the received text.
@param text (String) The downloaded text.
*****************************************************************/
obj.response = function(text){
this._data = eval('(' + text + ')');
if (this.$owner) {
this.$owner.clearSelectionModel();
this.$owner.clearSortModel();
this.$owner.clearRowModel();
this.$owner.setRowCount(this.getCount());
this.$owner.refresh();
}
// _super.response.call(this);
};
obj._data = [];
obj._formats = [];
/****************************************************************
Allows to specify the formatting object for the column.
@param format (Object) The formatting object.
@param index (Index) The column index.
*****************************************************************/
obj.setFormat = function(format, index){
this._formats = this._formats.concat();
this._formats[index] = format;
};
/****************************************************************
Allows to specify the formatting objects for each column.
@param formats (Array) The array of formatting objects.
*****************************************************************/
obj.setFormats = function(formats){
this._formats = formats;
};
/****************************************************************
Returns the number of data rows.
*****************************************************************/
obj.getCount = function(){
return this._data.length;
};
/****************************************************************
Returns the index.
*****************************************************************/
obj.getIndex = function(i){
return i;
};
/****************************************************************
Returns the cell text.
@param r (Index) Row index.
@param c (Index) Column index.
*****************************************************************/
obj.getData = function(c, r){
return this._data[r][c];
};
/****************************************************************
Returns the cell text.
@param i (Index) Row index.
@param j (Index) Column index.
*****************************************************************/
obj.getText = function(i, j){
var data = this.getData(i, j);
var format = this._formats[i];
return format ? format.dataToText(data) : data;
};
/****************************************************************
Returns the cell image.
@param i (Index) Row index.
@param j (Index) Column index.
*****************************************************************/
obj.getImage = function(){
return "";
};
/****************************************************************
Returns the cell hyperlink.
@param i (Index) Row index.
@param j (Index) Column index.
*****************************************************************/
obj.getLink = function(){
return "";
};
/****************************************************************
Returns the cell value.
@param i (Index) Row index.
@param j (Index) Column index.
*****************************************************************/
obj.getValue = function(i, j){
var data = this.getData(i, j) || "";
var format = this._formats[i];
return format ? format.dataToValue(data) : data;
};
};
Christian M.
November 12,