Dec 07
GWT-Ext Store to SmartGWT DataSource
GWT-Ext Store instances are all over my code. And I am populating them by various means. While migrating from GWT-Ext to SmartGWT, converting Stores to DataSources will probably take up a lot of time. I was wondering if I could migrate and test only the grids first and deal with the stores later. Code below could help do just that. It creates a DataSource from a Store. You can use this data source to populate SmartGWT grids and when the grids are working fine, replace the stores completely. Isn’t exactly a life saver but nevertheless. Have tested it only on a trivial store.
It doesn’t matter but assume that the RecordDef used to create the Store looks like this:
RecordDef recordDef = new RecordDef(new FieldDef[] { new StringFieldDef("company"), new FloatFieldDef("price"), new FloatFieldDef("change"), new FloatFieldDef("pctChange"), new DateFieldDef("lastChanged", "n/j h:ia"), new StringFieldDef("symbol"), new StringFieldDef("industry") });
Create a DataSource with the same fields:
private DataSource createDataSource(Store store){ DataSource dataSource = new DataSource(); dataSource.setClientOnly(true); String[] fields = store.getFields(); for(String field:fields) { DataSourceField smartgwtField = new DataSourceField(); smartgwtField.setName(field); dataSource.addField(smartgwtField); } return dataSource; }
Add a method that copies records from the Store to the data source created above and call it in the Store’s onLoad handler
public void copyStore(Store store, DataSource dataSource) { Record[] records = store.getRecords(); String[] fields = store.getFields(); for(Record gwtextRecord:records) { ListGridRecord smartgwtRecord = new ListGridRecord(); for(String field:fields) { smartgwtRecord.setAttribute(field, gwtextRecord.getAsString(field)); } dataSource.addData(smartgwtRecord); } }
For heavy stores, this code will probably punish the browser and column data types are ignored as well but it is meant only for the dev environment until all components have been replaced eventually.
PS: If you need it, here is a small sample with GWT-Ext and SmartGWT grids being populated from the same RPC method. You will need to add gwtext.jar and smartgwt.jar and use Cypal studio plugin or add compile/run scripts.

December 8th, 2008 at 12:57 pm
how to use gwt-rpc json data for smartgwt?
thanks
December 8th, 2008 at 5:44 pm
Haven’t tried this but I assume you can use JSONParser to convert the JSON string returned by RPC to a JSON object. Then populate the DataSource as done in the sample project.
http://www.gwtapps.com/doc/html/com.google.gwt.json.client.JSONParser.html
December 25th, 2008 at 3:44 am
Is there way to clear a DataSource, to refresh content from server? I am using GWT-RPC + Hibernate on server side. Till now I didn’t find any simple way to use DataSource with Hibernate, someone did ?
December 25th, 2008 at 4:25 am
It seems that I will answer myself
It it enough to call grid.setData(new ListGridRecord[]{});
February 15th, 2009 at 6:27 pm
Is there a way to submit changes in the store to the database and how?
I am using GWT Ext and still haven’t migrated to smart GWT because of the incompatibility between those 2 libs.
My question is how to submit changes in the store or datasource to the database!
February 16th, 2009 at 1:56 pm
Store..getModifiedRecords() combined with RPC?
I am not sure if Store provides any direct way to integrate with the backend such that modified data is automatically saved.
February 16th, 2009 at 5:06 pm
Maybe there isn’t!I dealt with the problem by getting all records using store.getRecords() and by manually persisting and updating them!
February 16th, 2009 at 5:08 pm
I have another problem:
I want to load store from xml to a combobox and use it as suggestion
Could You give one very simple example how to do that?
My Code is:
HttpProxy dataProxy = new HttpProxy(”data/alerts.xml”);
final String resultTpl = “{SerialKey}”;
RecordDef alertsRecordDef =
new RecordDef(new FieldDef[] { new IntegerFieldDef(”SerialKey”,”SerialKey”)});
XmlReader xmlreader = new XmlReader(”Alert”,alertsRecordDef);
xmlreader.setRecord(”Alert”);
Store store = new Store(dataProxy, xmlreader);
store.load();
Record[] records = store.getRecords();
int recordNum = records.length;
MessageBox.alert(recordNum+”");
ComboBox cb = new ComboBox();
cb.setStore(store);
cb.setDisplayField(”SerialKey”);
cb.setId(”SerialKey”);
cb.setTypeAhead(false);
cb.setLoadingText(”Searching…”);
cb.setFieldLabel(”SerialKey”);
cb.setWidth(130);
cb.setTpl(resultTpl);
cb.setPageSize(10);
cb.setHideTrigger(false);
cb.setMode(ComboBox.REMOTE);
cb.setLazyRender(true);
cb.setTitle(”Alerts”);
//cb.setHideLabel(true);
cb.setItemSelector(”div.search-item”);
RootPanel.get().add(cb);
this code gives me 0 records!
Please help!
February 17th, 2009 at 7:38 pm
Hi xaxera, I wont be able to test this code. Try posting your query in the gwt-ext forums at http://gwt-ext.com/forum. I am positive someone there must have an explanation.
Regards,
Abhijeet.