I have an application where i have a server application written in Python that returns JSON-formatted response. This response is displayed in a ListGrid. The response includes the names of the columns, their types, and the data itself. WHen the grid gets displayed, I see that all of the columns get equal space, as a result of which some columns that dont need the space are wasting it, and others that need the space end up showing only a part of the cell. I dont specify the width of each field (see code below) since I dont know how much horizontal space I would need to display the contents
What I would like to have is that
a) the grid takes up all the space currently available,
b) the fields size up and down based on the contents - i have tried autofit horizontal, but that does not seem to expand the columns (and based on the documentation, it will not shrink the columns).
c) display a scrollbar when all the data cannot be displayed in the available space.
The code that I use along with the relevant settings is included below.
Thanks in advance.
Abhijit
What I would like to have is that
a) the grid takes up all the space currently available,
b) the fields size up and down based on the contents - i have tried autofit horizontal, but that does not seem to expand the columns (and based on the documentation, it will not shrink the columns).
c) display a scrollbar when all the data cannot be displayed in the available space.
The code that I use along with the relevant settings is included below.
Thanks in advance.
Abhijit
Code:
QueryGrid inherits from ListGrid
public QueryGrid(JSONArray colNames, JSONArray colTypes, JSONArray dataJSON, String baseUrl) {
super();
setWidth100();
setHeight100();
DataSource ds = new DataSource();
ds.setClientOnly(true);
/* Get the column names, column types and data is JSON arrays and objects */
String [] fieldNames = Utils.jsonArrayToStringArray(colNames);
String [] fieldTypes = Utils.jsonArrayToStringArray(colTypes);
/* Create the datasource fields */
for (int fi=0; fi<fieldNames.length; fi++) {
String ft = fieldTypes[fi];
DataSourceField f = new DataSourceField();
f.setName(fieldNames[fi]);
if (ft.equals("float") || ft.equals("double")) {
f.setType(FieldType.FLOAT);
} else if (ft.equals("str") || ft.equals("text")) {
f.setType(FieldType.TEXT);
} else if (ft.equals("int") || ft.equals("long")) {
f.setType(FieldType.INTEGER);
} else if (ft.equals("date")) {
f.setType(FieldType.DATE);
}
ds.addField(f);
}
/* Now, populate the data into the datasource */
for (int di=0; di<dataJSON.size(); di++) {
if (verbose) Log.debug("Adding data for row " + di);
JSONObject jObj = dataJSON.get(di).isObject();
ListGridRecord row = new ListGridRecord(jObj.getJavaScriptObject());
ds.addData(row);
if (verbose) Log.debug("Added data for row " + di);
}
setDataSource(ds);
ArrayList<ListGridField> gridFields = new ArrayList<ListGridField>();
for (int fi=0; fi<fieldNames.length; fi++) {
ListGridField gridField = new ListGridField(fieldNames[fi], fieldNames[fi]);
gridFields.add(gridField);
gridField.setCanFreeze(true);
}
setFields(gridFields.toArray(new ListGridField[gridFields.size()]));
/* Set the grid properties */
setProperties();
}
private void setProperties() {
setAutoFetchData(true);
//grid.setDataPageSize(5); SmartGWT does not support paging?
setShowAllRecords(true);
setCanResizeFields(true);
setCanReorderFields(true); // columns can be moved around by dragging headers
setAlternateRecordStyles(true); // ledger style
//setAutoFitData(Autofit.HORIZONTAL);
// grid expand horizontally to accomodate data
setShowHeaderContextMenu(true); // enable header context menu for sort, group
setCanGroupBy(true); // enable/disable grouping by a column
setShowEmptyMessage(true); // will show message if there is no data
// enable a summary for a group, for each column that can be added, this is usually the
// sum of all rows in a group. in case of a path table, this does not mean much, so
// disable
setShowGroupSummary(false);
// Enabler simple filtering
setShowFilterEditor(true);
}
Comment