View Full Version : This is how to change the text color of a TreeNode

Stansin Cruller
31 Aug 2009, 7:14 AM
Hi everybody,
It is quite peculiar how something as trivial as setting a text color of a tree node might turn out to be a nightmare. Anyway this is how it can be done.

First , if one examines with firebug the HTML part of the page, one finds out that the CSS class ('cls' attribute of a TreeNode) that is supposed to customize the appearance of a tree node, does not in fact have full control over how the page is rendered. The reason being the fact that there is this one class(.x-tree-node a span) that basically overrides any other class's properties, and surprise it is that one class that sets the text color (this one being the only property it sets).
So what do we do? well we can directly add classes to 'span' (the element that holds the text), so as to override any other previous settings.
So we override renderElements method of TreeNodeUI, and put an 'id' attribute to every 'span', of course the attribute must be unique so we put something like '<... class="span-class-'+node.id+'"' ...'
and then inside any routine do:
Ext.get('span-class'+node.id).addClass(<CUSTOM-CLASS-NAME>); and

Inside our JavaScript:

.x-tree-node a span {
line-height: 25px !important;
font-size: 12px !important;
font-weight: normal;
color: red !important;
font-weight: bold !important;

//Inside some onClick event
//we turn the text of a node red & bold

//Inside some other onClick event
//the node returns normal

and of course we need to override the default 'renderElements' method and add an 'id' attribute to the span element
(this is taken directly from the source code of the library)

Ext.override( Ext.tree.TreeNodeUI, {
renderElements : function(n, a, targetNode, bulkRender){
// add some indent caching, this helps performance when rendering a large tree
this.indentMarkup = n.parentNode ? n.parentNode.ui.getChildIndent() : '';

var cb = typeof a.checked == 'boolean';

var href = a.href ? a.href : Ext.isGecko ? "" : "#";
var buf = ['<li class="x-tree-node"><div ext:tree-node-id="',n.id,'" class="x-tree-node-el x-tree-node-leaf x-unselectable ', a.cls,'" unselectable="on">',
'<span class="x-tree-node-indent">',this.indentMarkup,"</span>",
'<img src="', this.emptyIcon, '" class="x-tree-ec-icon x-tree-elbow" />',
'<img src="', a.icon || this.emptyIcon, '" class="x-tree-node-icon',(a.icon ? " x-tree-node-inline-icon" : ""),(a.iconCls ? " "+a.iconCls : ""),'" unselectable="on" />',
cb ? ('<input class="x-tree-node-cb" type="checkbox" ' + (a.checked ? 'checked="checked" />' : '/>')) : '',
'<a hidefocus="on" class="x-tree-node-anchor" href="',href,'" tabIndex="1" ',
a.hrefTarget ? ' target="'+a.hrefTarget+'"' : "", '><span id="span-class-'+n.id+'" unselectable="on">',n.text,"</span></a></div>",
'<ul class="x-tree-node-ct" style="display:none;"></ul>',

var nel;
if(bulkRender !== true && n.nextSibling && (nel = n.nextSibling.ui.getEl())){
this.wrap = Ext.DomHelper.insertHtml("beforeBegin", nel, buf);
this.wrap = Ext.DomHelper.insertHtml("beforeEnd", targetNode, buf);

this.elNode = this.wrap.childNodes[0];
this.ctNode = this.wrap.childNodes[1];
var cs = this.elNode.childNodes;
this.indentNode = cs[0];
this.ecNode = cs[1];
this.iconNode = cs[2];
var index = 3;
this.checkbox = cs[3];
// fix for IE6
this.checkbox.defaultChecked = this.checkbox.checked;
this.anchor = cs[index];
this.textNode = cs[index].firstChild;

16 Sep 2009, 2:14 PM
This very issue was a pain in my side, and I thought there must be an easier way. I was using Firefox, and found that the text color was actually getting overwritten by this css style in the ext-all.css:

.x-tree-node a span, .x-dd-drag-ghost a span{

The nodes themselves have an <a> tag with a <span> tag inside. Because this rule is so specific, it has to be overwritten with a rule just as specific. For your style, you can define something like the following:

.buckettextred {
font-size: 18pt;
color: #D21916;

.buckettextred a span{

Then just use the cls feature on the treeNode as it exists, no modifications necessary, and your font will be the correct color.

Hope this helps you avoid 2 hours of research like it did me.

1 Dec 2011, 6:25 AM
Thanks a lot for sharing your ideas mjaomaha (http://www.sencha.com/forum/member.php?65450-mjaomaha) & Stansin Cruller (http://www.sencha.com/forum/member.php?88283-Stansin-Cruller). It definitely saved a lot time of mine.:)