jQuery table sort

后端 未结 15 2813
温柔的废话
温柔的废话 2020-11-22 09:00

I have a very simple HTML table with 4 columns:

Facility Name, Phone #, City, Specialty

I want the user to be able to sort by Faci

15条回答
  •  迷失自我
    2020-11-22 09:30

    My vote! jquery.sortElements.js and simple jquery
    Very simple, very easy, thanks nandhp...

                $('th').live('click', function(){
    
                var th = $(this), thIndex = th.index(), var table = $(this).parent().parent();
    
                    switch($(this).attr('inverse')){
                    case 'false': inverse = true; break;
                    case 'true:': inverse = false; break;
                    default: inverse = false; break;
                    }
                th.attr('inverse',inverse)
    
                table.find('td').filter(function(){
                    return $(this).index() === thIndex;
                }).sortElements(function(a, b){
                    return $.text([a]) > $.text([b]) ?
                        inverse ? -1 : 1
                        : inverse ? 1 : -1;
                }, function(){
                    // parentNode is the element we want to move
                    return this.parentNode; 
                });
                inverse = !inverse;     
                });
    

    Dei uma melhorada do código
    One cod better! Function for All tables in all Th in all time... Look it!
    DEMO

提交回复
热议问题