简体   繁体   English

如何阅读HTML <table> 并排序表格内容?

[英]How to read HTML <table> and sort the table content?

I have a html table with three columns [Title, Category, Sub-Category] and multiple rows . 我有一个html表,其中包含三列[标题,类别,子类别]和多行。 And I want to read the entire table content and make it categorized as below output. 我想读取整个表的内容,并将其归类为以下输出。

My <html> table content is like this. 我的<html>表内容是这样的。

Title   Category  Sub
T1         C1     S1
T2         C2     S2
T3         C2     S3
T3         C1     S1
T2         C1     S3
T1         C2     S3

Here is the output format what I really need. 这是我真正需要的输出格式。 I just want to print the above html table content in the below out put format. 我只想以下面的输出格式打印上面的html表内容。

T1 :
   C1 : S1
   C2 : S3
T2 :
   C2 : S2
   C1 : S3
T3 :
   C2 : S3
   C1 : S3



[Title]
     [Category] :[Sub-Category]

Please help me. 请帮我。

Assuming the table has this code: 假设表具有以下代码:

<table>
    <thead>
        <tr>
            <th>Title</th>
            <th>Category</th>
            <th>Sub</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <th>T1</th>
            <td>C1</td>
            <td>S1</td>
        </tr>
        <tr>
            <th>T2</th>
            <td>C2</td>
            <td>S2</td>
        </tr>
        <tr>
            <th>T3</th>
            <td>C2</td>
            <td>S3</td>
        </tr>
        <tr>
            <th>T3</th>
            <td>C1</td>
            <td>S1</td>
        </tr>
        <tr>
            <th>T2</th>
            <td>C1</td>
            <td>S3</td>
        </tr>
        <tr>
            <th>T1</th>
            <td>C2</td>
            <td>S3</td>
        </tr>
    </tbody>
</table>

We can use jQuery to get output like this: 我们可以使用jQuery来获得如下输出:

$(document).ready(function(){
    $("table").hide();
    $("body").append("<textarea></textarea>");
    var s = "";
    $("tbody tr").each(function(){
        s += $(this).find("th").html() + ":\n";
        s += $(this).find("td:nth-child(2)").html() + ": " + $(this).find("td:nth-child(3)").html() + "\n\n";
    });
    $("textarea").val(s);
});

Fiddle: http://jsfiddle.net/praveenscience/9ueervw4/ 小提琴: http : //jsfiddle.net/praveenscience/9ueervw4/


The below code, you get a JavaScript object which is associative! 下面的代码,您将获得一个关联的JavaScript对象!

$(document).ready(function(){
    $("table").hide();
    $("body").append("<textarea></textarea>");
    var s = {};
    $("tbody tr").each(function(){
        if (!s[$(this).find("th").html()])
            s[$(this).find("th").html()] = [];
        s[$(this).find("th").html()].push($(this).find("td:nth-child(2)").html() + ": " + $(this).find("td:nth-child(3)").html());
    });
});

The below example will give you the output you want. 下面的示例将为您提供所需的输出。

<html>
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
    <script language="javascript">
        $(document).ready(function () {
            var outputtemp = "<table>";
            $("#tbl1 tr").not("tr th").each(function () {
                outputtemp += "<tr>"
                var i = 0;
                $(this).find("td").each(function () {
                    if (i == 0)
                        outputtemp += "<td>" + $(this).text() + ":</td></tr><tr>";
                    else if (i == 1)
                        outputtemp += "<td></td><td>" + $(this).text() + ":</td>";
                    else
                        outputtemp += "<td>" + $(this).text() + "</td>";
                    i++;
                });
                outputtemp += "</tr>"
            });
            outputtemp += "</table>"
            $(".output").html(outputtemp);
        });
    </script>
</head>
<body>
    <table id="tbl1">
        <tr>
            <th>
                Title
            </th>
            <th>
                Category
            </th>
            <th>
                Sub
            </th>
        </tr>
        <tr>
            <td>
                T1
            </td>
            <td>
                C1
            </td>
            <td>
                S1
            </td>
        </tr>
        <tr>
            <td>
                T2
            </td>
            <td>
                C2
            </td>
            <td>
                S2
            </td>
        </tr>
        <tr>
            <td>
                T3
            </td>
            <td>
                C3
            </td>
            <td>
                S3
            </td>
        </tr>
    </table>
    <div class="output">
    </div>
</body>
</html>

Try using associative arrays in javascript/jQuery: DEMO 尝试在javascript / jQuery中使用关联数组: DEMO

var tableRowArray = $('table tr');
var titleArray = [];
var mainArray = {};
// build an associative array of the values int he table
tableRowArray.each(function(){
    if($(this).children('th').length == 0) {
        var tempTitle = $(this).children('td').eq(0).text();
        if(mainArray[tempTitle]){
            mainArray[tempTitle][$(this).children('td').eq(1).text()] = $(this).children('td').eq(2).text();
        }
        else {
            titleArray.push(tempTitle);
            mainArray[tempTitle] = {};
            mainArray[tempTitle][$(this).children('td').eq(1).text()] = $(this).children('td').eq(2).text();
        }
    }
});
// print the formatted associative array to the page
$('body').append("<p></p>");
var formattedString = "";
$.each(titleArray, function(index, value){
    formattedString += "<b>" + value + " : </b><br/>";
    for(var key in mainArray[value]) {
        formattedString += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + key + " : " + mainArray[value][key] + "<br/>";
    }
});
$('p').html(formattedString);

Basically, we create an associative array from your table and then the next piece iterates through the array to print it in the format you specified (you may need to edit the set up of the array if you table structure varies from what I used the in JSfiddle). 基本上,我们从表中创建一个关联数组,然后下一部分遍历该数组,以您指定的格式打印该数组(如果表结构与我在其中使用的不同,则可能需要编辑该数组的设置。 JSfiddle)。

使用getElementbyId获取数据,然后在js变量中包含整个数据时,可以对该数据执行任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM