Column.name access/check?

Column.name access/check?

dubois_jdubois_j Posts: 7Questions: 3Answers: 0

I am looking to access a column name without using the tableselector.

I am trying to show/hide columns at startup based on column names rather than index as the table columns are likely to change down road and I don't want to have to go back and re-index all my code.

Basically I am looping over the columns().every(...) so I have a handle to the column, but want to check it's name?

Right now I have basically the following code (which works).
if ((column.index() != 0) && (column.index() != 0) && (column.index() != 1) && (column.index() != 4) ) {

But as I said the columns have already changed on me several times and I don't want to have to keep re-indexing things, so would like to check them by name instead.

I have used the following
"columnDefs": [{ "name": "xxxx"....]
So would like to use that name in my code, something to the extent.
column.name() != "xxxx"
Obviously column("xxxx:name") does not work.

I believe I can get access to it through table handle oTable.column('xxxx:name')

Thanks

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,852Questions: 1Answers: 10,519 Site admin
    Answer ✓

    Without doubt this is something that is missing in the current API. There is no column().name() method but there should be and I'll be adding it in for the next version.

    Here is a cheeky little plug-in method which uses the internal (private!) properties of DataTables to get what you are looking for:

    $.fn.dataTable.Api.register( 'column().getName()', function () {
      var ctx = this.context[0];
      return this.length ? 
        ctx.aoColumns[this[0][0]].sName :
        null;
    } );
    

    Example: http://live.datatables.net/datezufe/1/edit . I've called it column().getName() rather than column().name() since I'll be calling the built in method the latter option when it is implemented.

    Allan

  • dubois_jdubois_j Posts: 7Questions: 3Answers: 0

    Thanks Allan.

  • jzy19881108jzy19881108 Posts: 1Questions: 0Answers: 0

    Thanks Allan. save my problem

  • VyacheslavVyacheslav Posts: 70Questions: 27Answers: 0

    +1

This discussion has been closed.