简体   繁体   English

jquery:在表的倒数第二行中使用 appendTo

[英]jquery: using appendTo in second to last row of table

I have a piece of the DOM that I'd like to insert into my page.我有一段 DOM 想要插入到我的页面中。 Currently I'm just blindly using:目前我只是盲目地使用:

$(myblob).appendTo(someotherblob);

How do I do the same thing, but append myblob to the second to last row that is within someotherblob.我如何做同样的事情,但将 myblob 附加到 someotherblob 中的倒数第二行。 someotherblob in this case is a table and I want to inject a row above the second to last one.在这种情况下,someotherblob 是一个表,我想在倒数第二个上方注入一行。

$('#mytable tr:last').before("<tr><td>new row</td></tr>")

You can also select the row by index selector .您还可以通过索引选择器选择行。

$('#mytable tr').eq(-1).before("<tr><td>new row</td></tr>")

Here eq is used to select the last row .这里eq用于选择最后一行。 eq is zero index and negative index starts from last. eq是零索引,负索引从最后开始。 so i used -1 in index .所以我在 index 中使用了-1

Note that with before() the elements must already be inserted into the document (you can't insert an element before another if it's not in the page).请注意,使用 before() 元素必须已经插入到文档中(如果一个元素不在页面中,则不能在另一个元素之前插入它)。

So you have to insert someotherblob first:所以你必须先插入一些其他的blob:

$('selector to insert someotherblob at')
    .append(someotherblob)
       .find('table tr:last')
          .prev()
          .before(myblob);
  • I appreciate with answer given by orin which was this one .我很欣赏orin给出的答案,这是一个

  • I tried it and found that the required column literally shifted above the enitre table.我试了一下,发现所需的列实际上移动到了整个表的上方。 It's because I do not use thead tag for the heading column.这是因为我没有将thead标记用于标题列。 Once again I started digging for the answer and came up with my own solution.我再次开始寻找答案并提出了我自己的解决方案。

  • So here it is: In my scenario it was required to show grand total of all the entries coming in every single number type column.所以这里是:在我的场景中,需要显示每个数字类型列中所有条目的总计 It was not possible for me to make calculation unless all the rows are populated on the fly.除非所有行都即时填充,否则我无法进行计算。

HTML: HTML:

<tr class="gTotal"> 
      <th colspan="4" class="head">Grand Total</th>
      <th><?php echo $grandTtlResumeSntCnt; ?></th>
      <th><?php echo $grandTtlEvalCnt; ?></th>
      <th><?php echo $grandTtlUniqEvalCnt; ?></th>
      <th><?php echo $grandTtlResCnt; ?></th>
      <th><?php echo $grandTtlUniqResCnt; ?></th>
</tr>
</table>

Jquery:查询:

$('.gTotal').insertBefore('table>tbody>tr:nth-child(2)');

let me know if I answer your question .如果我回答你的问题,请告诉我

$('#mytable tr:last').prev().after("<tr><td>new row</td></tr>")

I recently put some code online that helps you deal with table modifications:我最近在网上放了一些代码来帮助你处理表格修改:

http://notetodogself.blogspot.com/2009/08/javascript-insert-table-rows-table-body.html http://notetodogself.blogspot.com/2009/08/javascript-insert-table-rows-table-body.html

Also, you question is not too clear, but hopefully the link above will cover all bases另外,您的问题不太清楚,但希望上面的链接将涵盖所有基础

i think you want to add a row per each row of the table beetween de second and the last row.我想你想在第二行和最后一行之间的每一行中添加一行。

in this case try :在这种情况下尝试:

var rows=someotherblob.find("TR");
var nrows=rows.length;
    rows.slice(1,(nrows-2)).each(function(){
     $(this).append(myblob);
    })

A bit of a shot in the dark, but I'm guessing that you've got a footer, or maybe even data entry, at the bottom of a table, and you want to add rows to the data, before the footer.有点摸不着头脑,但我猜您在表的底部有一个页脚,甚至可能是数据条目,并且您想在页脚之前向数据添加行。 In that case, you should restructure your table to this:在这种情况下,您应该将您的表重组为:

<table id="myTable">
    <thead>
        <tr><!-- header row(s) --></tr>
    </thead>
    <tfoot>
        <tr><!-- footer row(s) --></tr>
    </tfoot>
    <tbody>
        <tr><!-- exciting and possibly dynamically inserted data goes here --></tr>
    </tbody>
</table>

Yes, tbody does come after tfoot .是的, tbody确实在tfoot之后。 Wierd, huh?很奇怪吧?

Then you can simply use this:然后你可以简单地使用这个:

$("#myTable tbody").append(newrow);

就这么简单:

$("#TableName tr:last").prev().before("<tr><td>Hello World</td></tr>");

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

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