LoginSignup
12
13

More than 5 years have passed since last update.

jQueryでテーブルの行と列を入れ替える

Last updated at Posted at 2013-05-29

HTMLのテーブルの行と列を入れ替えるスクリプトです。
需要がどこにあるかは別として…(笑)

  • jQueryが必要です。
  • テーブルのIDを指定して実行するだけ(トグル動作します)。
    function matrix_transform(tableid){
        tid = "#"+tableid;
        //後で削除するため、既存の行にtemptrクラスをつける
        $(tid+" tr").addClass("temptr");
        //thead,tbodyは行列変換で意味をなさなくなるのでタグを除去する
        $(tid+" thead,"+tid+" tbody").children().unwrap();
        $(tid+" tr:first td,"+tid+" tr:first th").each(function(){
            tds = $(this).index()+1;
            $(tid).append("<tr />");
            $(tid+" tr.temptr").each(function(){
                $(tid+" tr:eq("+$(this).index()+") > :nth-child("+tds+")").clone(true).appendTo(tid+"tr:last");
            });//tr.temptr each
        });
        $(tid+" tr.temptr").remove();
        }

プラグイン化したもの、動作デモは下記URLで
Markdown: プラグイン、デモ

12
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
13