Is column.settings()[0].aoColumns[columnIndex].sType still the recommended way to find column.type?

Is column.settings()[0].aoColumns[columnIndex].sType still the recommended way to find column.type?

JiminyJiminy Posts: 13Questions: 4Answers: 0

Description of problem:
I'm just looking for an update on an answer that was given back in 2018 here https://datatables.net/forums/discussion/comment/124148/#Comment_124148

I'm in a similar situation where I have defined some date formats using commands like this:
jQuery.fn.dataTable.moment( 'DD-MM-YYYY' );

We recently added the accent neutraliser and this is causing some issues with our custom date filters, which I think I can solve if I can figure out the column's type. From that 2018 discussion I see that I can use column.settings()[0].aoColumns[columnIndex].sType to determine the type. However it feels a bit kludgy especially as for the Moment dates it returns "moment-DD-MM-YYYY" etc, so I'd have to figure out all possible date types we register and bucket those together to be handled as dates.

Ideally I'd like to be able to query the column type directly to see if it's a date. Is column.settings()[0].aoColumns[columnIndex].sType still the recommended/only way to do this?

thanks as always for an amazing product.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,635Questions: 1Answers: 10,092 Site admin

    Accessing the settings object externally is never recommended. It is not a documented API so it can change between versions.

    That said, there is no other way to get the type information for a column at the moment. I would 100% recommend wrapping it in an API method so that if the internal interface does change (unlikely, but you never know) then you only have a single place to change it.

    Allan

  • allanallan Posts: 61,635Questions: 1Answers: 10,092 Site admin
    Answer ✓

    Here is a little API plug-in to do that wrapping:

    DataTable.Api.registerPlural(
        'columns().type()',
        'column().type()',
        function ( selector, opts ) {
            return this.iterator( 'column', function ( settings, column ) {
                return settings.aoColumns[column].sType;
            }, 1 );
        }
    );
    

    Then you can do table.column(...).type().

    Allan

  • JiminyJiminy Posts: 13Questions: 4Answers: 0

    Many many thanks Allan for your help.

Sign In or Register to comment.