Howto style custom sorting icons
Howto style custom sorting icons
Hello,
I haven't been able to figure out how to create a custom icon styling when I'm *not* using bJQueryUI. When bJQueryUI is true, an extra span element is added to each which is handy to style an icon.
With my own custom styles, I can't figure out how to style my column headers with colors, backgrounds, etc.. and then still specify the sort icon? Any hints?
Thanks.
tj
I haven't been able to figure out how to create a custom icon styling when I'm *not* using bJQueryUI. When bJQueryUI is true, an extra span element is added to each which is handy to style an icon.
With my own custom styles, I can't figure out how to style my column headers with colors, backgrounds, etc.. and then still specify the sort icon? Any hints?
Thanks.
tj
This discussion has been closed.
Replies
To style all headers the same, it would normally be something generic:
th { background-color: #FF0000 }
But due to the rules of CSS Specificity, this doesn't work. It leaves you two options: either add to the existing two selectors:
table.display thead th, table.display tfoot th { background-color: #FF0000 }
Or in this case if those selectors are not already declaring the properties you want to modify, even:
table.display th {background-color: #FF0000}
----
I've used background-color, but of course you would put any other declarations in there. For example, if you wanted an icon to the left of the header text, you would need to set something like:
table.display th {background-image: url('images/icons/myIcon.png'); background-repeat: none; padding-left: 20px }
The padding-left makes room for the icon. (note: use short-form CSS where possible; I'm just using long-form in the example)
----
If you want to style individual header cells, it's a simple matter of adding a class to your pure HTML. DataTables will add classes, not replace them.
myHeader
Will render after initialization as
myHeader
---
Hope that helps.
One more option also springs to mind (although perhaps less attractive) - add in the span elements yourself using HTML or Javascript and then style them as needed.
Allan
I've played with creating a generic 'th' class and then building on that. One thing I'm running into yet is that I want a background image in my 'th' class, and then the sort icons will overwrite that as they use a background image definition as well. Not sure how to get around that one, other than to turn on bJQueryUI as Allan suggested? I'm not familiar with howto add in span elements via HTML or Javascript?
FWIW, as an inexperienced CSS user.. it does seem like it would be simpler if there were icon classes defined for column sorting like there are for all the other styling details when bJQueryUI is off. Or at least a way to define them in oStdClasses.
If you need to support older browsers, spans should work as you say. I wonder if the sTitle "column" parameter allows insertion of HTML? Certainly fnHeaderCallback would; I've used fnRowCallback in a similar way; my code snippet should be in one of my recent discussions, if you need me to dig it up!
Alternatively, it would be pretty simple to just add them in with a standard non-DataTables jQuery function. This would add a SPAN inside all headers:
[code]
$('th').wrapInner('')
[/code]
You can execute this from a number of different places. Perhaps the fnDrawCallback parameter? And then, assuming each of your TH has a unique id, you can then style those spans. So, given:
[code]
...etc
[/code]
Then your CSS would look something like this:
[code]
th { background-color: #FF0000} // whatever is common to all headers
th > span { display: block; padding-left: 20px} // whatever is common to all span children of headers
th#col_name > span { background: url('images/someNameIcon.png') no-repeat center left;
th#col_address > span { background: url('images/AddressIcon.png') no-repeat center left;
[/code]
(keeping in mind that the ">" child selector doesn't work in IE6 if you're still supporting IE6. I personally wouldn't.)
There are many ways to get the IE6 support, and many different approaches you could take to solve the problem. For that matter, the jQuery "adding a span" method isn't even the only way of modifying the DOM to suit your needs. Tonnes of different ways to do it. Heck, there's not even a good reason I used "span" other than you suggested it; it could as easily be a div and then you wouldn't need to set display:block because it's already done. ;-)
Hope that helps.
(side note: Allan, is there a parameter we can give the "code" shortcode? Something like "code lang='css'"?)
Allan