[英]How to fetch specific columns in a row and transfer it to another table
I would like to transfer a particular column only. 我只想转移一个特定的列。 Please help me.
请帮我。
Here is my code: 这是我的代码:
btnAdd.on('click', function () {
var trItem = $(this).closest("tr").clone();
trItem.find("input").remove();
trItem.add("<tr>").append("</tr>");
$("#products").append(trItem);
console.log(btnAdd);
})
thank you so much. 非常感谢。
You need to specify all cells need to be cloned. 您需要指定需要克隆的所有单元格。
Function "funcCloneRow" will clone a row (tr) and add cell's content by "tdKeyArr". 函数“funcCloneRow”将克隆一行(tr)并通过“tdKeyArr”添加单元格的内容。 Remember that "tdKeyArr" is an array contain choosen cells's position, like this:
请记住,“tdKeyArr”是一个包含选择单元格位置的数组,如下所示:
$(function() { var funcCloneRow = function($table1, $table2, trIndex, tdKeyArr) { // define. If tdKeyArr == undefinded, all the cell in choosen tr will be cloned if($table1 == undefined || $table2 == undefined || trIndex == undefined) { return; } // clone row var $tr = $table1.find('tr').eq(trIndex).clone(); // get cell content if(tdKeyArr != undefined) { $tr.children('td').text(''); for (var i = 0; i < tdKeyArr.length; i++) { $tr.children('td').eq(tdKeyArr[i]).html($table1.find('tr').eq(trIndex).children('td').eq(tdKeyArr[i]).html()); } } // append new row to second table if ($table2.children('tbody').length) { $table2.children('tbody').append($tr); } else { $table2.append($tr); } } $('table button').on('click', function(event) { funcCloneRow($('#table1'), $('#table2'), $(this).closest('tr').index(), [$(this).parent().index(), 1]); }); });
table { float:left; margin-right:100px; border-collapse:collapse; text-align:center; vertical-align:top; } table th, table td { border:1px solid #000; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> <body> <table id="table1"> <tr> <th>head 1</th> <th>head 2</th> <th>head 3</th> </tr> <tr> <td>row 1</td> <td>row 1</td> <td><button>Append</button></td> </tr> <tr> <td>row 2</td> <td>row 2</td> <td><button>Append</button></td> </tr> <tr> <td>row 3</td> <td>row 3</td> <td><button>Append</button></td> </tr> </table> <table id="table2"> <tr> <th>head 1</th> <th>head 2</th> <th>head 3</th> </tr> </table> </body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.