table动态添加

<html>

    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
        <script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script>
        <style>
        
            table.default-table{
    /* -moz-border-radius: 5px;
    -webkit-border-radius:5px;
    border-radius:5px; */
    width: 100%;
    border:solid #333; 
    border-width:1px 0px 0px 1px;
    font-size: #333;
    border-collapse: collapse;
    border-spacing: 0;
}
table.default-table tbody tr{
    height: 20px;
    line-height: 20px;
}
table.default-table tbody tr.odd{
    background-color: #fff;
}
table.default-table tbody tr.even{
    background-color: #F5F5F5;
}
table.default-table tbody tr:hover{
    background-color: #eee;
}
table.default-table tbody tr th,table.default-table tbody tr td{
    padding:3px 5px;
    text-align: left;
    /* border: 1px solid #ddd; */
    border:solid #333; 
    border-width:0px 1px 1px 0px; 
}
table.default-table tbody tr th{
    font-weight: bold;
    text-align: center;
}

table.default-table tbody  tr td.tac{
    text-align: center;
}
table.default-table tbody tr td a:hover{
    color:#0080c0;
}
input[name]{
    width:100%;
    height:100%;
    
}
        </style>
    </head>
    <body>
    <form action="123" method="get">
        <table class="default-table" id = "test">
            <thead>
                <tr style="border-bottom:1px solid black;">
                        <th>名称</th>
                        <th>类型</th>
                        <th>长度</th>
                        <th>小数点</th>
                        <th>不是null</th>
                        <th>虚拟</th>
                        <th>主键</th>
                        <th>注释</th>
                        <th>操作</th>
                </tr>
            </thead>
            
            <tbody>
                <tr>
                        <td><input type="text" name="one"></td>
                        <td class="tac"><input type="text" name="two"></td>
                        <td class="tac"><input type="text" name="two"></td>
                        <td class="tac"><input type="text" name="two"></td>
                        <td class="tac"><input type="text" name="two"></td>
                        <td class="tac"><input type="text" name="two"></td>
                        <td class="tac"><input type="text" name="two"></td>
                        <td class="tac"><input type="text" name="two"></td>
                        <td class="tac"><a href="#"  οnclick="delete_this(this)">删除</a></td>
                </tr>
            </tbody>
        </table>
        <!--<button οnclick="">获取数组</button> -->
        <input type="submit" value="提交">
    </form>    
        <input type="button" id="btn2" value="增加新数据">
    
    </body>
    <script>
    function delete_this(temp){
      $(temp).parent().parent().remove(); //必须保证已经引入了jQuery,此处$(temp)先获取到<a>对象,.parent()拿到<td>,再.parent()获取到tr
    }
    $(function(){
    $("#btn2").click(function() {
        $("#test tbody").append('<tr><td><input type="text" name="one"></td><td class="tac"><input type="text" name="two"></td><td class="tac"><input type="text" name="two"></td><td class="tac"><input type="text" name="two"></td><td class="tac"><input type="text" name="two"></td><td class="tac"><input type="text" name="two"></td><td class="tac"><input type="text" name="two"></td><td class="tac"><input type="text" name="two"></td><td class="tac"><a href="#"  οnclick="delete_this(this)">删除</a></td></tr>');
    });
});

        
            //获取table内容并转成二维数组(支持colspan和rowspan)
    function getTabArray(tabId) {
        var data = [];
        if ($(tabId + ' tr').length == 0) {
            return data;
        }
        //填充数组
        $(tabId + ' tr').each(function () {
            var arr = [];
            $(this).children('th,td').each(function () {
                arr.push($(this).text().trim());
                if ($(this).attr('colspan')) {
                    for (var i = 0, len = parseInt($(this).attr('colspan')) - 1; i < len; i++) {
                        arr.push('');
                    }
                }
            });
            data.push(arr);
        });
        //二次填充
        var yIndex = 0;
        $(tabId + ' tr').each(function () {
            var xIndex = 0;
            $(this).children('th,td').each(function () {
                var t_yIndex = 0;
                if ($(this).attr('rowspan')) {
                    t_yIndex = parseInt($(this).attr('rowspan'));
                }

                for (var i = 1, len = t_yIndex; i < len; i++) {
                    var arr = data[yIndex + i];
                    arr.splice(xIndex, 0, '');
                }

                if ($(this).attr('colspan')) {
                    xIndex += parseInt($(this).attr('colspan'));
                } else {
                    xIndex++;
                }
            });
            yIndex++;
        });
        return data;
    }


    //测试调用显示到console
    getCvsData()
    function getCvsData() {
        var data = getTabArray('#test');
        var cvsLines = '';
        for (var i = 0, len = data.length; i < len; i++) {
            cvsLines += data[i].join(',') + '\n';
        }

        //debug
        for (var i = 0, len = data.length; i < len; i++) {
            console.info(data[i].join(','));
        }
        
        console.info(data)
        return cvsLines;
    }
    </script>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子杣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值