更改节点的样式

时间:2013-10-29 06:21:02

标签: gwt gxt

在我的项目中,我使用 gwt 2.5.1 gxt 3.0.1 。 我创造了树。

    protected void createTree(TreeStore<ProjectExtended> treeStore) {

    ValueProvider<ProjectExtended, SafeHtml> provider = new ValueProvider<ProjectExtended, SafeHtml>() {

        @Override
        public void setValue(ProjectExtended object, SafeHtml value) {

        }

        @Override
        public SafeHtml getValue(ProjectExtended object) {

            if(object == null) return SafeHtmlUtils.EMPTY_SAFE_HTML;

            if (!object.getActive()) {
                return SafeHtmlUtils.fromTrustedString(
                        "<span class=\"projectNotActive\">" + object.getName() + 
                        "</span> " + "<span class=\"dictIdText\"> ( " + 
                        object.getId() + " / " + object.getParentId() + " ) </span>");
            } else {
                return SafeHtmlUtils.fromTrustedString(
                        object.getName() + "<span class=\"dictIdText\"> ( " +
                        object.getId() + " / " + object.getParentId() + " ) </span>");
            }
        }

        @Override
        public String getPath() {
            return "name";
        }
    };

    Tree<ProjectExtended, SafeHtml> tree = new Tree<ProjectExtended, SafeHtml>(treeStore, provider);
    tree.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);

    add(tree);
}

我尝试更改非活动节点的样式。但我无法解决这个问题,因为浏览器显示错误!

http://i.stack.imgur.com/0Jmdq.png

有办法解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

我解决了这个问题。帮助的文章是here

有一个简单的代码示例。

  1. 您应该创建一个继承的类AbstractCell并覆盖render方法:

    public class SimpleAbstractCell extends AbstractCell<String> {
    
        @Override
        public void render(Context context, String value, SafeHtmlBuilder sb) {
            /*
            * Always do a null check on the value. Cell widgets can pass null to
            * cells if the underlying data contains a null, or if the data arrives
            * out of order.
            */
            if (value == null) {
                return;
            }
    
            SafeHtml safeValue = SafeHtmlUtils.fromTrustedString(value);
            sb.append(safeValue);
        }
    }
    
  2. 在创建树时,您应该声明contentCell:

    protected void createTree(TreeStore<ProjectExtended> treeStore) {
    
        ValueProvider<ProjectExtended, String> provider = new ValueProvider<ProjectExtended, String>() {
    
            @Override
            public void setValue(ProjectExtended object, String value) {
    
            }
    
            @Override
            public String getValue(ProjectExtended object) {
    
                if(object == null) return "";
    
                if (!object.getActive()) {
                    return 
                        "<span class=\"projectNotActive\">" + object.getName() + 
                        "</span> " + "<span class=\"dictIdText\"> ( " + 
                        object.getId() + " / " + object.getParentId() + " ) </span>";
                } else {
                    return 
                        object.getName() + "<span class=\"dictIdText\"> ( " +
                        object.getId() + " / " + object.getParentId() + " ) </span>";
                }
            }
    
            @Override
            public String getPath() {
                return "name";
            }
        };
    
        Tree<ProjectExtended, String> tree = new Tree<ProjectExtended, String>(treeStore, provider);
        tree.setCell(new SimpleAbstractCell());
        tree.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
    
        add(tree);
    }
    
  3. 这就是全部。我希望它对smb有用。