2
votes

I need to disable sorting for all the columns with no data in it. I am looking to do it using DataTable or jQuery. If I have multiple tables, I should be able to iterate through all of them and return true or false for each column. So that I can take the result and disable column sorting. This is what I have so far, eventhough the code is incomplete.

function detectEmptyColumn(index) {

var sStatus = [], status ='';
var colors = ['red','green','blue','yellow','purple','black','cyan','green'];

$('.data-table').eq(index).find('tbody').each(function(i) {
    var self = this;
    $(self).find('tr td').each(function(j) {
        if (j > 0) {
            $(self).find('tr').each(function(k) {
                var $tableCell  = $(this).find('td:visible').eq(j);

                $tableCell.css('border','2px solid '+colors[j]);
                var $innerWrapper = $('.inner-wrapper', $tableCell);
                status = ('a', $innerWrapper).size() > 0;
                sStatus[k] = status;
                if (status) return false;
            });
        }
    });
});

return status;

}


function disableEmptyColumnSorting() {

$('.data-table').each(function(index) {
    var status = detectEmptyColumn(index);
});

}

And this is how Datatable td looks like:

<td class="reading-level interpTRC_no_interp
    firstCol
     "><div class="inner-wrapper" style="position: relative;">
        <span title="-5" class="sortindex"></span>


        <a href="/test/test123">

            &lt;PC

                <sup>F</sup>
        </a>
</div></td>
1
Will you know before you initialize datatables which column(s) will not have data? If so you can apply the bsortable: false property in the aoColumns section. DataTables ColumnsWar10ck
The data in columns changes based on some criteria. So it's always different.user1916494

1 Answers

0
votes

Detecting an emtpy column is way easier then you do atm:

function isColumnEmpty(index)
{
    return $("tbody td").filter(function(){
        return $(this).index() == index && $.trim($(this).text()).length;
    }).length == 0;        
}

Above function uses the filter on the TD, then detect the index and compare it to the index of the column passed and check if the text content is larget than 0. Since all HTML tags are filtered out, you will only have the text.

What plugin are you using so I can check out how to disable the sorting?