Wicket 1.4中使用DataView作为动态搜索结果表

发布于 2024-12-01 04:29:43 字数 481 浏览 0 评论 0原文

我对 Wicket 还很陌生,但我已经遇到了一个非常奇怪的问题。

我正在创建一个页面,其中包含一个非常基本的搜索表单和一个最初为空的结果表(DataView)。当用户在字段中输入数据并单击“搜索”时,应用程序会调用一些后端服务,然后将这些服务用于填充数据视图。

然而,用户必须单击“搜索”两次才能显示数据。

我终于找到了这个问题,这是因为 Wicket 使用零来表示第一次“搜索”点击时显示的项目数。第二次单击时,行已添加,Wicket 已计算出要显示的正确行数,因此它决定显示数据。

在 AbstractPageableView.getItemModels() 中,要显示的结果的大小最初为零,因为我可能没有加载带有任何初始数据的表。

我通过在页面加载时加载带有空行的 DataView 解决了这个问题。这似乎欺骗 DataView 使用显示第一次“搜索”点击的数据。

我的问题是:我这样做对吗?还有其他中继器更适合这项任务吗?这是一个错误还是什么?

I'm fairly new to Wicket but I've already run into a very strange problem.

I'm creating a page with a pretty basic search form and a results table (a DataView) which is initially empty. When the user enters data into the fields and clicks "Search", the app calls some backend services which are then used to populate the DataView.

However the user has to click "Search" twice for the data to be displayed.

I finally tracked this down, and it's because Wicket is using zero for the number of items to be displayed for the first "Search" click. At the second click, the rows have already been added and Wicket has already calculated the proper number of rows to display, so it decides it will show the data.

In AbstractPageableView.getItemModels(), the size of the results to display is initially zero, because I don't load the table with any initial data probably.

I got around this problem by loading the DataView with empty rows on page load. This seems to trick the DataView into using the displaying the data for the first "Search" click.

My question is: am I doing this right? Is there another repeater that is better for this task? Is this a bug or something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红焚 2024-12-08 04:29:43

终于破解了:这是因为我只在 iterator() 方法中加载数据提供程序中的数据,而数据提供程序的 size() 方法通常在 iterator() 方法之前调用。我应该在它自己的方法中加载数据并从 iterator() 和 size() 调用该方法。这样做解决了它。

之前的数据提供者(Splc 是 DTO):

SearchResultsDataProvider implements IDataProvider<Splc> {

  /**
   * The list of search results
   */
  private List<Splc> models;

  @Override
  public void detach() {
    // Do nothing
  }

  @Override
  public Iterator<Splc> iterator(int first, int count) {
    // load the data into the list of models
    models = service.getSplcModels();
    return models.subList(....).iterator();
  }

  @Override
  public IModel<Splc> model(Splc object) {
    return new Model<Splc>(object);
  }

  @Override
  public int size() {
    return models.size();
  }
}

之后的数据提供者:

SearchResultsDataProvider implements IDataProvider<Splc> {

  private List<Splc> getModels() {
    // load the data into the list of models
    return service.getSplcModels();
  }

  @Override
  public void detach() {
    // Do nothing
  }

  @Override
  public Iterator<Splc> iterator(int first, int count) {
        return getModels().subList(....).iterator();
  }

  @Override
  public IModel<Splc> model(Splc object) {
    return new Model<Splc>(object);
  }

  @Override
  public int size() {
    return getModels().size();
  }
}

Finally cracked it: it was because I was loading the data in my data provider only in the iterator() method, and the data provider's size() method is usually called before the iterator() method is. I should have been loading the data in its own method and calling that method from iterator() and size(). Doing that fixed it.

Data Provider before (Splc is the DTO):

SearchResultsDataProvider implements IDataProvider<Splc> {

  /**
   * The list of search results
   */
  private List<Splc> models;

  @Override
  public void detach() {
    // Do nothing
  }

  @Override
  public Iterator<Splc> iterator(int first, int count) {
    // load the data into the list of models
    models = service.getSplcModels();
    return models.subList(....).iterator();
  }

  @Override
  public IModel<Splc> model(Splc object) {
    return new Model<Splc>(object);
  }

  @Override
  public int size() {
    return models.size();
  }
}

Data Provider after:

SearchResultsDataProvider implements IDataProvider<Splc> {

  private List<Splc> getModels() {
    // load the data into the list of models
    return service.getSplcModels();
  }

  @Override
  public void detach() {
    // Do nothing
  }

  @Override
  public Iterator<Splc> iterator(int first, int count) {
        return getModels().subList(....).iterator();
  }

  @Override
  public IModel<Splc> model(Splc object) {
    return new Model<Splc>(object);
  }

  @Override
  public int size() {
    return getModels().size();
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文