Delete specific set of columns by column header

Delete specific set of columns by column header

NoBullManNoBullMan Posts: 61Questions: 17Answers: 2

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:

I am displaying counts for a set of error codes in a datatable. Users can select for which error codes they want to see the counts.

I need to pass table data (myTable.data()) to another function and would like to remove the columns user has not chosen before passing it. How do I remove table columns by looping through header names and compare each to a set of selected error codes? (what is the proper way of selecting the column to be removed)

Thanks.

Answers

  • kthorngrenkthorngren Posts: 20,139Questions: 26Answers: 4,734

    One option is to use toArray(), chained to the myTable.data() statement, to get a Javascript array of your data. Then you can loop through each row to copy the desired row elements into a new array. Without knowing what you have its hard to say specifically what to do but you will need a way to map the columns you want to keep. If you want help with this then please build a simple test case with an example of what you are doing so we can offer suggestions.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • NoBullManNoBullMan Posts: 61Questions: 17Answers: 2

    @kthorngren thank you for the response.
    So, there is no way to loop through column headers and delete the columns. I have to loop through rows and copy the cells I need.
    I am dealing with something 80K rows or more.

    I will try to create a test case,

  • kthorngrenkthorngren Posts: 20,139Questions: 26Answers: 4,734

    Datatables does not support dynamically adding or removing columns. To change the column config you would need to use destroy() and reinitialize the Datatable with the updated config.

    You could use columns().data() to get the data for specific columns. The resulting data structure will be different and might not be useful in your function.

    Not sure what your function is doing but maybe you can pass into the function the columns you want to ignore and the function can filter/ignore the columns during its processing.

    Kevin

  • NoBullManNoBullMan Posts: 61Questions: 17Answers: 2

    Is there a way of getting table data (myTable.data() or myTable.rows().data()) excluding the hidden columns?

  • kthorngrenkthorngren Posts: 20,139Questions: 26Answers: 4,734

    No, there aren't any parameters to pass into either data() or rows().data() to specify the columns.

    The columns().data() has column-selector options that can be used to get only the visible columns. But as I mentioned the data structure will be different with each array element containing the column's data.

    Kevin

  • NoBullManNoBullMan Posts: 61Questions: 17Answers: 2

    @kthorngren Thank you again. I appreciate your quick and helpful responses. Happy Holidays.

Sign In or Register to comment.