Default order AJAX datatable by data property

Default order AJAX datatable by data property

gb691gb691 Posts: 7Questions: 3Answers: 0
edited August 27 in Free community support

Hi,

I have a DataTable whose contents are being loaded via AJAX. I know that to default order a DataTable on load I can use the order: [[10,"asc"]] property, however, this requires that I don't later decide to move my columns about. Is there a way to order by specifying a column name or something that isn't tied to the column's position? I've tried the following but have had no luck. It doesn't have to be the data property, just anything that prevents me from having to specify the column's position, so that if I decide to change things around later, I don't have to change the order property too.

order: {
        data: "studentId",
        dir: "desc"
}

An example of one of my columns.

columns: [
    { 
        data: "studentId",
        title: "Student ID",
        visible: true
    }
]

Apologies if this has been covered elsewhere but I couldn't see anything on the order reference pages.

Answers

  • allanallan Posts: 65,947Questions: 1Answers: 10,977 Site admin

    You can use the columns.name property for this. The final example on the order reference page shows it:

    new DataTable('#myTable', {
        columns: [
            { name: 'first_name' },
            { name: 'last_name' },
            { name: 'position' },
            { name: 'city' }
        ],
        order: {
            name: 'city',
            dir: 'asc'
        }
    });
    

    Possibly I should look at allowing the data property to be used as well to reduce possible boiler plate, although the data property can be nested JSON keys or null, so it wouldn't always work.

    Allan

  • gb691gb691 Posts: 7Questions: 3Answers: 0

    Hi @allan,

    Thanks for getting back to me.

    I've added a 'name' property to the column I wish to order by:

    { 
        data: "dbsAgeInDays",
        name: "dbsAge",
        title: "DBS Age (Days)",
        visible: true
    }
    

    and have changed my order to the following:

    order: {
        name: "dbsAge",
        dir: "desc"
    }
    

    But it doesn't work unfortunately. Is it because this is the only column I've given a name property to?

  • allanallan Posts: 65,947Questions: 1Answers: 10,977 Site admin

    What version of DataTables are you using? I've just tried with the current release (3.0.2 at the time of writing) and it appears to work as expected: https://live.datatables.net/bicadeje/1/edit .

    Regards,
    Allan

  • gb691gb691 Posts: 7Questions: 3Answers: 0

    Hi @allan,

    Ah ok, it's probably the version I'm using then. I'm developing for a platform, the owners of which, have forked dataTables and effectively put their own wrapper around it to better work with their product. Best I can tell, it's version 1.13.11, but this may not be accurate.

    Regards,
    Guy

  • allanallan Posts: 65,947Questions: 1Answers: 10,977 Site admin

    Hi Guy,

    That would do it. Per the note in that example on the order reference page, that ability was added in 2.0.

    You can use DataTable.version to check the version, but it certainly does sound like you might have an old one.

    Allan

  • gb691gb691 Posts: 7Questions: 3Answers: 0

    Hi, @allan,

    Thanks for your help on this. I'll have to stick to using the index. Unfortunately, I'm not in control over the version of DataTables the company providing the platform chooses to use. It would definitely be nice if they updated to a newer version!

    Guy

  • allanallan Posts: 65,947Questions: 1Answers: 10,977 Site admin

    Out of interest, are you at liberty to say what platform it is that you are using?

Sign In or Register to comment.