代码之家  ›  专栏  ›  技术社区  ›  aeq

jquery在某个索引处向表中插入新行

  •  105
  • aeq  · 技术社区  · 16 年前

    我知道如何使用jquery将新行追加到表中或预先追加到表中:

    $('#my_table > tbody:last').append(html);
    

    如何将行(在HTML变量中给定)插入特定的“行索引” i . 所以如果 i=3 例如,该行将作为表中的第4行插入。

    9 回复  |  直到 8 年前
        1
  •  144
  •   Nick Craver    16 年前

    你可以使用 .eq() 和 .after() 这样地:

    $('#my_table > tbody > tr').eq(i-1).after(html);
    

    索引是基于0的,所以要成为第4行,您需要 i-1 ,因为 .eq(3) 会是第四排,你需要回到第三排( 2 )和插入 之后() 那。

        2
  •  15
  •   user113716    16 年前

    试试这个:

    var i = 3;
    
    $('#my_table > tbody > tr:eq(' + i + ')').after(html);
    

    或者:

    var i = 3;
    
    $('#my_table > tbody > tr').eq( i ).after(html);
    

    或者:

    var i = 4;
    
    $('#my_table > tbody > tr:nth-child(' + i + ')').after(html);
    

    所有这些都将把行放在同一位置。 nth-child 使用基于1的索引。

        3
  •  3
  •   Adriano Carneiro Tigran    15 年前

    注:

    $('#my_table > tbody:last').append(newRow); // this will add new row inside tbody
    
    $("table#myTable tr").last().after(newRow);  // this will add new row outside tbody 
                                                 //i.e. between thead and tbody
                                                 //.before() will also work similar
    
        4
  •  1
  •   Prateek    10 年前

    我知道这很晚了,但是对于那些只想使用 JavaScript ,您可以这样做:

    1. 获取当前的引用 tr 点击。
    2. 做一个新的 TR DOM元素。
    3. 将其添加到引用的 TR 父节点。

    HTML:

    <table>
         <tr>
                        <td>
                            <button id="0" onclick="addRow()">Expand</button>
                        </td>
                        <td>abc</td>
                        <td>abc</td>
                        <td>abc</td>
                        <td>abc</td>
         </tr>
    
         <tr>
                        <td>
                            <button id="1" onclick="addRow()">Expand</button>
                        </td>
                        <td>abc</td>
                        <td>abc</td>
                        <td>abc</td>
                        <td>abc</td>
         </tr>
         <tr>
                        <td>
                            <button id="2" onclick="addRow()">Expand</button>
                        </td>
                        <td>abc</td>
                        <td>abc</td>
                        <td>abc</td>
                        <td>abc</td>
         </tr>
    

    在JavaScript中:

    function addRow() {
            var evt = event.srcElement.id;
            var btn_clicked = document.getElementById(evt);
            var tr_referred = btn_clicked.parentNode.parentNode;
            var td = document.createElement('td');
            td.innerHTML = 'abc';
            var tr = document.createElement('tr');
            tr.appendChild(td);
            tr_referred.parentNode.insertBefore(tr, tr_referred.nextSibling);
            return tr;
        }
    

    这将在单击按钮的行的正下方添加新表行。

        5
  •  0
  •   Adam    16 年前

    使用 eq selector 选择第n行(以0为基),然后使用 after 因此:

    $('#my_table > tbody:last tr:eq(2)').after(html);
    

    其中HTML是 tr

        6
  •  0
  •   Arjan    16 年前
    $('#my_table tbody tr:nth-child(' + i + ')').after(html);
    
        7
  •  0
  •   Agus Puryanto    15 年前

    试试这个:

    $("table#myTable tr").last().after(data);
    
        8
  •  0
  •   Jaggan_j    8 年前

    加上Nick Craver的回答,同时考虑到Rossisdead提出的观点,如果场景存在,比如必须附加到空表中,或者在某一行之前,我这样做了:

    var arr = []; //array
    if (your condition) {
      arr.push(row.id); //push row's id for eg: to the array
      idx = arr.sort().indexOf(row.id);
    
      if (idx === 0) {   
        if (arr.length === 1) {  //if array size is only 1 (currently pushed item)
            $("#tableID").append(row);
        }
        else {       //if array size more than 1, but index still 0, meaning inserted row must be the first row
           $("#tableID tr").eq(idx + 1).before(row);
        }   
      }          
      else {     //if index is greater than 0, meaning inserted row to be after specified index row
          $("#tableID tr").eq(idx).after(row);
      }    
    }
    

    希望它能帮助别人。

        9
  •  -1
  •   Eli Grey    16 年前
    $($('#my_table > tbody:last')[index]).append(html);