views:

383

answers:

2

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/

A: 

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, '');
    }
}
Luke
But then the column loses its link
Paul Tarjan
True. The easy way is to leave the link in tact and write a custom sorter. The cleaner, more performant way is to include the field in the schema twice (w diff keys), one with a parser like above, the other with none. Then in columnDefs, point to the parsed key, but specify a custom formatter that sets cell content from the unparsed key. This way the underlying data is pure, sorting is fast, and display dynamics are maintained.
Luke
A: 

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