简体   繁体   English

如何在表中的某个位置插入新行

[英]How to insert new rows in some position in table

I have page with table. 我有桌子的页面。 In tbody I show data via angularjs . tbody我通过angularjs显示数据。 In thead I have same row as in tbody , but its empty, and when I filled input field and click somewhere (focus lost), one row adds to my table ( thead ). thead我有与tbody相同的行,但它是空的,当我填充输入字段并click某处(焦点丢失)时,一行添加到我的表( thead )。 And I need to make some flag on filled row, as like - rowAdded = true , because without that I clicking on input of one row and rows adds. 我需要在填充行上制作一些标志,就像 - rowAdded = true ,因为没有它我点击一行的输入和行添加。 And one more problem is that rows adds to the end of table . 还有一个问题是行添加到table的末尾。 it's all works on this script: 这一切都适用于这个脚本:

var tbody = $('.table-timesheet thead');
tbody.on('blur', ':text', function () {
    var tr = $(this).closest('tr'),
        notEmpty = $(':text', tr).filter(function () {
            return $.trim($(this).val()) != '';
        }).length;
    if (notEmpty) {
        $('.note-input').css('width', '88%').css('float', 'left');
        $('.timesheet-delete-button').css('display', 'block');
        //tr.clone(true).appendTo(tbody).find(':text').val('');
        insRow();
    }
});

function deleteRow(row) {
    var i = row.parentNode.parentNode.rowIndex;
    document.getElementById('table-body').deleteRow(i);
}


function insRow() {
    var table = document.getElementById('table-body');
    var newRow = table.rows[1].cloneNode(true);
    var len = table.rows.length;
    newRow.cells[0].innerHTML = len;

    var inp1 = newRow.cells[1].getElementsByTagName('input')[0];
    inp1.id += len;
    inp1.value = '';
    var inp2 = newRow.cells[2].getElementsByTagName('input')[0];
    inp2.id += len;
    inp2.value = '';
    table.appendChild(newRow);
}

There is my example in plunker: http://plnkr.co/edit/rcnwv0Ru8Hmy7Jrf9b1C?p=preview 在plunker中有我的例子: http ://plnkr.co/edit/rcnwv0Ru8Hmy7Jrf9b1C?p = preview

Is this what you are looking for 这是你想要的

 function insRow(ind){ var table = document.getElementById('table-body'); var newRow = table.rows[1].cloneNode(true); var len = table.rows.length; newRow.cells[0].innerHTML = ind!==undefined ? ind : len; if(ind!==undefined) $(table).find('tr:eq('+ind+')').before(newRow); else table.appendChild(newRow); } insRow(2); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="table-body"> <tbody> <tr><td>1</td></tr> <tr><td>2</td></tr> <tr><td>3</td></tr> </tbody> </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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