Changing column type on the fly?

Changing column type on the fly?

skipchrisskipchris Posts: 7Questions: 0Answers: 0
edited August 2010 in General
(I'm not sure if this is actually possible - either by intentional omission (because it's a little odd), or there's just no support to do it yet.)

I've got a fairly complex table, which I'm sorting using an external #SELECT element rather than column header clicks. Depending on what option is picked in the SELECT, I want a given column in the table to sort according to different comparison functions. To illustrate (badly:)

Table has 4 columns: c0,c1,c2,c3
SELECT has 8 options: op0 .. op7.

Picking a given option in the select (i.e. op1) should cause a given column (i.e. c2) to sort according to one comparison function. However, picking another option (op2) should cause the same column to sort according to a different comparison function.

How I want to achieve this (seems like the best way from the documentation but I can't make it work) is to dynamically set a new sType on the given column then call fnSort on that column, with me defining a few custom sTypes for the comparisons I need.

I can also see that this could be achieved by inserting a lot of hidden columns (about 2 for every 'real' column in my table) and sorting ASC/DESC on them. But I don't want to as it's adding a lot of duplication and page weight to the raw HTML document.

I've trawled the docs and I've found lots of bits and pieces relating to what I want to do, but can't quite see how to string it together. Any brilliant ideas would be gratefully received.

Thanks,
Chris.

Replies

  • skipchrisskipchris Posts: 7Questions: 0Answers: 0
    Update:

    ccTable.fnSettings().aoColumns[n].sType = 'newType'

    Seems to set the type. However, the question now becomes - does this way lie madness?
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Heh - it's not exactly "supported" behaviour - but it is certainly possible to do that. The sType can effect both filtering and sorting. If there filtering method remains the same (i.e. a simple string match) then it's fine, and the sorting will only apply when you actually do a sort. The fun starts when you switch between html, and string types (or anything else which effects the sorting). At that point you'd need to do a complete redraw I think (possibly rebuilding the search array - can't quite remember off the top of my head).

    Allan
  • skipchrisskipchris Posts: 7Questions: 0Answers: 0
    Will post any findings (or warnings of dire consequences) when I have them!
  • skipchrisskipchris Posts: 7Questions: 0Answers: 0
    This works fine (for now). Luckily, the filtering method is the same...

    I I have something resembling:

    [code]

    First kind of sort on col 1
    Second kind of sort on col 1
    First kind of sort on col 2
    Second kind of sort on col 2

    [/code]

    Picking an option triggers a function which uses a lookup table to determine which column the sort belongs to i.e. something like:

    [code]
    var lookup = {
    'type-name-1' : 0,
    'type-name-2' : 0,
    'type-name-3' : 1, //etc.
    }
    [/code]

    Then I just change the column type of that column, and trigger a re-sort:

    [code]
    ccTable.fnSettings().aoColumns[thisCol].sType = sortType;
    ccTable.fnSort([[thisCol,'asc']]);
    [/code]

    It's a fairly specific use-case, but hopefully this helps someone out.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Nice one - thanks for sharing this with us :-)

    Regards,
    Allan
This discussion has been closed.