行业分类
纵向合并内容相同单元格
日期:2013-01-21 14:17  点击:237

<!DOCTYPE html>
<html>
<head>
    <title>Duplicate Cell Merge</title>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.1.js" 
             type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            //模M@示Y料
            var data =
            [
                { No: "1", Name: "Jeffrey", Date: "2011/05/07", Score: 2011 },
                { No: "1", Name: "Jeffrey", Date: "2011/06/21", Score: 9999 },
                { No: "1", Name: "Jeffrey", Date: "2011/06/22", Score: 32767 },
                { No: "2", Name: "Mulder", Date: "2011/06/01", Score: 999 },
                { No: "3", Name: "Darkthread", Date: "2011/06/10", Score: 100 },
                { No: "3", Name: "Darkthread", Date: "2011/06/15", Score: 100 }
            ];
            var h = [];
            for (var i = 0; i < data.length; i++) {
                h.push("<tr>");
                var obj = data[i];
                for (var p in obj)
                    h.push("<td class='c-" + p + "'>" + obj[p] + "</td>");
                h.push("</tr>");
            }
            $("#scoreboard tbody").html(h.join('\n'));
 
            //合��容相同�位的��
            $("#btnShowMe").click(function () {
                var $lastCell = null;
                var mergeCellSelector = ".c-No,.c-Name";
                $("#scoreboard tbody td.c-No").each(function () {
                    //跟上列的td.c-No比^,是否相同
                    if ($lastCell && $lastCell.text() == $(this).text()) {
                        //取得上一列,�要合��位的rowspan + 1
                        $lastCell.closest("tr").children(mergeCellSelector)
                        .each(function () {
                            this.rowSpan = (this.rowSpan || 1) + 1;
                        });
                        //�本列被合�的�位移除
                        $(this).closest("tr").children(mergeCellSelector).remove();
                    }
                    else //若未l生合�,以目前的�位作�上一�位
                        $lastCell = $(this);
                });
            });
        });
    </script>
    <style type="text/css">
        #scoreboard { width: 400px; margin-top: 20px; }
        #scoreboard td { border: 1px solid gray; padding: 4px; }
        #scoreboard thead { text-align: center; background-color: #ddd; }
        #scoreboard tbody { text-align: center; }
        td.c-Score { text-align: right; }
    </style>
</head>
<body>
<input type="button" id="btnShowMe" value="Show Me the Trick!" />
<table id="scoreboard">
    <thead>
        <tr><td>No</td><td>Name</td><td>Date</td><td>Score</td></tr>
    </thead>
    <tbody>
    </tbody>
</table>
 
</body>
</html>
 

关于网站  |  普通版  |  触屏版  |  网页版
首页 刷新 顶部