簡體   English   中英

如何閱讀HTML <table> 並排序表格內容?

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

我有一個html表,其中包含三列[標題,類別,子類別]和多行。 我想讀取整個表的內容,並將其歸類為以下輸出。

我的<html>表內容是這樣的。

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

這是我真正需要的輸出格式。 我只想以下面的輸出格式打印上面的html表內容。

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



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

請幫我。

假設表具有以下代碼:

<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>

我們可以使用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);
});

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


下面的代碼,您將獲得一個關聯的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());
    });
});

下面的示例將為您提供所需的輸出。

<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>

嘗試在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);

基本上,我們從表中創建一個關聯數組,然后下一部分遍歷該數組,以您指定的格式打印該數組(如果表結構與我在其中使用的不同,則可能需要編輯該數組的設置。 JSfiddle)。

使用getElementbyId獲取數據,然后在js變量中包含整個數據時,可以對該數據執行任何操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM