Exclude first column (Sl. No) from sort.

Exclude first column (Sl. No) from sort.

MirMir Posts: 27Questions: 7Answers: 1
edited April 2015 in Free community support

I have a table with three columns and the first one being Sl. No (row count), which should not be sorted. The other two columns are sortable and they are sorting. However my issues are:

  1. On sort of second/third column, the first column is also sorting as it belong to a row, which should not happen. With this my sl. no (first column) is displayed in random order

  2. Even after making the first column "sortable: false", onload, the first column is showing the sort icon (although the column does not sort on click). However the icon goes off when any other sortable column is clicked.

Could anyone see if something is missing.

Here is my code snippet:

HTML:

<table id="myTableId" class="myTable display">
    <thead>
        <tr>
            <th width="5%">Sl. No</th>
            <th width="30%">Name</th>
            <th width="65%">Description</th>
        </tr>
    </thead>
    <tbody></tbody>
</table>

JS:

function initMyTable() {
    myTable = $("myTableId")
            .dataTable(
                    {
                        scrollY:        "250px",
                        scrollCollapse: false,
                        jQueryUI:       true,
                        "bJQueryUI": true,
                        bProcessing : true,
                        bRetrieve : true,
                        bDestroy : true,
                        "bPaginate": true,
                        "bSearch":false,
                        bFilter: false, 
                        bInfo: false,
                       "sPaginationType": "input",
                       "bLengthChange" : true,
            
                        "aoColumns" : [
                                {
                                        "bVisible" : true,
                                       "bSortable" : false,
                                       "sClass": "centerAligned",
                                },
                                
                                {
                                     "bSortable" : true,
                                     "bVisible" : true,
                                    "mRender" : function(data, type, full) {  
                                          return full[1];
                                     }
                                },
                                {
                                     "bSortable" : true,
                                     "bVisible" : true,
                                    "mRender" : function(data, type, full) {  
                                        return full[2];
                                   }
                                }]
                    });
}

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,787Questions: 1Answers: 10,115 Site admin
    Answer ✓

    The default sort (order) also needs to be changed.

    Allan

  • MirMir Posts: 27Questions: 7Answers: 1

    Issue# 2 got resolved by adding - "order": [[ 2, 'asc' ], [ 3, 'asc' ]].

    Thanks Allan.

    Issue#1 still open. Any thoughts?

  • allanallan Posts: 61,787Questions: 1Answers: 10,115 Site admin

    On sort of second/third column, the first column is also sorting as it belong to a row

    I must confess I don't quite understand. Can you link to the page showing the issue so I can debug it please.

    Allan

  • MirMir Posts: 27Questions: 7Answers: 1

    I am not aware of the option to link thee image. But here is my case:

    On Load:

    Sl. No.                Name ^                       Description ^
    ---------------------------------------------------------------------
      1                    FIL                            Air Filter
      2                    PIP                           Fuel Pipe
      3                    BAT                          DC Battery
    

    On clicking the Name (sortable) column, Actual Result is (Sl. No is sorting):

    Sl. No.                Name ^                         Description ^
    ---------------------------------------------------------------------
      3                    BAT                          DC Battery
      1                    FIL                            Air Filter
      2                    PIP                           Fuel Pipe
    

    Expected Result (Sl. No should NOT sort):

    Sl. No.                Name ^                        Description ^
    ----------------------------------------------------------------------
      1                    BAT                          DC Battery
      2                    FIL                            Air Filter
      3                    PIP                           Fuel Pipe
    

    Hope this gives you the idea what I am at.

  • allanallan Posts: 61,787Questions: 1Answers: 10,115 Site admin

    Yes thank you, however, I'm not sure what would cause that. I would need a link to a page that shows the problem so I can debug it. If you can't link to the page you are working on, please use JSFiddle or http://live.datatables.net to provide a test case.

    Allan

  • MirMir Posts: 27Questions: 7Answers: 1

    Allan

    I have created my test table here, please check:

    http://live.datatables.net/vusujuse/7/edit

  • allanallan Posts: 61,787Questions: 1Answers: 10,115 Site admin

    Thank you. And when you click on the Name column in your demo, do you see the incorrect sorting (i.e. sorting by the first column, rather than by the second)? For me it sorts as expected on the Name column.

    Allan

  • MirMir Posts: 27Questions: 7Answers: 1

    Sorting is correct. But I want to somehow stop elements of first column from sorting themselves. Meaning allow sort of rows except the first column. I need first column rows in same ascending order regardless of sort on other sortable columns as my first column is just a serial number for rows.

  • allanallan Posts: 61,787Questions: 1Answers: 10,115 Site admin

    I need first column rows in same ascending order regardless of sort on other sortable columns

    Ah - that was what I was missing before or didn't understand, sorry.

    Use the orderFixed option to force a column to be the first ordered.

    Allan

  • MirMir Posts: 27Questions: 7Answers: 1

    Looks like that option is restricting the second & third column sort - making then unsortable. Not sure if I have used in correctly. Could you please see that here:

    http://live.datatables.net/vusujuse/8/edit

  • allanallan Posts: 61,787Questions: 1Answers: 10,115 Site admin

    Looks like it is working as expect to me. You said before:

    I need first column rows in same ascending order regardless of sort on other sortable columns

    Which is exactly what is happening. The first column is always sorting ascending first and the other columns sorting secondary. Since the first column contains unique data, it it appears that it is only the first column that is sorted.

    If that isn't what you want, could you clarify please and I'm afraid I've misunderstood.

    Allan

  • MirMir Posts: 27Questions: 7Answers: 1

    Allan, Thanks for all the effort you have been putting to get this issue resolved. Yes, I do need what I said before & the first column is always sorted in asc order as expected.

    However what I am trying to say is, the change made ("orderFixed") to acheive first column order-fixed has impacted the sorting functionality of other two (Name/Description) columns. They have become UNSORTABLE

    If you try to sort on this example code, you find it not working on any of the other two columns: http://live.datatables.net/vusujuse/8/edit

    Please let me know if it is not clear to you.

  • MirMir Posts: 27Questions: 7Answers: 1

    I got it!!! Will post the solution in some time.

  • allanallan Posts: 61,787Questions: 1Answers: 10,115 Site admin

    They have become UNSORTABLE

    No - they are still sortable, but since the first column always takes priority (per the use of orderFixed) they will appear unsortable since the first column forces the sorting, which is what you said you wanted.

    I don't actually understand what you want to happen if you want the first column to always sort first. That is what is happening.

    Allan

  • MirMir Posts: 27Questions: 7Answers: 1

    Allan, you are right as I could see the 'Processing...' message in the process, but first column forcing sort was not required. I think it would have been easy for you to understand had I knew and mentioned the exact term for that first column of mine - its "Index column".

    Here is what I required & its there in dataTable documentation -
    http://datatables.net/examples/api/counter_columns.html#

    Here I implemented that:
    http://live.datatables.net/vusujuse/10/edit.

    Thanks much Allan for the support and help you provided. With this both of my issues got resolved.

  • allanallan Posts: 61,787Questions: 1Answers: 10,115 Site admin

    Phew :-) We got there in the end!

This discussion has been closed.