Using jquery, how do you reorder rows in a table based on a TR attribute?

前端 未结 2 1040
野的像风
野的像风 2020-12-14 11:23

I have a table with rows similar to the following. These rows get updated from time to time via jquery calls. Using jquery, how would I construct a function that reorders th

2条回答
  •  情深已故
    2020-12-14 12:01

    One1 Two1
    One2 Two2

    JQuery

    var $table=$('table');
    
    var rows = $table.find('tr').get();
    rows.sort(function(a, b) {
    var keyA = $(a).attr('myAttribute');
    var keyB = $(b).attr('myAttribute');
    if (keyA < keyB) return 1;
    if (keyA > keyB) return -1;
    return 0;
    });
    $.each(rows, function(index, row) {
    $table.children('tbody').append(row);
    });
    

    Working Demo is http://www.jsfiddle.net/HELUq/1/

提交回复
热议问题