如何使用SmartGWT和SQL实现惰性列表

时间:2023-01-17 22:19:47

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)

我昨天尝试将一个SQL数据库与SmartGWT集成在一个懒惰的列表中,但我无法弄清楚如何实现它。 (JavaDoc和懒惰列表的示例)

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).

我想要做的是创建一个遍布全球的一堆“网站”的列表。问题是它们可能会有大约一百万个,所以我一次尝试加载的数量尽可能少。我的数据库中的每个站点都有一个地址,所以我试图在树状结构中对它们进行排序,如(Country-> State-> City-> Sites)。每次你进入一个级别时,都会有一个查询到数据库,询问所有下一级别(是否所有城市都选择了状态,或者是什么状态)。

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?

另外:在链接的示例中,文件夹和叶子是元素的类型,有没有办法保存文件夹,文件夹,然后叶子单独类型的对象?

2 个解决方案

#1


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.

过了一会儿我终于明白了。我最终创建了自己的RPC,它将提供一个字符串数组,代表下一级所有TreeNode的名称。

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.

服务器端的serverlet创建查询,执行,然后将ResultSet转换为字符串数组并将其传回。回到客户端,所有回调都要将该数组转换为TreeNode数组,并将它们附加到单击的原始节点上。最后,在所有这些之后,使用新节点重新绘制GUI元素。

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.

令我感到惊讶的是节点加载(少于1秒)之间的停机时间非常短,即使有时会显示大约一百个被查询的节点。

#2


Note there is also a Pro version of the product which includes SQL connectivity like this out of the box (for Java server platforms). Showcase here:

请注意,还有一个Pro版本的产品,其中包括开箱即用的SQL连接(适用于Java服务器平台)。在这里展示:

http://www.smartclient.com/smartgwtee/showcase/

The SQL connector in the Pro product includes load on demand / data paging, search, and all 4 CRUD operations, as well as DataSource Wizards that can generate a working SQL DataSource for an existing database table if you just enter JDBC settings.

Pro产品中的SQL连接器包括按需加载/数据分页,搜索和所有4个CRUD操作,以及可以为现有数据库表生成工作SQL数据源的DataSource向导(如果只输入JDBC设置)。

Note the Pro product doesn't require SQL, that's just one of the things it can connect to.

请注意,Pro产品不需要SQL,这只是它可以连接的东西之一。

#1


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.

过了一会儿我终于明白了。我最终创建了自己的RPC,它将提供一个字符串数组,代表下一级所有TreeNode的名称。

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.

服务器端的serverlet创建查询,执行,然后将ResultSet转换为字符串数组并将其传回。回到客户端,所有回调都要将该数组转换为TreeNode数组,并将它们附加到单击的原始节点上。最后,在所有这些之后,使用新节点重新绘制GUI元素。

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.

令我感到惊讶的是节点加载(少于1秒)之间的停机时间非常短,即使有时会显示大约一百个被查询的节点。

#2


Note there is also a Pro version of the product which includes SQL connectivity like this out of the box (for Java server platforms). Showcase here:

请注意,还有一个Pro版本的产品,其中包括开箱即用的SQL连接(适用于Java服务器平台)。在这里展示:

http://www.smartclient.com/smartgwtee/showcase/

The SQL connector in the Pro product includes load on demand / data paging, search, and all 4 CRUD operations, as well as DataSource Wizards that can generate a working SQL DataSource for an existing database table if you just enter JDBC settings.

Pro产品中的SQL连接器包括按需加载/数据分页,搜索和所有4个CRUD操作,以及可以为现有数据库表生成工作SQL数据源的DataSource向导(如果只输入JDBC设置)。

Note the Pro product doesn't require SQL, that's just one of the things it can connect to.

请注意,Pro产品不需要SQL,这只是它可以连接的东西之一。