jQuery datatables image sorting

Mathematics

Sorting with jQuery datables, pretty easy but with images having images and "-" is bit difficult. I manage to do it for string, date and int but for images I can't think of anyway,

What I really want is to sort images and leave row which has value "-" at bottom,

http://jsfiddle.net/CYubV/73/

JS

$.extend($.fn.dataTableExt.oSort, {
    "date-eu-asc": function (a, b) {
        if (a == '-') return 1;
        else if (b == '-') return -1;
        else {
            //return ((a < b) ? -1 : ((a > b) ? 1 : 0));
            // what going to go here ?
        }
    },

        "date-eu-desc": function (a, b) {
        if (a == '-') return 1;
        else if (b == '-') return -1;
        else {
            //return ((a < b) ? 1 : ((a > b) ? -1 : 0));
            // what going to go here ?
        }
    }
});

$('#table').dataTable({
    "aoColumns": [null, {
        type: 'date-eu',
        targets: 0
    }]
});

HTML

<table id="table">
    <thead>
        <tr>
            <th>Normal</th>
            <th>Image</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>5</td>
            <td data-order=2>
                <img src='http://t1.gstatic.com/images?q=tbn:ANd9GcTU-gTaS7b67Ii3MMFaIRNQBdCinr9tNrU31GakpQGq9zok7cjKFg' />
            </td>
        </tr>
        <tr>
            <td>-</td>
            <td>-</td>
        </tr>
        <tr>
            <td>10</td>
            <td data-order=1>
                <img src='http://www.fancyicons.com/free-icons/112/must-have/png/16/stock_index_down_16.png' />
            </td>
        </tr>
        <tr>
            <td>20</td>
            <td>-</td>
        </tr>
    </tbody>
</table>
Mathematics

I made it work myself now,

http://jsfiddle.net/CYubV/76/

HTML

<table id="table">
    <thead>
        <tr>
            <th>Normal</th>
            <th>Image</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>5</td>
            <td data-order=2>
                <img src='http://t1.gstatic.com/images?q=tbn:ANd9GcTU-gTaS7b67Ii3MMFaIRNQBdCinr9tNrU31GakpQGq9zok7cjKFg' />
            </td>
        </tr>
        <tr>
            <td>-</td>
            <td data-order='-'>-</td>
        </tr>
        <tr>
            <td>10</td>
            <td data-order=1>
                <img src='http://www.fancyicons.com/free-icons/112/must-have/png/16/stock_index_down_16.png' />
            </td>
        </tr>
        <tr>
            <td>20</td>
            <td data-order='-'>-</td>
        </tr>
    </tbody>
</table>

JS

$.extend($.fn.dataTableExt.oSort, {
    "date-eu-asc": function (a, b) {
        if (a == '-') return 1;
        else if (b == '-') return -1;
        else {
            return ((a < b) ? -1 : ((a > b) ? 1 : 0));
        }
    },

        "date-eu-desc": function (a, b) {
        if (a == '-') return 1;
        else if (b == '-') return -1;
        else {
            return ((a < b) ? 1 : ((a > b) ? -1 : 0));
        }
    }
});

$('#table').dataTable({
    "aoColumns": [null, {
        type: 'date-eu',
        targets: 0
    }]
});

Added data-order = "-" for rows i want to keep at the bottom.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

JQuery DataTables elements show up and toggle but sorting doesnt work at all?

Natural sorting Ajax Datatables in Codeigniter

Disable sorting on last column when using jQuery DataTables

Is there a way to disable initial sorting for jquery DataTables?

jQuery DataTables: display rows in the original data source order, while keeping sorting feature available

Jquery Datatables column rendering and sorting

Can the jquery dataTables plugin respect alternate row colors after sorting/filtering?

Sorting after row reordering in DataTables (jQuery) does not work

jquery datatables not sorting after updating value in row with x-editable

suppressing sorting in dataTables in Shiny

Dynamic sorting in jquery DataTables

DataTables not sorting properly

jQuery DataTables: Weird sorting with Dates

Image with hyperlink in jQuery datatables not showing

Sorting datatables using a particular field

Custom sorting on values in a data-sort attribute with Jquery Datatables

JQuery DataTables server-side pagination, sorting, search with Spring MVC + JPA

datatables 1.10 and custom sorting function

Show image based on label - jQuery Datatables

Sorting Datatables with dashes and dots

jquery-datatables-rails not sorting

How to disable datatables default sorting?

Explicit Sorting for datatables

jQuery DataTables - Sorting doesn't work when column is clicked

JQuery Datatables sorting a select inside a column

jQuery dataTables: sorting a table with custom filter applied causes rows disappear

Disable sorting for a few cells in datatables jQuery

Custom Sorting with JQuery datatables

Bootstrap Datatables pagination and sorting