简体   繁体   English

如何将克隆表的行数限制为4?

[英]How to limit the cloned table rows to 4?

=== table html structure === ===表格HTML结构===

<div id="table-container">
<table cellspacing="0" cellpadding="0">
<thead>
<tr>
<td>Table head text</td>
<td>Table head text</td>
<td>Table head text</td>
<td>Table head text</td>
<td>Table head text</td>
</tr>
</thead>
<tbody>
<tr class="row">
<td width="10%" data-group="group" data-id="table1-row1-col1" data-type="integer" class="cell-editable" title="Click to input data"></td>
</tr>
<tr class="row">
<td width="10%" data-group="group" data-id="table1-row2-col1" data-type="integer" class="cell-editable" title="Click to input data"></td>
</tr>
<tr class="row">
<td width="10%" data-group="group" data-id="table1-row3-col1" data-type="integer" class="cell-editable" title="Click to input data"></td>
</tr>
<tr class="row">
<td width="10%" data-group="group" data-id="table1-row4-col1" data-type="integer" class="cell-editable" title="Click to input data"></td>
</tr>
<tr class="row">
<td width="10%" data-group="group" data-id="table1-row4-col1" data-type="integer" class="cell-editable" title="Click to input data"></td>
</tr>
</table>
</div>

=== jquery === === jQuery ===

$( '#addNewTable' ).click(function() {
var source = $( '#table-container' ),
            clone = source.clone( true );
clone.insertAfter( source )
                .find( 'tbody tr td' ).not( 'tbody tr td:first-child, tbody tr td.title' ).text('')
                .find( 'tr.expend_row input[type="hidden"]' ).siblings().remove();
});
  • I want to clone the source table but to only 4 rows. 我想将源表克隆到仅4行。 Dynamically the source table rows will be added by 1 through a button. 动态地,源表行将通过按钮加1。 And when I clone it, the whole structure will be clone which I don't want to do only until 4 rows. 而当我克隆它时,整个结构将被克隆,直到4行我才不想做。

Try this, 尝试这个,

clone.find("tr").eq(3).nextAll().remove();

or in your case change your code like this, 或者您这样更改代码,

$( '#addNewTable' ).click(function() {
   var source = $( '#table-container' );
   clone = source.clone( true );
   clone.find("tr").eq(3).nextAll().remove();
   clone.insertAfter( source )
                .find( 'tbody tr td' ).not( 'tbody tr td:first-child, tbody tr td.title' ).text('')
                .find( 'tr.expend_row input[type="hidden"]' ).siblings().remove();
});
$('td:eq(1)').clone();
$('td:eq(2)').clone();
$('td:eq(3)').clone();
$('td:eq(4)').clone();

you can clone td one by one first four rows by applying this step or you can also append it to other as; 您可以通过应用此步骤一一克隆前四行td,也可以将其附加到另一行;

$('td:eq(1)').appendTo('.your-class-name');

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

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