How to create tables from column data instead of row data in HTML?

前端 未结 6 1785
抹茶落季
抹茶落季 2020-12-14 06:11

According to this article at W3 Schools, one can create a basic table in HTML like this:

row 1, c
6条回答
  •  孤街浪徒
    2020-12-14 06:13

    You're best bet is to render the table by rows, and then use javascript to invert the table

    http://jsfiddle.net/CsgK9/2/

    The following code will invert a table (this sample code uses jquery)

    
        $("table").each(function() {
            var $this = $(this);
            var newrows = [];
    
            $this.find("tr").each(function(){
                var i = 0;
    
                $(this).find("td").each(function(){
                    i++;
                    if(newrows[i] === undefined) { newrows[i] = $(""); }
                    newrows[i].append($(this));
                });
            });
    
            $this.find("tr").remove();
            $.each(newrows, function(){
                $this.append(this);
            });
        });
    

    UPDATE:

    Here is a version that works with th elements as well: http://jsfiddle.net/zwdLj/

提交回复
热议问题