How to change columns.type dynamically?

How to change columns.type dynamically?

mgalgsmgalgs Posts: 7Questions: 4Answers: 0

This is somewhat of a continuation of my earlier post. I'm building a table that has column with a bunch of stuff in it, and I'd like my users to be able to sort that column by any of the various bits of data inside the column.

With Allan's help I was able to successfully switch the sorting criteria dynamically. The problem I'm encountering now is that I actually need to switch the columns.type setting dynamically as well.

Here is a fiddle demonstrating the problem. The "Info" column has a "Customer Name" as well as a "Price". The user can choose to sort the Info column by either "Customer Name" or "Price" using the radio at the top of the page. DataTables is automatically setting the sType for the column to 'html', but I need it to be 'num' when the "Price" radio is selected (and html when "Customer Name" is selected). There's also a button to print out the value of sType for the column. It will make more sense when you look at the example.

So is there away to switch the columns.type setting on the fly?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,815Questions: 1Answers: 10,517 Site admin
    Answer ✓

    First thing to say is that the settings object (settings()) is considered to be private. Change property values in it is a sure fire way of breaking DataTables :smile:. I would recommend never using that method (I regret putting it in at all).

    Second point: rows().invalidate() is your friend here as well :smile:. When DataTables rereads a row (as is the case when invalidate() is called, it will also check the type. Since you can invalidate all rows, you can cause it to change the type.

    Third: Since the type is effectively dynamic here, you need to consider that option in the columns.render method and return different data based on what it is that you want to be sorted.

    Updated example.

    Regards,
    Allan

This discussion has been closed.