Sort Table Rows according to table data

前端 未结 3 570
梦谈多话
梦谈多话 2021-01-13 16:49

For example, I have a code:

相关标签:
3条回答
  • 2021-01-13 17:08

    Have a look at Sorting - we're doing it wrong. A simple jQuery plugin for sorting stuff is available here.


    some notes on your code:

    // you're binding a document ready event within a function call? 
    // looks the wrong way 'round, to me
    function sortTheTable(){
        $(function() {
            // 1) you probably want to use .detach() over .remove()
            // 2) "tr:has(.price)" will match ALL table rows 
            // containing an element with the class .price
            // even if they're children of different <table>s!
            // 3) $('.selector') is already "an array", at least it's sortable right away.
            // there's no need for $.makeArray() here
            var elems = $.makeArray($('tr:has(.price)').remove())
            elems.sort(sortNum)
            // "#information" is a sufficient (and more efficient) selector, 
            $('table#information').append($(elems));
        });
    }
    
    0 讨论(0)
  • 2021-01-13 17:17

    You have number of plugins to sort it why are you reinventing the wheel. Here is one such plugin

    Link

    <script type="text/javascript" src="/path/to/jquery-latest.js"></script> 
    <script type="text/javascript" src="/path/to/jquery.tablesorter.js"></script> 
    
    $("#myTable").tablesorter(); 
    
    0 讨论(0)
  • 2021-01-13 17:31

    Instead of

    return 1 * $(a).find('.sort').text() < 1 * $(b).find('.sort').text() ? 1 : 0;
    

    insert

    return 1 * $(a).find('.sort').text() < 1 * $(b).find('.price').text() ? 0 : 1;
    

    http://jsfiddle.net/E56j8/

    0 讨论(0)
提交回复
热议问题
name price