问题描述
我在使用smartGWT的ListGrid中绑定数据源时遇到了问题。我有GWT-RPC-DataSource,并且我已经将它设置为我的数据源
grid.setDataSource(ds);
在一个按钮上点击,我的数据源发生了一些变化,我正在生成新的数据源并用smartgwt重新绑定格。但它失败了。我已经尝试使用 grid.redraw()函数来重绘网格。
下面是我的类GWTRPCDATASOURCE
public abstract class GwtRpcDataSource extends DataSource {
$ b $ / **
*创建新的数据源,通过GWT RPC与服务器通信。它是
*正常服务器端的SmartClient数据源,其数据协议设置为
*< code> DSProtocol.CLIENTCUSTOM< / code> (clientCustom - 原生支持SmartClient的
*,但应添加到smartGWT)以及数据格式
*< code> DSDataFormat.CUSTOM< / code> ;.
* /
public GwtRpcDataSource(){
setDataProtocol(DSProtocol.CLIENTCUSTOM);
setDataFormat(DSDataFormat.CUSTOM);
setClientOnly(false);
}
/ **
*执行请求到服务器。
*
* @param请求
*< code> DSRequest< / code>正在处理。
* @return< code>对象< / code>来自原始请求的数据。
* /
@Override
保护对象transformRequest(DSRequest请求){
字符串requestId = request.getRequestId();
DSResponse response = new DSResponse();
response.setAttribute(clientContext,
request.getAttributeAsObject(clientContext));
// Asume成功
response.setStatus(0);
switch(request.getOperationType()){
case FETCH:
executeFetch(requestId,request,response);
休息;
case ADD:
executeAdd(requestId,request,response);
休息;
case UPDATE:
executeUpdate(requestId,request,response);
休息;
case REMOVE:
executeRemove(requestId,request,response);
休息;
默认值:
//未执行操作。
休息;
}
返回request.getData();
}
/ **
*执行于< code> FETCH< / code>操作。
*< code> processResponse(requestId,response)< / code>应在
*操作完成时调用(成功或失败)。
*
* @param requestId
*< code> String< / code>从
*< code> DSRequest.getRequestId()< / code>中提取。
* @param请求
*< code> DSRequest< / code>正在处理。
* @param response
*< code> DSResponse< / code> ;. < code> setData(list)< / code>应该是
*,呼吁成功执行此方法。
*< code> setStatus(& lt; 0)< / code>应该在失败时被调用。
* /
protected void executeFetch(String requestId,DSRequest request,
DSResponse response);
/ **
*执行于< code> ADD< / code>操作。
*< code> processResponse(requestId,response)< / code>应在
*操作完成时调用(成功或失败)。
*
* @param requestId
*< code> String< / code>从
*< code> DSRequest.getRequestId()< / code>中提取。
* @param请求
*< code> DSRequest< / code>正在处理。
*< code> request.getData()< / code>包含的记录应该是
*添加的。
* @param response
*< code> DSResponse< / code> ;. < code> setData(list)< / code>应该是
*,呼吁成功执行此方法。数组应该
*包含表示添加行的单个元素。
*< code> setStatus(& lt; 0)< / code>应该在失败时被调用。
* /
protected abstract void executeAdd(String requestId,DSRequest request,
DSResponse response);
/ **
*执行于< code> UPDATE< / code>操作。
*< code> processResponse(requestId,response)< / code>应在
*操作完成时调用(成功或失败)。
*
* @param requestId
*< code> String< / code>从
*< code> DSRequest.getRequestId()< / code>中提取。
* @param请求
*< code> DSRequest< / code>正在处理。
*< code> request.getData()< / code>包含的记录应该是
*更新。
* @param response
*< code> DSResponse< / code> ;. < code> setData(list)< / code>应该是
*,呼吁成功执行此方法。数组应该
*包含表示更新行的单个元素。
*< code> setStatus(& lt; 0)< / code>应该在失败时被调用。
* /
protected abstract void executeUpdate(String requestId,DSRequest request,
DSResponse response);
/ **
*执行于< code> REMOVE< / code>操作。
*< code> processResponse(requestId,response)< / code>应在
*操作完成时调用(成功或失败)。
*
* @param requestId
*< code> String< / code>从
*< code> DSRequest.getRequestId()< / code>中提取。
* @param请求
*< code> DSRequest< / code>正在处理。
*< code> request.getData()< / code>包含的记录应该是
*删除。
* @param response
*< code> DSResponse< / code> ;. < code> setData(list)< / code>应该是
*,呼吁成功执行此方法。数组应该
*包含表示移除行的单个元素。
*< code> setStatus(& lt; 0)< / code>应该在失败时被调用。
* /
protected abstract void executeRemove(String requestId,DSRequest request,
DSResponse response);
private ListGridRecord getEditedRecord(DSRequest request){
//在编辑
之前检索值JavaScriptObject oldValues =请求
.getAttributeAsJavaScriptObject(oldValues);
//创建用于将旧值与更改合并的新记录
ListGridRecord newRecord = new ListGridRecord();
//从旧记录复制属性
JSOHelper.apply(oldValues,newRecord.getJsObj());
//检索已更改的值
JavaScriptObject data = request.getData();
//应用更改
JSOHelper.apply(data,newRecord.getJsObj());
返回newRecord;
}
}
我已经实现了这个抽象类来我自己的数据源类名为NTDatasource。
public class NTDataSource extends GwtRpcDataSource {
public static int total = 991;
记录[]记录;
public NTDataSource(){
}
public void setData(List< NTListGridField> lstFields,Record [] records){
// setTestData(records);
for(NTListGridField lstField:lstFields){
if(lstField.getType()== ListGridFieldType.DATE){
DataSourceDateField dateField = new DataSourceDateField(
lstField.getName()) ;
dateField.setHidden(lstField.getAttributeAsBoolean(visible));
if(lstField.getName()。equals(id)){
dateField.setHidden(true);
}
addField(dateField);
} else {
DataSourceTextField textField = new DataSourceTextField(
lstField.getName());
textField.setHidden(lstField.getAttributeAsBoolean(visible));
if(lstField.getName()。equals(id)){
textField.setHidden(true);
textField.setPrimaryKey(true);
}
addField(textField);
}
}
total = records.length;
this.records = records;
}
$ b $ @覆盖
protected void executeFetch(String requestId,DSRequest request,
DSResponse response){
//假设我们有1000个项目。
response.setTotalRows(total);
int end = request.getEndRow();
if(end> total){
end = total;
}
记录returnRecords [] =新记录[end
- request.getStartRow()];
for(int i = request.getStartRow(); i< end; i ++){
ListGridRecord r = new ListGridRecord();
r =(ListGridRecord)records [i];
returnRecords [i - request.getStartRow()] = r;
}
GWT.log(call from+ request.getStartRow()+to
+ request.getEndRow()+result+ returnRecords.length,null);
response.setData(returnRecords);
processResponse(requestId,response);
}
@Override
protected void executeAdd(String requestId,DSRequest request,
DSResponse response){
// TODO自动生成方法存根
$ b @Override
protected void executeUpdate(String requestId,DSRequest request,
DSResponse response){
// TODO自动生成方法存根
$ b @覆盖
保护无效executeRemove(字符串requestId,DSRequest请求,
DSResponse响应){
// TODO自动生成的方法存根
$ b code $ $ $ $ $ $ $ $ $ 解决方案解决我自己解决了这个问题。
$ b 答案是我需要使用grid.fetchData()方法并将数据源绑定一次用它.... !!我希望它可以帮助别人。
I have a problem with datasource binding in ListGrid with smartGWT. I have GWT-RPC-DataSource and i have set it as my datasource
grid.setDataSource(ds);
On one button click I have some changes in my datasource and I am generating new datasource and rebinding with smartgwt's grid. but it fails. I have tried grid.redraw() function to redraw the grid.
Below is my class for GWTRPCDATASOURCE
public abstract class GwtRpcDataSource extends DataSource {
/**
* Creates new data source which communicates with server by GWT RPC. It is
* normal server side SmartClient data source with data protocol set to
* <code>DSProtocol.CLIENTCUSTOM</code> ("clientCustom" - natively supported
* by SmartClient but should be added to smartGWT) and with data format
* <code>DSDataFormat.CUSTOM</code>.
*/
public GwtRpcDataSource() {
setDataProtocol(DSProtocol.CLIENTCUSTOM);
setDataFormat(DSDataFormat.CUSTOM);
setClientOnly(false);
}
/**
* Executes request to server.
*
* @param request
* <code>DSRequest</code> being processed.
* @return <code>Object</code> data from original request.
*/
@Override
protected Object transformRequest(DSRequest request) {
String requestId = request.getRequestId();
DSResponse response = new DSResponse();
response.setAttribute("clientContext",
request.getAttributeAsObject("clientContext"));
// Asume success
response.setStatus(0);
switch (request.getOperationType()) {
case FETCH:
executeFetch(requestId, request, response);
break;
case ADD:
executeAdd(requestId, request, response);
break;
case UPDATE:
executeUpdate(requestId, request, response);
break;
case REMOVE:
executeRemove(requestId, request, response);
break;
default:
// Operation not implemented.
break;
}
return request.getData();
}
/**
* Executed on <code>FETCH</code> operation.
* <code>processResponse (requestId, response)</code> should be called when
* operation completes (either successful or failure).
*
* @param requestId
* <code>String</code> extracted from
* <code>DSRequest.getRequestId ()</code>.
* @param request
* <code>DSRequest</code> being processed.
* @param response
* <code>DSResponse</code>. <code>setData (list)</code> should be
* called on successful execution of this method.
* <code>setStatus (<0)</code> should be called on failure.
*/
protected abstract void executeFetch(String requestId, DSRequest request,
DSResponse response);
/**
* Executed on <code>ADD</code> operation.
* <code>processResponse (requestId, response)</code> should be called when
* operation completes (either successful or failure).
*
* @param requestId
* <code>String</code> extracted from
* <code>DSRequest.getRequestId ()</code>.
* @param request
* <code>DSRequest</code> being processed.
* <code>request.getData ()</code> contains record should be
* added.
* @param response
* <code>DSResponse</code>. <code>setData (list)</code> should be
* called on successful execution of this method. Array should
* contain single element representing added row.
* <code>setStatus (<0)</code> should be called on failure.
*/
protected abstract void executeAdd(String requestId, DSRequest request,
DSResponse response);
/**
* Executed on <code>UPDATE</code> operation.
* <code>processResponse (requestId, response)</code> should be called when
* operation completes (either successful or failure).
*
* @param requestId
* <code>String</code> extracted from
* <code>DSRequest.getRequestId ()</code>.
* @param request
* <code>DSRequest</code> being processed.
* <code>request.getData ()</code> contains record should be
* updated.
* @param response
* <code>DSResponse</code>. <code>setData (list)</code> should be
* called on successful execution of this method. Array should
* contain single element representing updated row.
* <code>setStatus (<0)</code> should be called on failure.
*/
protected abstract void executeUpdate(String requestId, DSRequest request,
DSResponse response);
/**
* Executed on <code>REMOVE</code> operation.
* <code>processResponse (requestId, response)</code> should be called when
* operation completes (either successful or failure).
*
* @param requestId
* <code>String</code> extracted from
* <code>DSRequest.getRequestId ()</code>.
* @param request
* <code>DSRequest</code> being processed.
* <code>request.getData ()</code> contains record should be
* removed.
* @param response
* <code>DSResponse</code>. <code>setData (list)</code> should be
* called on successful execution of this method. Array should
* contain single element representing removed row.
* <code>setStatus (<0)</code> should be called on failure.
*/
protected abstract void executeRemove(String requestId, DSRequest request,
DSResponse response);
private ListGridRecord getEditedRecord(DSRequest request) {
// Retrieving values before edit
JavaScriptObject oldValues = request
.getAttributeAsJavaScriptObject("oldValues");
// Creating new record for combining old values with changes
ListGridRecord newRecord = new ListGridRecord();
// Copying properties from old record
JSOHelper.apply(oldValues, newRecord.getJsObj());
// Retrieving changed values
JavaScriptObject data = request.getData();
// Apply changes
JSOHelper.apply(data, newRecord.getJsObj());
return newRecord;
}
}
I have implemented this abstract class to my own datasource class named NTDatasource.
public class NTDataSource extends GwtRpcDataSource {
public static int total = 991;
Record[] records;
public NTDataSource() {
}
public void setData(List<NTListGridField> lstFields, Record[] records) {
// setTestData(records);
for (NTListGridField lstField : lstFields) {
if (lstField.getType() == ListGridFieldType.DATE) {
DataSourceDateField dateField = new DataSourceDateField(
lstField.getName());
dateField.setHidden(lstField.getAttributeAsBoolean("visible"));
if (lstField.getName().equals("id")) {
dateField.setHidden(true);
}
addField(dateField);
} else {
DataSourceTextField textField = new DataSourceTextField(
lstField.getName());
textField.setHidden(lstField.getAttributeAsBoolean("visible"));
if (lstField.getName().equals("id")) {
textField.setHidden(true);
textField.setPrimaryKey(true);
}
addField(textField);
}
}
total = records.length;
this.records = records;
}
@Override
protected void executeFetch(String requestId, DSRequest request,
DSResponse response) {
// assume we have 1000 items.
response.setTotalRows(total);
int end = request.getEndRow();
if (end > total) {
end = total;
}
Record returnRecords[] = new Record[end
- request.getStartRow()];
for (int i = request.getStartRow(); i < end; i++) {
ListGridRecord r = new ListGridRecord();
r = (ListGridRecord) records[i];
returnRecords[i - request.getStartRow()] = r;
}
GWT.log(" called from " + request.getStartRow() + " to "
+ request.getEndRow() + " result " + returnRecords.length, null);
response.setData(returnRecords);
processResponse(requestId, response);
}
@Override
protected void executeAdd(String requestId, DSRequest request,
DSResponse response) {
// TODO Auto-generated method stub
}
@Override
protected void executeUpdate(String requestId, DSRequest request,
DSResponse response) {
// TODO Auto-generated method stub
}
@Override
protected void executeRemove(String requestId, DSRequest request,
DSResponse response) {
// TODO Auto-generated method stub
}
}
解决方案 I have solve this question myself.
the answer is i need to use grid.fetchData() method and bind datasource one more time to use it.... !! I hope it might help someone else.
这篇关于SMARTGWT数据源(GWT-RPC-DATASource)LISTGRID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
07-31 08:43