简体   繁体   中英

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 . And I want to read the entire table content and make it categorized as below output.

My <html> table content is like this.

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.

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:

$(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/


The below code, you get a JavaScript object which is associative!

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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