动态添加新行

 var order = 0;
        function AddLine()
        {
            if ($("#hid").val() == "NoSelected") {
                order += 1;
                var newRowHtml = "<tr key='Data'><td abc='left' onclick='selectRow(this,event)'>" + order + "</td><td><input type='text' name='projectName'/><input type='hidden' name='taskLevel' value='1'/></td></tr>";
                $("#project_TB").append(newRowHtml);

            }
            else {
                //已经选中了行,在选中行下面添加行
                //获取选中行的序号
                var selectedRowOrder = $("tr[class='selectedRow']").find("td[abc='left']").html();
                var nowOrder = parseInt(selectedRowOrder) + 1;
                var projectLevel_Parent = $("tr[class='selectedRow']").find("input[name='taskLevel']").val();
                if (parseInt(projectLevel_Parent) > 1) {
                    var trHTML = "<tr Key='Data'><td onclick='selectRow(this,event)' abc='left'>" + nowOrder + "</td><td class='ProjectName_td' style='padding-left:" + (parseInt(projectLevel_Parent)) * 20 + "px'><input type='text' name='projectName'/><input type='hidden' name='taskLevel' value='" + (parseInt(projectLevel_Parent)) + "'/></td></tr>";
                    $(trHTML).insertAfter($("tr[class='selectedRow']"));
                }
                else {
                    var newRowHtml = "<tr key='Data'><td abc='left' onclick='selectRow(this,event)'>" + nowOrder + "</td><td><input type='text' name='projectName'/><input type='hidden' name='taskLevel' value='1'/></td></tr>";
                    //把错误的序号改成正确的
                    $(newRowHtml).insertAfter($("tr[class='selectedRow']"));
                }
                //获取选中行的索引
                var selectedIndex = ($("tr[class='selectedRow']").index());
                //错误行的索引
                var errorIndex = parseInt(selectedIndex) + 2;
                //获取表格有多少行
                var rowCount = $("#project_TB tr").length;
                for (var i = errorIndex; i < rowCount; i++) {
                    $($("#project_TB tr")[i]).find("td[abc='left']").html(i);
                }
            }
        }

posted @ 2016-07-09 08:50  大黄人  阅读(212)  评论(0编辑  收藏  举报