Jquery sort table data

前端 未结 7 2204
醉酒成梦
醉酒成梦 2020-12-14 00:50

I got struck in sorting tds in table using jquery.

My Demo fiddle

How can I call it for any table with id in my project?

var $sort = this;
v         


        
7条回答
  •  渐次进展
    2020-12-14 01:26

    Here's a modified version of Table sorting with jquery that works for me as a SIMPLE INTERNAL ASCENDING ROW SORTING FUNCTION

    var $tbody = $('#mytable tbody');
    
    $tbody.find('tr').sort(function(a, b) {
      var tda = $(a).attr('data-order'); // target order attribute
      var tdb = $(b).attr('data-order'); // target order attribute
      // if a < b return 1
      return tda > tdb ? 1
        // else if a > b return -1
        : tda < tdb ? -1
        // else they are equal - return 0    
        : 0;
    }).appendTo($tbody);
    
    

    提交回复
    热议问题
    d
    b
    a
    c