Javascript 如何将具有数据搜索属性的行移动到另一个表?
我正在尝试将数据表中的选定行移动到另一个数据表。我有这几乎工作,但问题是与单元格上有数据搜索属性。这些数据只是作为Javascript 如何将具有数据搜索属性的行移动到另一个表?,javascript,jquery,datatables,Javascript,Jquery,Datatables,我正在尝试将数据表中的选定行移动到另一个数据表。我有这几乎工作,但问题是与单元格上有数据搜索属性。这些数据只是作为[object object]放入我的另一个表中。我试着在文档中找到一个如何处理这个案例的例子,但我没有任何运气。这是我的 HTML <table id="selected_items"> <thead> <tr> <th
[object object]
放入我的另一个表中。我试着在文档中找到一个如何处理这个案例的例子,但我没有任何运气。这是我的
HTML
<table id="selected_items">
<thead>
<tr>
<th>Item</th>
<th>Description</th>
<th>Crest Allowed</th>
<th> </th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<table id="select_items">
<thead>
<tr>
<th>Item</th>
<th>Description</th>
<th>Crest Allowed</th>
<th> </th>
</tr>
</thead>
<tbody>
<tr id="1">
<td data-search="test">1</td>
<td>Testing Bowl</td>
<td data-search="nocrest">NO</td>
<td><button class="button select">Select</button></td>
</tr>
<tr>
<td data-search="test">32</td>
<td>Cup Test</td>
<td data-search="nocrest">NO</td>
<td><button class="button select">Select</button></td>
</tr>
<tr>
<td data-search="pristine">3335</td>
<td>Bowl Test</td>
<td data-search="nocrest">NO</td>
<td><button class="button select">Select</button></td>
</tr>
<tr>
<td data-search="pristine">120</td>
<td>Plate Test</td>
<td data-search="yescrest">YES</td>
<td><button class="button select">Select</button></td>
</tr>
<tr>
<td data-search="test">1000</td>
<td>Mug Test</td>
<td data-search="yescrest">YES</td>
<td><button class="button select">Select</button></td>
</tr>
<tr>
<td data-search="pristine">65</td>
<td>Ramekin Test</td>
<td data-search="yescrest">YES</td>
<td><button class="button select">Select</button></td>
</tr>
</tbody>
</table>
您的基本问题是数据。
当td具有数据属性(数据搜索)时,它包含在数据对象中,因此您的数据数组如下所示: [{display:“1”、@data search:“test”}、“Testing Bowl”、{display:“NO”、@data search:“nocrest”},“Select”]因此数组中的第一个和第三个项实际上是对象,因此[object,object] 因此,最快的(不是我心目中最好的)方法是在添加数据之前修改数据
select_items.on("click", "button.select", function(){
var selectedRow = select_items.api().row( $(this).parents("tr") ).data();
console.log(selectedRow);
selectedRow[0] = selectedRow[0].display;
selectedRow[2] = selectedRow[2].display;
selected_items.row.add(selectedRow).draw(true);
});
我还将把数据放在json而不是html中,并使用DataTable和render函数创建按钮。
select_items.on("click", "button.select", function(){
var selectedRow = select_items.api().row( $(this).parents("tr") ).data();
console.log(selectedRow);
selectedRow[0] = selectedRow[0].display;
selectedRow[2] = selectedRow[2].display;
selected_items.row.add(selectedRow).draw(true);
});