column().name()

column().name()

YaniYani Posts: 24Questions: 7Answers: 0

I'm looking for a way to grab the name of a column.

The column().name() function seems perfect but isn't available until DataTables 2.0.0:
https://datatables.net/reference/api/column().name()

Does anybody know of a way to do this in 1.6.5?

This question has an accepted answers - jump to answer

Answers

  • YaniYani Posts: 24Questions: 7Answers: 0

    Seems like using column().header() is the easiest workaround for this.

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    I have, in the past, declared my columns ahead of my data table so I could have full access to the information it contains.

  • YaniYani Posts: 24Questions: 7Answers: 0

    I don't think that would have been usable in my case. When adding new rows, I'm selecting a specific column to be edited, but with colReorder the index was kind of useless.

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    did you know about the $("#example").DataTable().column( 'salary:name' ).data(); ?

    https://datatables.net/reference/type/column-selector

  • YaniYani Posts: 24Questions: 7Answers: 0

    Yes. I already selected the column, but needed the name.

  • allanallan Posts: 61,805Questions: 1Answers: 10,118 Site admin

    The new column().name() method won't be available until v2 ships (unlikely to happen this year).

    If you wanted then before then, you could easily use them as plug-in API method. This is the code in DataTables core for them.

    Allan

  • allanallan Posts: 61,805Questions: 1Answers: 10,118 Site admin
    Answer ✓

    I haven't tested it, but this should do it:

    $.fn.dataTable.Api.registerPlural( 'columns().names()', 'column().name()', function ( setter ) {
        return this.iterator( 'column', function ( settings, column ) {
            var col = settings.aoColumns[column];
    
            if ( setter !== undefined ) {
                col.sName = setter;
                return this;
            }
            else {
                return col.sName;
            }
        }, 1 );
    } );
    
  • YaniYani Posts: 24Questions: 7Answers: 0
    edited September 2017

    Perfect! Thanks a lot Allan.

This discussion has been closed.