datatables 1.10 shifts sorting to previous col-index ? why ?

datatables 1.10 shifts sorting to previous col-index ? why ?

babablacksheepbabablacksheep Posts: 41Questions: 23Answers: 0

Hi,
In datatables 1.10, if click oon header to sort column and then turn that column visibility of by api and reload table,
table shifts that sort (asc or desc) from hidden col to previous col while sending ajax params.
it just shifts sort info but u can see no classes applied to previous indexed col.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin

    Can you please link to a test case showing the issue, as noted in the forum rules.

    Allan

  • babablacksheepbabablacksheep Posts: 41Questions: 23Answers: 0
    edited February 2015

    @allan
    See jsfiddle: http://jsfiddle.net/bababalcksheep/ntcwust8/

    1. By default column 0 is in sorting when table is rendered.
    2. check console for submitted post data, I am printing .order key to investigate what order is being submitted to server. Right now it is [Object { column=0, dir="asc"}]
    3. Now click on FirstName button to toggle column-index-0
    4. now press reload table button to trigger ajax reload of table and check console again.
    5. At console, you can see it still submits order [Object { column=0, dir="asc"}]which is wrong because column-index-0 was originally FirstName col and turned off.

    I wish datatables do not change index of columns it created for the first time.
    even if column visibility is changed for any number of columns. this way we can ref to column-index.
    but as soon as you reorder or turn off column, indexes are changed and there is no way to track back to actual cols.

  • babablacksheepbabablacksheep Posts: 41Questions: 23Answers: 0
    edited February 2015

    Dear @allan
    possible solutions would be

    1. if classes like sorting_asc are added before ajax call so one can iterate dom and find sorting columns
    2. column indexes are defined one time and do not change or altered.
      so if column 2 is hidden , its index is reserved and should only refer to its column
  • babablacksheepbabablacksheep Posts: 41Questions: 23Answers: 0

    Dear @allan,
    based on answer http://datatables.net/forums/discussion/79/column-name-instead-of-index-for-sorting-filtering
    Is there a way i can get order data by name instead of indexes ?

    I am creating columns like

    columns: [{
      'data': 'id',
      'title': 'ID',
      'name': '_id',
    } {
      'data': 'title',
      'title': 'Title',
      'name': '_title',
    }]
    

    is there a possibility that datatables give me ordering in form of
    [{ column="_id", dir="asc"}] based on column names ?

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin

    At console, you can see it still submits order [Object { column=0, dir="asc"}]which is wrong because column-index-0 was originally FirstName col and turned off.

    This statement is incorrect. You are assuming that hidden columns cannot be sorted, which is wrong. They can be.

    The column() selector method will use the column data index unless the :visible pseudo selector is specified (in which case it uses the visible index).

    So where you have:

    <button class="toggleCols" data-column="0">Last Name</button>

    That is wrong - it should be data-column="1" if you want to target the last name column using data-column.

    Allan

  • babablacksheepbabablacksheep Posts: 41Questions: 23Answers: 0
    edited February 2015

    thank you.

    But is it possible to get order() data based on names instead of col indexes

    Secondly Correct me if I am wrong, if a column is at position 2 and if it is hidden, it will still have index 2 , right ?

  • babablacksheepbabablacksheep Posts: 41Questions: 23Answers: 0

    Ok I found the solution.
    in
    ```
    'ajax': {
    type: 'POST',
    'url': url,
    'data': function (d) {
    console.log(d.order);
    return JSON.stringify( d );
    }
    }
    '''
    data has 2 keys , columns and order.and order index refer to index state of columns which is array.
    What I was doing wrong that I was searching dom for every index in found in order while I should search in data.columns array which holds current state of table.

    indexes reffered in data.order refer to data.columns very nicely.

    SOLVED

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    Answer ✓

    But is it possible to get order() data based on names instead of col indexes

    The order information is based on the column data indexes. If you want the column name you would need to convert it.

    Secondly Correct me if I am wrong, if a column is at position 2 and if it is hidden, it will still have index 2 , right ?

    Correct. Changing the visibility of a column doesn't alter its data index.

    Allan

This discussion has been closed.