In firefox, how to change color of text in a treecell using javascript
-
22-07-2019 - |
Question
BACKGROUND: I have the following XUL fragment
<tree id="treeToChange" flex="1">
<treecols>
<treecol label = "First Column" id="c1" flex="1"/>
<treecol label = "Second Column" id="c2" flex="1"/>
</treecols>
<treechildren>
<treeitem>
<treerow>
<treecell label="Data for Column 1"/>
<treecell label="Data for Column 2"/>
</treerow>
</treeitem>
</treechildren>
</tree>
and the following css fragment
tree { font-size: 120%; color: green;}
This causes my column data to be displayed in green text.
I have many such tree objects on the XUL page
QUESTION: In firefox, in response to a click event, which calls a javascript routine, how do I set the data for the object "treeToChange" in column 1 red and the data in column blue?
Solution 3
It turns out that the element.style.color only effects the column headings, and that within firefox, the cells in a tree structure can only be affected by coding the dataview.
code snippets follow:
// DatabaseTreeView: Create a custom nsITreeView
DatabaseTreeView: function(aTableData, aColumns) {
this.getCellProperties = function(row,col,props){
var aserv=Components.classes["@mozilla.org/atom-service;1"].getService(Components.interfaces.nsIAtomService);
props.AppendElement(aserv.getAtom("color_"+col.id));
props.AppendElement(aserv.getAtom("font_"+col.id));
};
...
and modify the css as follows
treechildren::-moz-tree-cell-text(color_c1){ color:DarkGreen}
treechildren::-moz-tree-cell-text(color_c2){ color:Navy}
treechildren::-moz-tree-cell-text(font_c1){ font-size:120%}
treechildren::-moz-tree-cell-text(font_c1){ font-size:150%}
I hope this helps someone else in the future
OTHER TIPS
The style property of a DOM element contains all the CSS declarations for that element. The naming scheme is slightly different (camelCaps instead of dashes), but otherwise exactly the same.
element.style.color = 'blue';
You can read more on the style property in the Mozilla javascript manual.
To set the colour of any element, you can use:
function changecolour(elid, nc) {
document.getElementById(elid).style.color = nc;
}
So
changecolour("c1", "red");
var x = document.getElementsByClassName("cell");
for ( var i = 0; i < x.length; i ++ ) {
changecolour(x, "blue");
}
will change the colour of the data, if you add a cell class to the cells (to avoid conflicting with other trees in the doc)
BTW, here is document.getElementsByClassName:
function getElementsByClassName(className, tag, elm){
var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
var tag = tag || "*";
var elm = elm || document;
var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
var returnElements = [];
var current;
var length = elements.length;
for(var i=0; i<length; i++){
current = elements[i];
if(testClass.test(current.className)){
returnElements.push(current);
}
}
return returnElements;
}