Find the index of a column

Find the index of a column

arnorbldarnorbld Posts: 110Questions: 20Answers: 1

Hi guys,

I need to find the index of a column by name. I can get that, but what is messing me up is that a couple of columns on the LEFT side of the column I need can be set to visible or not visible. This changes the index that I need to use from 8 to 6 if both go from visible to hidden.

    var columns = table.settings().init().columns;
    table.columns().every(function(index) {
         if(columns[index].name) {
            console.log('Column ' + index + ' name is: ' + columns[index].name);
        }
    })

This shows me the column names, but I can't figure out how I can find if the column is visible or not... Note that not all the columns have names, but I'm not sure if that matters.

Essentially I need to know the column index of column with name "po_amt" so I can update the value in the column with something like:

jQuery("td:eq(6)", row).text(amtPO);

Any ideas most appreciated! :)

Best regards,

Answers

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    You can use column().visible() to determine if a column is visible - that one method is a setter and a getter. If that doesn't help, could you generate a test case, please, to help understand the issue. Information on how to create a test case is available here.

    Cheers,

    Colin

  • montoyammontoyam Posts: 568Questions: 136Answers: 5
    edited February 2022

    Kevin, @kthorngren , helped me with a similar request with this plugin:

      // API to get column indexif the supplied object key
      jQuery.fn.dataTable.Api.register( 'col.index()', function (data) {
        // Get the table settings;
        var context = this.context[0];
    
        var idx = null;
        
        // loop through the columns
        context.aoColumns.forEach(function (col, index) {
          // See if column.data setting matches parameter
          if (col.mData == data) {
            idx = col.idx;  // Save index of matching column.data
          }
        });
        return idx;
      } );
    

    you can see it in use here:
    http://live.datatables.net/rutatuka/1/edit

    the full thread of my post is here:
    https://datatables.net/forums/discussion/71484/separate-excel-sheet-for-each-row-grouping#latest

    hope this helps

  • arnorbldarnorbld Posts: 110Questions: 20Answers: 1

    Hi guys,

    Thank you so much. I was trying the visible, but I think I was trying it on the columns() object not column() - not the same thing!<g>

    I will test this out and let you know!

    Best regards,

Sign In or Register to comment.