Search code examples
javasqlgwtlazy-loadingsmartgwt

How to implement a Lazy List using SmartGWT and SQL


I was trying all of yesterday to try and integrate a SQL Database with SmartGWT for a lazy list but I just couldn't figure out how to implement it. (JavaDoc, and example of a lazy list)

What I want to do is create a list of a bunch of "sites" all over the world. The problem is there will probably be about a million of them, so I'm trying to load as few as possible at a time. Each site in my DB has an address, so I'm trying to sort them in the tree structure like (Country->State->City->Sites). Every time you go down a level there will be a query to the DB asking for all of the next level (Whether that be all the cities that have sites in the state chosen, or what ever).

Any help is greatly appreciated.

ALSO: In the example linked the folders and leafs are the type of element, is there a way to keep folders, folders, and then leafs a separate type of object?


Solution

  • After a while I finally got it. I ended up creating my own RPC that would serve up an array of strings that would represent the names of all the TreeNodes for the next level.

    So entry point would be:

    private NodeServiceAsync nodesRpc; //The RPC that grabs more nodes
    private Tree data; //The data structure to hold all of the nodes
    private ColumnTree list; //The GUI element that is shown on in the browser
    public void onModuleLoad() {
        nodesRpc = (NodeServiceAsync) GWT.create(NodeService.class);
        data = new Tree();
        list = new ColumnTree;
    
        list.setAutoFetchData(true);
        list.setLoadDataOnDemand(true);
    
        list.addNodeSelectedHandler(new NodeSelectedHandler () {
            public void onNodeSelected(NodeSelectedEvent event) {
                if(/*Node is folder and hasn't been opened before*/) {
                    //Get More Nodes        
                    AsyncCallback<String[]> callback = new NodeGetter<String[]>();
                    nodesRpc.getData(event.getNode(), callback);
                }
    
                else if(/*Node is not a folder (at the end) */) {
                    //Do something else
                }   
            }
        });
        list.setData(data); //Make the GUI Element Represent The Data Structure
        RootPanel.get().add(list); //Add to screen
    }
    

    The serverlet on the server side creates the query, executes, then translates the ResultSet into an array of Strings and passes that back. All the callback has to do, back on the client side, is translate that array into an array of TreeNodes and attach them to the original Node that was clicked on. Finally after all of this the GUI element is redrawn with the new nodes.

    I was surprised that there was very little down time between node loads (less then 1 sec) even when sometimes a hundred or so nodes where being queried then displayed.