How do I get the sorting column text?

How do I get the sorting column text?

robertbrowerrobertbrower Posts: 158Questions: 1Answers: 0
edited May 2011 in General
To get the text of the sorting column from within my 'fnDrawCallback', I am doing this:

[code]
var xAxisLabel = $(oSettings.oInstance.dataTableSettings[0].aoColumns[oSettings.aaSorting[0][0]].nTh).attr('innerText');
[/code]

My contents of my javascript file are shown here:

[code]
$(document).ready(function () {
$('#table1').dataTable({
'bJQueryUI': true,
'sPaginationType': 'full_numbers',
'iDisplayLength': 5,
'aLengthMenu': [[5, 10, 25, -1], [5, 10, 25, 'All']],
'sScrollY': 300,
'bFilter': false,
'fnDrawCallback': function (oSettings) {
var sortColumn = oSettings.aaSorting[0][0] + 1;
if (sortColumn == 6) sortColumn = 1;
var xAxisTicks = [];
var xValues = [];
var xAxisLabel = $(oSettings.oInstance.dataTableSettings[0].aoColumns[oSettings.aaSorting[0][0]].nTh).attr('innerText');
oSettings.oInstance.find('tbody>tr').each(function () {
var tr = $(this);
var axisTick = tr.children('td:nth-child(' + sortColumn + ')').attr('innerText');
xAxisTicks.push(axisTick);
var value = parseFloat(tr.children('td:nth-child(4)').attr('innerText'));
xValues.push(value);
});
$('#chart1').html('');
$.jqplot('chart1', [xValues], {
title: 'Book Prices',
series: [{ renderer: $.jqplot.BarRenderer}],
axesDefaults: {
tickRenderer: $.jqplot.CanvasAxisTickRenderer,
tickOptions: { angle: -30, fontSize: '10pt' }
},
axes: {
xaxis: {
label: xAxisLabel,
renderer: $.jqplot.CategoryAxisRenderer,
ticks: xAxisTicks
}
}
});
}
});
});
[/code]

Is there an easier, faster, or better way to retrieve the innerText of the column which is being used for the sort?

Many, many thanks.

Robert

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    In fnDrawCallback the settings object is being passed in - so you don't need to look it up (btw the API method 'fnSettings' would be more portable). So:

    [code]
    $(oSettings.oInstance.dataTableSettings[0].aoColumns[oSettings.aaSorting[0][0]].nTh).attr('innerText');
    [/code]
    Can become:

    [code]
    $(oSettings.aoColumns[oSettings.aaSorting[0][0]].nTh).attr('innerText');
    [/code]
    Another option would be:
    [code]
    $('#table1 th.sorting_asc,#table1 th.sorting_desc').attr('innerText');
    [/code]
    Allan
This discussion has been closed.