Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force the jQuery Datatables header sort icon to the far right or far left of the cell?

The Datatables sort icon, on column headers, by default appears under the column header text. I'd like to have this appear on the same line and on the far right side or far left side of the header cell.

Here's how the HTML for one of the column headers appears (in Firebug).

<th class="ui-state-default" rowspan="1" colspan="1" style="width: 252px;">
    <div class="DataTables_sort_wrapper" style="text-align: left;">
        Account Name
        <span class="DataTables_sort_icon css_right ui-icon ui-icon-triangle-1-n"></span>
    </div>
</th>

I added display: inline-block; to the css-right class on the <span> to force it to appear on the same line as mentioned here and here. However, I still can't find how to force the icon to appear on the far right or the far left. Adding float:left; or float:right; bumps the icon to the next line.

Any ideas?

like image 681
Matt K Avatar asked Dec 21 '11 15:12

Matt K


People also ask

How to add sort icon on the left of header in JavaScript?

First, you need to initialize datatables and place the sort icon. To add icon on the left of the header in datatables add following To keep it short, we we have done is remove default implementation if sort icon and added new icon using JS.

What is DataTable header in jQuery?

DataTables is a plug-in for the jQuery Javascript library. It is a highly flexible tool, built upon the foundations of progressive enhancement, that adds all of these advanced features to any HTML table. Inside this article we will fix an issue related with DataTable header. I wrote this article after a deep research.

How do I implement DataTable in jQuery?

Implementation of jQuery DataTable To use DataTables, the first step is to include the jQuery library since it is a jQuery plugin. Secondly, two additional files need to be included to get DataTables running on your website.

How to use DataTables in WordPress?

To use DataTables, the first step is to include the jQuery library since it is a jQuery plugin. Secondly, two additional files need to be included to get DataTables running on your website. To get started, we should know that DataTables can work with data from various sources.


2 Answers

For me table.display did not exist, I added the following:

table.dataTable thead th div.DataTables_sort_wrapper {
    position: relative;
    padding-right: 20px;
}

table.dataTable thead th div.DataTables_sort_wrapper span {
    position: absolute;
    top: 50%;
    margin-top: -8px;
    right: 0;
}

The above worked for me.

like image 112
Manny Avatar answered Nov 13 '22 23:11

Manny


Though Greg's answer lead me to the solution (and is the answer I accepted), for clarity, and as a help to others that might run into this issue, I'm adding my own answer.

What most affects the positioning of the sort icons is this default DataTables CSS:

table.display thead th div.DataTables_sort_wrapper {
    padding-right: 20px;
    position: relative;
}

table.display thead th div.DataTables_sort_wrapper span {
    margin-top: -8px;
    position: absolute;
    right: 0;
    top: 50%;
}

If the class="display" attribute is not on the table, the CSS above is not applied.

like image 36
Matt K Avatar answered Nov 13 '22 23:11

Matt K