【问题标题】:How to remove "no data" labels from empty nodes in GWT?如何从 GWT 中的空节点中删除“无数据”标签?
【发布时间】:2023-04-02 17:13:01
【问题描述】:

我有一个继承自 CellTree 的小部件。如果节点没有子元素,则可以打开该节点并显示"no data"标签。

我希望看到没有子节点的节点显示为空。

这就是我填充树的方式。我的DictionaryTreeDataProvider 班级(相关部分):

public class DictionaryTreeDataProvider extends ListDataProvider<MValue> {
    private final DictionariesServiceAsync service = GWT.create(DictionariesService.class);

    ...    

    @Override
    public void onRangeChanged(HasData<MValue> result) {
        service.queryDictionaryValues(range, query, new AsyncCallback<SubsetResult<MValue>>() {
            @Override
            public void onFailure(Throwable t) {
            }

            @Override
            public void onSuccess(SubsetResult<MValue> result) {
                getList().clear();
                for (MValue value : result.items) {
                    getList().add(value);
                }
            }
        });
    }
}

在服务器端,我进行 EJB 调用,该调用填充 SubsetResult。 我发现这个问题在 GWT-2.5.0-rc2 版本中得到修复(参见https://groups.google.com/forum/#!topic/google-web-toolkit/d-rFUmyHTT4)。


现在一切正常,感谢@moutellou。 我按照他的建议做了:

...
@Override
public void onSuccess(SubsetResult<MValue> result) {
    if (result.length == 0) {
        updateRowCount(-1, true);
        return;
    } else {
        for (MValue value : result.items) {
            // some checks here
            getList().add(value);
        }
    }
}
...

【问题讨论】:

  • 如何获取数据。你在使用 AsyncDataProvider 吗?

标签: gwt gwt-2.4 gwt-widgets


【解决方案1】:

一些替代解决方案。可以定义扩展接口CellTree.Resources的接口。 在此界面中必须指定 CSS 的路径,该路径会覆盖所需的样式。

接口CellTree.Resources

public class CellTree extends AbstractCellTree implements HasAnimation,
    Focusable {
   ...  
  /**
   * A ClientBundle that provides images for this widget.
   */
  public interface Resources extends ClientBundle {

    /**
     * An image indicating a closed branch.
     */
    @ImageOptions(flipRtl = true)
    @Source("cellTreeClosedArrow.png")
    ImageResource cellTreeClosedItem();

    /**
     * An image indicating that a node is loading.
     */
    @ImageOptions(flipRtl = true)
    ImageResource cellTreeLoading();

    /**
     * An image indicating an open branch.
     */
    @ImageOptions(flipRtl = true)
    @Source("cellTreeOpenArrow.png")
    ImageResource cellTreeOpenItem();

    /**
     * The background used for selected items.
     */
    @ImageOptions(repeatStyle = RepeatStyle.Horizontal, flipRtl = true)
    ImageResource cellTreeSelectedBackground();

    /**
     * The styles used in this widget.
     */
    @Source(Style.DEFAULT_CSS)
    Style cellTreeStyle();
  } 
...
}

接口CustomCellTreeResources,基于CellTree.Resources

import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.user.cellview.client.CellTree;

public interface CustomCellTreeResources extends CellTree.Resources {
    static final String STYLE_PATH = "components/common/client/static/custom-cell-tree.css";

    @Override
    @ClientBundle.Source({CellTree.Style.DEFAULT_CSS, STYLE_PATH})
    CellTree.Style cellTreeStyle();
}

覆盖规则:

.cellTreeEmptyMessage {
    display: none;
}

创建一个实例:

private final static CellTree.Resources customCellTreeResources = 
    GWT.create(CustomCellTreeResources.class);

接下来需要将customCellTreeResources 显式传递给CellTree 类构造函数。

消息没有显示更多。

强制:在归档列表之前,即在点击一个节点之前,应该清理列表(getList().clear();):

@Override
public void onRangeChanged(HasData<MValue> result) {
    service.queryDictionaryValues(range, query, 
          new AsyncCallback<SubsetResult<MValue>>() {
        @Override
        public void onFailure(Throwable t) {}
        @Override
        public void onSuccess(SubsetResult<MValue> result) {
            getList().clear();
            for (MValue value : result.items) {
                getList().add(value);
            }
        }
    });
}

【讨论】:

    【解决方案2】:

    这就是我删除 DataProvider 中无数据标签的方法

     //Fetch children
      int size = children.size();
      if (size == 0) {
         updateRowCount(-1, true); //Method called on AsyncDataProvider 
         return;
      }  
    

    【讨论】:

      【解决方案3】:

      TreeViewModel 中,如果参数值没有子值,请确保isLeaf 方法返回true。示例:

      @Override
      public boolean isLeaf(Object value) {
          if (value instanceof DepartmentDto) {
              DepartmentDto department = (DepartmentDto) value;
              return department.getEmployees().isEmpty();
          } else if (value instanceof EmployeeDto) {
              return true;
          } else {
              return false;
          }
      }
      

      在这种情况下,只有在没有员工的情况下,部门才应将自己声明为叶子,员工将自己声明为叶子,并且默认为 false。

      请注意,value many 也是一个内部 GWT 节点。在此示例中,它可能不一定只是 DepartmentDtoEmployeeDto

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-13
        • 1970-01-01
        • 2017-08-13
        • 1970-01-01
        相关资源
        最近更新 更多