Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery dataTables - left align sort icon

as you can see the sort icons on my Datatable are on the far right of the column:

enter image description here

Is it possible to align these on the left so they appear just after the text?

ie.

# ^            Technician ^              Completed Date ^

Thank you

Code as requested:

    <div class="dataTable_wrapper">
        <table class="table table-striped table-hover" id="table-d">
            <thead>
            <tr>
                <th>{% trans %} id {% endtrans %}</th>
                <th>{% trans %} technician {% endtrans %}</th>
                <th>{% trans %} date {% endtrans %}</th>
                <th>{% trans %} summary {% endtrans %}</th>
            </tr>
            </thead>
        </table>
    </div>

And:

$('#table-d').DataTable( {
    "processing": true,
    "serverSide": true,
    "ajax": "{{ path('table_data') }}",
    "pageLength": 10
})'
like image 305
b85411 Avatar asked Feb 18 '15 04:02

b85411


2 Answers

I have managed to do this by applying the following styles (better if applied as last css include file definition)

/**
 * Datatables Sorting icons on left
 */

table.dataTable thead > tr > th {
    padding-left: 30px !important;
    padding-right: initial !important;
}

table.dataTable thead .sorting:after,
table.dataTable thead .sorting_asc:after,
table.dataTable thead .sorting_desc:after {
    left: 8px !important;
    right: auto !important;
}
like image 146
Gabriel Avatar answered Nov 20 '22 14:11

Gabriel


No, that is not possible to appear right after the text, since the arrows in fact are background images in CSS classes attached dynamically to the <th>'s. But you can change the position from far right to left :

table.dataTable thead .sorting_asc {
  background: url("http://cdn.datatables.net/1.10.0/images/sort_asc.png") no-repeat center left;
}
table.dataTable thead .sorting_desc {
  background: url("http://cdn.datatables.net/1.10.0/images/sort_desc.png") no-repeat center left;
}
table.dataTable thead .sorting {
  background: url("http://cdn.datatables.net/1.10.0/images/sort_both.png") no-repeat center left;
}

enter image description here

demo -> http://jsfiddle.net/ttcz5odt/


Update - how to place arrow icons directly after text.

Gave it some extra thoughts - with a little "hack" it is indeed possible. The trick is to disable the <th> backgrounds and continuously inject / remove <span>'s with the original dataTables backgrounds instead.

CSS (besides disabling the original) :

span.arrow-hack {
  margin-left: 5px;
}
span.asc {
  background: url("http://cdn.datatables.net/1.10.0/images/sort_asc.png") no-repeat center right;
}
span.desc {
  background: url("http://cdn.datatables.net/1.10.0/images/sort_desc.png") no-repeat center right;
}
span.sort {
  background: url("http://cdn.datatables.net/1.10.0/images/sort_both.png") no-repeat center right;
}

script :

var spanSorting = '<span class="arrow-hack sort">&nbsp;&nbsp;&nbsp;</span>',
    spanAsc = '<span class="arrow-hack asc">&nbsp;&nbsp;&nbsp;</span>',
    spanDesc = '<span class="arrow-hack desc">&nbsp;&nbsp;&nbsp;</span>';

$("#example").on('click', 'th', function() {
    $("#example thead th").each(function(i, th) {
        $(th).find('.arrow-hack').remove();
        var html = $(th).html(),
            cls = $(th).attr('class');
        switch (cls) {
            case 'sorting_asc' : 
                $(th).html(html+spanAsc); break;
            case 'sorting_desc' : 
                $(th).html(html+spanDesc); break;
            default : 
                $(th).html(html+spanSorting); break;
        }
    });     
});    

update the arrow icons :

$("#example th").first().click().click();

Now it looks like what we wanted! : enter image description here

demo -> http://jsfiddle.net/dmn4q141/

like image 25
davidkonrad Avatar answered Nov 20 '22 15:11

davidkonrad