0

I have a table that looks like this:

JS:

"columns": [
            {
                "data": 0,
                "iDataSort" : 0
            },
            {
                "data": 1,
                "iDataSort" : 1,
                "render": function ( data, type, row ) {
                    var returnString = data;
                    if( row[2] !== null && row[2].length > 0){
                        returnString += "<ul>";
                        for( var alternativeTitle in row[2] ){
                            returnString += "<li>" + row[2][alternativeTitle] + "</li>"
                        }
                        returnString += "</ul>"
                    }
                    return returnString;
                }
            },
            {
                "iDataSort" : 3,
                "data": 3
            },
            {
                "iDataSort" : 4,
                "data": 4
            },
            {
                "data":null,
                "bSearchable": false,
                "bSortable": false,
                "sClass": "center",
                "render": function (row) {
                    return '<a href"#">Edit</a>';
                }

            }

        ],

HTML :

        <table>
            <thead>
            <tr role="row" class="heading">
                <th>
                    Id#
                </th>
                <th>
                    Original title (alternative titles)
                </th>
                <th>
                    Year of production
                </th>
                <th>
                    Country
                </th>
                <th>
                    Actions
                </th>
            </tr>
            </thead>
            <tbody>
            </tbody>
        </table>

As you can see, I provide a 5 column array as a data source (id, title, alternative titles[], year of production and country) but I render them differently (original title and alternative titles go in the same column while the last column is for action buttons). Now I want to disable sorting on the last column (as that would make no sense, because its reserved for buttons) but when I put "bSortable": false on the last column with buttons, it affects the last column visually, but also affects the GET sent by datatables, in effect when I click on "sort by country", nothing happens, because datatables sends "bSortable_4 = false to the server.

I want to be able to sort on the Country column, but to disable sorting on the Actions column. How to achieve this?

msniezko
  • 321
  • 1
  • 3
  • 11

1 Answers1

2

Here is the html

<table id="example">
            <thead>
            <tr role="row" class="heading">
                <th>
                    Id#
                </th>
                <th>
                    Original title (alternative titles)
                </th>
                <th>
                    Year of production
                </th>
                <th>
                    Country
                </th>
                <th>
                    Actions
                </th>
            </tr>
            </thead>
            <tbody>
            <tr><td>1</td><td>1</td><td>1</td><td>1</td><td>delete</td></tr>
            <tr><td>2</td><td>2</td><td>4</td><td>5</td><td>delete</td></tr>
            </tbody>
        </table>

Here is javascript

$(document).ready(function() {
$('#example').dataTable({
"order": [],
"aoColumnDefs": [
          { 'bSortable': false, 'aTargets': [ 4 ] }
       ],
});
});

Here is working fiddle https://jsfiddle.net/rtn6496n/10/

Hope it helps !

Pardeep Poria
  • 1,049
  • 1
  • 7
  • 12
  • It did not work. When I used your solution, it disabled sorting on both 'Actions' and 'Country' column. I was able to click on 'Country' column, but nothing happened, because the Datatables sent 'bSortable_4' == false, where 4 is the Country column in the server columns array (0 - id, 1 - original title, 2 - alternative titles, 3 - year, 4 - country) – msniezko May 11 '16 at 08:36
  • It is working in the fiddle i sent, please try improvise as per your needs, thank you ! – Pardeep Poria May 11 '16 at 08:39