You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Domino-ui DataTable的SearchTableAction与服务端搜索对接?

对接SearchTableAction表头与服务端搜索的最优方案

你当前复制UI配置再手动setData的方案确实会带来代码冗余、维护成本高的问题,下面是更合理的实现思路,不用重复造UI轮子:

核心思路

扩展原有的SearchTableAction,替换掉客户端过滤的LocalListDataStore,改用异步数据获取逻辑,把表头的搜索参数传递给服务端,再将返回结果渲染到表格中。

1. 自定义异步数据处理类

不用直接依赖LocalListDataStore,自己实现一个适配服务端请求的数据处理类,负责封装搜索、分页、排序参数,调用服务端接口并更新表格数据:

public class AsyncSearchDataStore<T> {
    private final SearchTableAction<T> tableAction;
    private final ServiceAsync dataService; // 你的服务端异步接口

    public AsyncSearchDataStore(SearchTableAction<T> tableAction, ServiceAsync dataService) {
        this.tableAction = tableAction;
        this.dataService = dataService;
    }

    // 根据表头搜索条件发起服务端请求
    public void fetchData(SearchFilter filter) {
        // 把SearchFilter转换成服务端需要的参数格式
        ServerSearchParams params = convertFilterToParams(filter);
        // 调用服务端异步接口
        dataService.searchData(params, new AsyncCallback<List<T>>() {
            @Override
            public void onSuccess(List<T> result) {
                // 更新表格数据,复用原SearchTableAction的渲染逻辑
                tableAction.getStore().setData(result);
                tableAction.refresh();
            }

            @Override
            public void onFailure(Throwable caught) {
                // 处理请求失败逻辑,比如弹出提示
                Window.alert("搜索失败:" + caught.getMessage());
            }
        });
    }

    // 转换客户端SearchFilter为服务端能识别的参数
    private ServerSearchParams convertFilterToParams(SearchFilter filter) {
        ServerSearchParams params = new ServerSearchParams();
        // 遍历过滤条件,设置到服务端参数中
        for (FilterCriterion criterion : filter.getCriteria()) {
            params.addCondition(criterion.getField(), criterion.getOperator(), criterion.getValue());
        }
        // 如果有分页、排序需求,也在这里添加对应参数
        params.setPage(tableAction.getPager().getCurrentPage());
        params.setPageSize(tableAction.getPager().getPageSize());
        return params;
    }
}

2. 扩展SearchTableAction,绑定异步搜索逻辑

创建一个继承自SearchTableAction的子类,在构造方法中初始化UI(复用父类的UI配置,不用复制),然后替换搜索触发逻辑:

public class AsyncSearchTableAction<T> extends SearchTableAction<T> {
    private final AsyncSearchDataStore<T> dataStore;

    public AsyncSearchTableAction(Class<T> beanClass, ServiceAsync dataService) {
        super(beanClass); // 复用父类的UI初始化逻辑,不用自己复制配置
        this.dataStore = new AsyncSearchDataStore<>(this, dataService);
        // 覆盖原客户端搜索的监听事件
        this.getSearchButton().addClickHandler(event -> {
            SearchFilter filter = getSearchFilter();
            dataStore.fetchData(filter);
        });
        // 如果有回车触发搜索的需求,也在这里绑定
        this.getSearchField().addKeyUpHandler(event -> {
            if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
                dataStore.fetchData(getSearchFilter());
            }
        });
        // 初始化时加载默认数据
        dataStore.fetchData(new SearchFilter());
    }
}

3. 使用自定义的异步搜索表格

在你的页面中直接使用这个子类,不用再处理繁琐的UI配置和数据同步:

// 初始化你的服务端接口实例
ServiceAsync dataService = GWT.create(Service.class);
// 创建异步搜索表格
AsyncSearchTableAction<YourBean> searchTable = new AsyncSearchTableAction<>(YourBean.class, dataService);
// 添加到页面容器中
yourContainer.add(searchTable.getWidget());

为什么这个方案更优

  • 完全复用原SearchTableAction的UI配置和表格渲染逻辑,避免代码冗余,后续原组件更新时你的自定义类能自动同步
  • 把数据获取逻辑和UI逻辑解耦,维护更方便
  • 天然支持扩展分页、排序等服务端交互需求

内容的提问来源于stack exchange,提问作者Ahmad Bawaneh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 00:05:27