Sort Data Type based on classnames

Sort Data Type based on classnames

ZenCocoonZenCocoon Posts: 10Questions: 0Answers: 0
edited April 2012 in DataTables 1.9
Hi,

New to DataTable I'm wondering if it's possible to give the data type of a column using classnames in it's header. I've seen the "aoColumns" option and type detection but would rather use something more generic that would simply check for classname to force the data type of a column.

Any help or path to dig through will be greatly appreciated.
P.S. : Thanks for this amazing library, really useful!

Replies

  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin
    Use aoColumnDefs with class name targets (in aTargets) - this allows a generic initialisation to be used for multiple tables: http://datatables.net/usage/columns

    > P.S. : Thanks for this amazing library, really useful!

    Thanks :-)

    Allan
  • ZenCocoonZenCocoon Posts: 10Questions: 0Answers: 0
    Checking it right now, thanks a lot for the prompt reply.

    That's impressively fast! I really appreciate it.
  • ZenCocoonZenCocoon Posts: 10Questions: 0Answers: 0
    Awesome, that just work like a charm, really handy. This is actually used on a public ruby gem : http://github.com/ZenCocoon/istart-rails
  • ZenCocoonZenCocoon Posts: 10Questions: 0Answers: 0
    I do use something like :

    [code]
    "aoColumnDefs": [ {
    "sType": "numeric",
    "aTargets": ["sort-as-numeric"]
    }
    ]
    [/code]

    and all looks great at first sight, however, clicking to header of a numeric column doesn't reorder anymore, is that an expected behavior ? Did I missed something ?
  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin
    At a glade that looks okay, however, DataTables should automatically detect numeric column types and that code should be redundant. That suggests to me that you have some non-numeric data in the column (HTML?) or numeric formatting that isn't being picked up automatically by DataTables (currency?). What is in the column you are trying to sort numerically?

    I suspect what you might need to do is use a sorting plug-in, given that is isn't being pick dup automatically: http://datatables.net/plug-ins/sorting .

    Allan
  • ZenCocoonZenCocoon Posts: 10Questions: 0Answers: 0
    This columns includes links with a number as value and it actually order them like [1,10,11,2,20,3,4,5] if no configuration is made. Using this option mentioned previously fix the ordering to be [1,2,3,4,5,10,11,20] by default but the reordering to ASC or DSC when clicking the header does not respond any more.

    Shall I rather use the HTML type ? or a sorting plugin is required ?

    The surprising thing is that it work just nice when the table is first loaded.

    Thanks Allan,
    - Seb
  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin
    What to do is include these two plug-ins:

    http://datatables.net/plug-ins/sorting#numbers_html
    http://datatables.net/plug-ins/type-detection#numbers_html

    That will allow DataTables to automatically detect this column as numeric and sorting it accordingly. You won't need to specify anything in aoColumnDefs :-)

    Allan
  • ZenCocoonZenCocoon Posts: 10Questions: 0Answers: 0
    Brilliant, thanks for that.
This discussion has been closed.