PDA

View Full Version : How can I use HttpProxy to Paing grid with gxt 3.0



29 Mar 2012, 7:14 AM
How can I use HttpProxy to Paing grid, any idea?

Colin Alworth
29 Mar 2012, 8:25 AM
Start with the ideas in the paging grid example (which uses a grid, paging toolbar, and binds them to a ListStore and PagingLoader), and then wire the loader into an HttpProxy. The advanced combobox example shows how to use ScriptTagProxy, which has an API very similar to HttpProxy, but loads its data in a different way.


http://sencha.com/examples-dev/#ExamplePlace:paginggrid
http://sencha.com/examples-dev/#ExamplePlace:advancedcombobox


(http://sencha.com/examples-dev/#ExamplePlace:paginggrid)The basic idea when using these tools is that the Store and Loader should be used by your own application code and widgets, and the Loader should itself know about the DataReader and DataProxy, to see how it will load the data. At each level, these pieces should be interchangable - a Loader can work with any DataProxy (and possibly a DataReader) that can pull in the right kind of data for it, and then the PagingToolbar just needs to talk to the right kind of Loader.

poseidonjm
4 Dec 2012, 3:26 PM
I extend HttpProxy and I added two helper methods like requestfactory

public interface MyAutoBeanFactory extends AutoBeanFactory {

AutoBean<FilterPagingLoadConfig> loadConfig();

AutoBean<SortInfo> getSortInfo();

AutoBean<FilterConfig> getFilterConfig();
}
The class

public class MyHttpProxy<C> extends HttpProxy<C> {

public MyHttpProxy(RequestBuilder builder) {
super(builder);
}

protected List<SortInfo> createRequestSortInfo(MyAutoBeanFactory factory, List<? extends SortInfo> original) {
List<SortInfo> sortInfo = new ArrayList<SortInfo>();

for (int i = 0; i < original.size(); i++) {
SortInfo originalSortInfo = original.get(i);
SortInfo reqSortInfo = factory.getSortInfo().as();
reqSortInfo.setSortDir(originalSortInfo.getSortDir());
reqSortInfo.setSortField(originalSortInfo.getSortField());
sortInfo.add(reqSortInfo);
}

return sortInfo;
}


protected List<FilterConfig> createRequestFilterConfig(MyAutoBeanFactory factory, List<? extends FilterConfig> original) {
List<FilterConfig> sortInfo = new ArrayList<FilterConfig>();

for (int i = 0; i < original.size(); i++) {
FilterConfig originalSortInfo = original.get(i);
FilterConfig reqSortInfo = factory.getFilterConfig().as();

reqSortInfo.setComparison(originalSortInfo.getComparison());
reqSortInfo.setField(originalSortInfo.getField());
reqSortInfo.setType(originalSortInfo.getType());
reqSortInfo.setValue(originalSortInfo.getValue());

sortInfo.add(reqSortInfo);
}

return sortInfo;
}


}

Example


public interface UserProxy { String getFirst();
void setFirst(String first);
String getLast();
void setLast(String last);
String getLogin();
void setLogin(String login);
Long getId();
void setId(Long id);
}


public interface UserListProxy {
List<UserProxy> getData();
int getTotalCount();
}


public class UserJsonReader extends JsonReader<PagingLoadResult<UserProxy>, UserListProxy> {
public UserJsonReader(AutoBeanFactory factory,
Class<UserListProxy> rootBeanType) {
super(factory, rootBeanType);
}

@Override
protected PagingLoadResult<UserProxy> createReturnData(Object loadConfig, UserListProxy incomingData) {
FilterPagingLoadConfig pagingLoadConfig = (FilterPagingLoadConfig)loadConfig;

return new PagingLoadResultBean<UserProxy>(incomingData.getData(), incomingData.getTotalCount(), pagingLoadConfig.getOffset());
}


}


public interface AppAutoBeanFactory extends MyAutoBeanFactory {
AutoBean<UserListProxy> users();
}


final AppAutoBeanFactory factory = GWT.create(AppAutoBeanFactory.class);
UserJsonReader reader = new UserJsonReader(factory, UserListProxy.class);

String path = "users/users.php";
RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, path);
MyHttpProxy<FilterPagingLoadConfig> proxy = new MyHttpProxy<FilterPagingLoadConfig>(builder){
@Override
public void load(FilterPagingLoadConfig loadConfig,
Callback<String, Throwable> callback) {

List<SortInfo> sortInfo = createRequestSortInfo(factory, loadConfig.getSortInfo());

List<FilterConfig> filterConfig = createRequestFilterConfig(factory, loadConfig.getFilters());

loadConfig.setSortInfo(sortInfo);
loadConfig.setFilters(filterConfig);

super.load(loadConfig, callback);
}
};
//proxy.setWriter(new UrlEncodingWriter<FilterPagingLoadConfig>(factory, FilterPagingLoadConfig.class));
proxy.setWriter(new JsonWriter<FilterPagingLoadConfig>(factory, FilterPagingLoadConfig.class));

loader = new PagingLoader<FilterPagingLoadConfig, PagingLoadResult<UserProxy>>(proxy, reader){
@Override
protected FilterPagingLoadConfig newLoadConfig() {
return new FilterPagingLoadConfigBean();
}
};

loader.setRemoteSort(true);
loader.useLoadConfig(factory.loadConfig().as());

UserProxyProperties props = GWT.create(UserProxyProperties.class);

store = new ListStore<UserProxy>(props.id());
loader.addLoadHandler(new LoadResultListStoreBinding<FilterPagingLoadConfig, UserProxy, PagingLoadResult<UserProxy>>(store));


I pasted only the relevant code.
The helper methods of MyHttpProxy is used because AutoBeans cannot send List
http://stackoverflow.com/questions/11530451/how-to-serialize-a-list-in-autobean-gwt