sort table by price column

前端 未结 5 897
野趣味
野趣味 2021-01-23 20:44

It\'s the billing list:

Service   Price
---------------
S1        13 CHF
S2        Free
S3        Free
S4        40 CHF

I want to sort it by pr

5条回答
  •  误落风尘
    2021-01-23 21:22

    If you don't want to use heavy plugins, you cant to sort it manually:

    $(function(){
        var table=$('#table');
        var rowsArray=$('tr',table).sort(function(current,next){
            var compareCurrent=$('td.price',current).text().toUpperCase();
            var compareNext=$('td.price',next).text().toUpperCase();
            return (compareCurrent compareNext) ? 1 : 0;
        });
        $('tr',table).remove();
        $.each(rowsArray,function(index,element){
            $(element).appendTo(table)
        })
    })
    

    In this example you should add class "price" to cells with prices. Or you can use pseudo-selector ":last-child".

提交回复
热议问题