how to sort fractions?
how to sort fractions?
blag
Posts: 18Questions: 0Answers: 0
Alan - hi, it's me again.
I searched the forums before posting, but couldn't find any relevant posts containing the keyword 'fraction' or similar.
Is there an EASY way to sort a table on a column that contains fractions?
I would prefer to display data in a cell as, for example, [code]html8½[/code] rather than decimal [code]8.5[/code]and have this column 'sortable'.
&frac1/2; is the only fraction that my data contains, but may be, of course, prefixed by integers (range 8 to 10) or as part of a range in a cell e.g [code]8½ - 9½[/code] or [code]9½ - 10½[/code]
Some of the cells in the same column may be integers.
Great news about the Microsoft CDN, BTW!!
I searched the forums before posting, but couldn't find any relevant posts containing the keyword 'fraction' or similar.
Is there an EASY way to sort a table on a column that contains fractions?
I would prefer to display data in a cell as, for example, [code]html8½[/code] rather than decimal [code]8.5[/code]and have this column 'sortable'.
&frac1/2; is the only fraction that my data contains, but may be, of course, prefixed by integers (range 8 to 10) or as part of a range in a cell e.g [code]8½ - 9½[/code] or [code]9½ - 10½[/code]
Some of the cells in the same column may be integers.
Great news about the Microsoft CDN, BTW!!
This discussion has been closed.
Replies
Allan
Thanks for that, but that looks a bit daunting. The data I am using is stored in decimal format before being displayed in the table. At present the 'conversion' to fractions takes place in PHP on the server.
Here's my thinking - if I were to create a with a class that suppresses its display in each cell, and include in that span the decimal number as a prefix to the data I want displayed, for example, as follows
[code]8.58½[/code] will dataTables 'see' the 'hidden' data and sort the column correctly?
http://datatables.net/plug-ins/sorting#hidden_title
You just need to output the data like:
[code]
8½
[/code]
Allan
Wow - prompt (and, as ever, helpful) response
Yep - hadn't thought that one through! I tried my idea and it failed. I'll now have a go at using the plug-in that you have brought to my attention!!
I'll let you know how I get on.
Finally got it working. It would have been much easier if I had used the correct format for the sorting function. I put [code]"title_numeric"[/code"]instead of[code]"title-numeric"[/code"]which I didn't notice until I had been trying for nearly an hour.
One moment's carelessness...
Thanks for you assistance and guidance - much appreciated. When I get some dollars I send some. Can I donate in GBP??
> When I get some dollars I send some. Can I donate in GBP??
That would be awesome - thanks! And yes absolutely you can donate in GPB (I'm near Edinburgh!). If using Paypal just sent it to donate . datatables.net (@ in the middle :-) ).
Regards,
Allan
I realised that you are in Edinburgh, and since I'm in Leicester, GBP seems sensible - but I don't have any GBP, only Euros in the account at the moment, but as soon as some arrives, I'll be donating once more.
Now, for a further question on this topic...
I have adjusted the code so as to use a (legitimate) class in the span rather than a title. But the addition of a span irks me a little. I already have a class in each cell in the column that could contain fractions, and this class is almost identical to that in the extra spans, as follows[code]
9½[/code]
The cell title is generated in javascript from the cell's class.
I have tried but I have not been able to get the plug-in to work without the extra spans.
OK - my tables aren't large or very long (at most 7 columns x say 90 rows), but I like to feel that I have done what I can to eliminate 'avoidable' html, and this looks so tempting.
Any suggestions or guidance would be greatly appreciated.
Heh - yup I know exactly where you are coming from! The issue with stripping out the span is that the sort function only has access to the inner text of the cell - it doesn't have the TD element (by default).
Having said that, it is actually possible to do using custom data source sorting: http://datatables.net/development/sorting#data_source . With that you basically build an array that provides the data to be sorted. The issue with this is that on large tables it is a bit slow because of the DOM interaction.
The alternative is to use Ajax and mDataProp to provide multiple bits of information for a single cell/column.
Allan
Finally got it working with the following[code]jQuery.fn.dataTableExt.afnSortData["dom-text"]=function(a,c){var b=[];$("td:eq("+c+")",a.oApi._fnGetTrNodes(a)).each(function(){var a=$(this).prop("class");b.push(parseInt(a.match(/\d{2,4}/,a)))});return b};[/code]and added[code]{"sSortDataType":"dom-text","sType":"numeric","aTargets":[2]}[/code] in the "aoColumnDefs" initialisation.
The dom-text function has been modified by me and is specific to my particular need, but eliminates the extra spans, uses the dataTables standard "numeric" sorting functions, and uses my existing class for the cell!!
Simples.