如何更改GXT 3中TreeNode的字体颜色?
我尝试从ValueProvider返回SafeHtml,但这似乎只是在SafeHtml对象上调用toString()。我也尝试在ValueProvider.getValue()中获取Element,但它总是返回null。
在GXT 2中,我们使用的是ModelStringProvider并返回HTML,但我找不到任何类似的东西。
以下是我尝试过的一些示例代码:
tree=new Tree<NavigableModel<Integer>, String>(treeStore, new ValueProvider<NavigableModel<Integer>, String>() {
public String getValue(NavigableModel<Integer> _model) {
TreeNode<NavigableModel<Integer>> treeNode=tree.findNode(_model);
StringBuilder sb=new StringBuilder();
if (!_model.getActive()) {
// All elements return null
XElement elem=tree.getView().getElement(treeNode);
if(elem!=null) {
elem.getStyle().setColor("red");
}
// treeNode.getElement().getStyle().setColor("red");
// treeNode.getTextElement().getStyle().setColor("red");
// sb.appendHtmlConstant("<span class=\"item-deleted\">");
}
sb.append(_model.get("name"));
if (idsCheckBox.getValue()) {
sb.append(" ("+_model.get("id")+")");
}
// if (!_model.getActive()) {
// sb.appendHtmlConstant("</span>");
// }
return(sb.toString());
}
public String getPath() {
return("name");
}
public void setValue(NavigableModel<Integer> object, String value) {
}
});
答案 0 :(得分:0)
想出来了!
我需要将SafeHtml用于ValueProvider 并将Tree单元格设置为SafeHtmlCell,例如。
tree=new Tree<NavigableModel<Integer>, SafeHtml>(treeStore, new ValueProvider<NavigableModel<Integer>, SafeHtml>() {
public SafeHtml getValue(NavigableModel<Integer> _model) {
SafeHtmlBuilder sb=new SafeHtmlBuilder();
if(_model==null) return sb.toSafeHtml();
if (!_model.getActive()) {
// My class to make the text red if this model isn't active
sb.appendHtmlConstant("<span class=\"item-deleted\">");
}
sb.appendEscaped((String)_model.get("name"));
if (!_model.getActive()) {
sb.appendHtmlConstant("</span>");
}
return(sb.toSafeHtml());
}
public void setValue(NavigableModel<Integer> object, SafeHtml value) {
}
public String getPath() {
return("name");
}
});
// Set the cell to SafeHtmlCell to use the SafeHtml returned by ValueProvider
tree.setCell(new SafeHtmlCell());
希望这会帮助别人。