Paul Tarjan
Paul Tarjan

Reputation: 50652

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/

Upvotes: 1

Views: 2068

Answers (2)

Paul Tarjan
Paul Tarjan

Reputation: 50652

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"},
];

Upvotes: 2

Luke
Luke

Reputation: 2581

You have to define a custom parser for that field to extract the number from the tag soup.

Something like

{
    key: 'num_in_there_somewhere',
    parser: function (html) {
        return +html.replace(/<.*?>|\s/g, '');
    }
}

Upvotes: 1

Related Questions