Merging two table into single table JQuery
I have two separate table, I want to merge into a single table using JQuery. But the out put I am seeing is not the correct one. Any help regarding this will be appreciated.
Source Table
<html>
<table class="left-column">
<tbody>
<tr>
<td>Name
</td>
</tr>
<tr>
<td>Age
</td>
</tr>
</tbody>
</table>
<table class="right-column">
<tbody>
<tr>
<td>Andy
</td>
</tr>
<tr>
<td>30
</td>
</tr>
</tbody>
</table>
</html>
Target Table
<html>
<table>
<tr>
<td>
<table class="left-column">
<tbod开发者_开发百科y>
<tr>
<td>Name
</td>
</tr>
<tr>
<td>Age
</td>
</tr>
</tbody>
</table>
</td>
<td>
<table class="right-column">
<tbody>
<tr>
<td>Andy
</td>
</tr>
<tr>
<td>30
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
</html>
Jquery Code snippet
var leftTable=$('table.left-column').clone();
var rightTable=$('table.right-column').clone();
var newTable= $('div').append('<table><tr><td>')
.append(leftTable).append('</td><td>')
.append(rightTable).append('</td></tr></table>')
.html();
return newTable;
If I understand correctly what you're trying to do, maybe something like this will work:
$('table.left-column').find('tr').each( function(i){
$(this).append( $('table.right-column').find('tr').eq(i).find('td') );
});
Just do this:
$('table').wrap('<td></td>')
.parent().wrapAll('<table><tbody><tr></tr></tbody></table>');
Example: http://jsfiddle.net/6y4qt/
If you have other tables on the page, then replace:
$('table')
with:
$('.left-column,.right-column')
How about:
var leftTable = $(".left-column tbody").html();
var rightTable = $(".right-column tbody").html();
var newTable = "<table></table>";
return newtable.append(leftTable).append(rightTable);
How about something like this (jsFiddle). This gives exactly the output you've requested in 3 short lines of code. (NOTE, this code actually MOVES the individual tables into the joined table instead of copying/cloning them.
$('div').html('<table border=1><tr><td id="cell1"></td><td id="cell2"></td></tr></table>');
$('#cell1').html($('.left-column').clone());
$('#cell2').html($('.right-column').clone());
Possibly a little more robust than you were looking for, but very reusable, here's a function that you can call and optionally pass a parent container to select tables within and the option to remove them from the page after processing them, but this will return to you all your tables in a combined structure like you specified.
function mergeTables(parent, remove){
//if no parent is passed, default to body
if (!parent){
parent = 'body';
}
//place to store the full HTML string of all the combined tables
var table_html = '<table><tr>';
//go through all tables
$(parent).find('table').each(function(){
//add their HTML to our big HTML string
table_html += '<td><table>' + $(this).html() + '</table></td>';
//remove that table from the page
if (remove){
$(this).remove();
}
});
//close off and return the big HTML string
return table_html + '</tr></table>';
}
//call example
$('#container').html( mergeTables('#container') );
You can't use "partial elements" like '<table><tr><td>'
. Change the code to:
var leftTable = $('table.left-column' ).clone();
var rightTable = $('table.right-column').clone();
var newTable = $('div').append('<table><tr><td></td><td></td></tr></table>');
newtable = $('td:first', newtable).append(leftTable );
newtable = $('td:last' , newtable).append(rightTable);
精彩评论