如何从GWT中的空节点中删除"no data"标签?



我有一个从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/#!主题/谷歌网络工具包/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);
        }
    }
}
...

一些替代解决方案。可以定义扩展接口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);
            }
        }
    });
}

这就是我如何删除DataProvider 中的无数据标签

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

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。

注意,值many也是一个内部GWT节点。在这个例子中,它可能不一定仅仅是DepartmentDtoEmployeeDto

最新更新