技术学习

我所喜欢的

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
          "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>动态创建表格</title>
    <script src="js/jQuery1.10.js"></script>
    <script type="text/javascript">


        function CreateTable() {
            var str = '{ "total": 149, "columns": [{ "field": "EMPLOYEEID", "title": "员工编号" }, { "field": "EMPLOYEENAME", "title": "员工姓名" }],"rows":[{"EMPLOYEEID":"001","EMPLOYEENAME":"小明"},{"EMPLOYEEID":"002","EMPLOYEENAME":"小宏"}]}';
            var data = JSON.parse(str);
            var rowCount = data.rows.length;
            var cellCount = data.columns.length;

            var table = $("<table border=\"1\">");
            table.appendTo($("#createtable"));
            
            var trHeader = $("<tr></tr>");
            trHeader.appendTo(table);
            for (var j = 0; j < cellCount; j++) {
                var td = $("<td>" + data.columns[j].title + "</td>");
                td.appendTo(trHeader);
            }

            for (var i = 0; i < rowCount; i++) {
                var tr = $("<tr></tr>");
                tr.appendTo(table);
                for (var j = 0; j < cellCount; j++) {
                    var field = data.columns[j].field;
                    var val="";
                    if (data.rows[i][field] != null)
                    {
                        val = data.rows[i][field];
                    }

                    var td = $("<td>" + val + "</td>");
                    td.appendTo(tr);
                }
            }
            $("#createtable").append("</table>");
        }
    </script>
</head>

<body>
    <input type="button" value="添加表格" onclick="CreateTable(5,6)">
    <input type="button" value="添加行">
    <div id="createtable"></div>
    <div id="createrow"></div>
</body>
</html>

 

posted on 2016-03-09 16:46  飘扬De黑夜  阅读(13784)  评论(0编辑  收藏  举报