I have a sortable table, and I'm adding a way to show you've sorted a column.
The implementation is pretty basic; if you click a unsorted column's header, it appends a ▲ (\u25b2
) to the header name. If it's already sorted, it replaces the ▲ with ▼ (\u25bc
), or vice versa. This is all done in JavaScript.
Here is my code accomplishing this:
var headerContents = header.innerHTML;
if(headerContents.indexOf('\u25b2') != -1)
headerContents = headerContents.substring(0, headerContents.length - 1) + '\u25bc';
else if(headerContents.indexOf('\u25bc') != -1)
headerContents = headerContents.substring(0, headerContents.length - 1) + '\u25b2';
else
headerContents = headerContents + '\u25b2';
header.innerHTML = headerContents;
My issue is that the arrow is being counted as a separate word (or at least a place where the word can break), so in narrow columns the arrow gets its own line beneath the header name. That looks pretty bad.
Is there a way I can force the arrow to stay on the same line as the header's name? I'd love to keep it in pure JavaScript if possible, as I have many projects with the same JS script I'd like to add this to. If this isn't possible in JavaScript alone, though, I'm open to other solutions.
I've tried adding a non-breaking space (\xa0
) between the header and the arrow and I've tried editing the word-break
attribute of the header, but neither changed this behavior.