Change table columns order

前端 未结 4 1545
故里飘歌
故里飘歌 2020-12-03 06:47

I need to change the order of columns in a table in html / js dynamically, you can tell me how to do it?

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-03 07:17

    Here's a jQuery plugin I just wrote to switch the contents of two columns:

    $.fn.switchColumns = function ( col1, col2 ) {
        var $this = this,
            $tr = $this.find('tr');
    
        $tr.each(function(i, ele){
            var $ele = $(ele),
                $td = $ele.find('td'),
                $tdt;
    
            $tdt = $td.eq( col1 ).clone();
            $td.eq( col1 ).html( $td.eq( col2 ).html() );
            $td.eq( col2 ).html( $tdt.html() );
        });
    };
    

    See example →

提交回复
热议问题