开发者

YUI DataTable sort number with link

开发者 https://www.devze.com 2022-12-15 18:46 出处:网络
Ihave columns th开发者_JAVA技巧at are basically <td><a href=\"somewhere\">399.99</a>

I have columns th开发者_JAVA技巧at 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/


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


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, '');
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消