Search code examples
datatableyuiyui-datatableyui2

YUI DataTable sort number with link


I have columns that are basically

<td><a href="somewhere">399.99</a>

If I set parser:"number" on the column, I get a blank column, but if I don't, the sorting is not a numeric sort.

Is there a better parser that can handle links around the number?

The code is for http://paulisageek.com/compare/cpu/


Solution

  • 7 views, a new minimum record for me when i find the answer.

    I had to define my own sort function (using an undefined 3rd param http://yuilibrary.com/projects/yui2/ticket/2528649).

    function sortNumbersWithLinks(a, b, desc, field) {
        a = a.getData(field).replace(/<[^>]+>/, '');
        b = b.getData(field).replace(/<[^>]+>/, '');
    
        a = parseFloat(a);
        b = parseFloat(b);
    
        return YAHOO.util.Sort.compare(a, b, desc);
    }
    
    var myColumnDefs = [
                 {key:"Name", sortable:true},
                 {key:"Performance", sortable:true, sortOptions:{sortFunction:sortNumbersWithLinks}},
                 {key:"Price", sortable:true, sortOptions:{sortFunction:sortNumbersWithLinks}},
                 {key:"Performance / Price", sortable:true, parser:"number"},
    ];