Solved

Jquery: Append selected data from one table into another

Posted on 2013-06-05
3
436 Views
Last Modified: 2013-06-06
Hi Experts,

I have attached some code below. I am looking to toggle add/remove selected rows from one table to another when the checkbox in a row has been selected.

So when a user checks an input in tableA it adds the row to tableB with only the correct columns added (as you will see there are less columns in tableB)

I also need it to remove these rows from tableB when user unchecks the input in tableA

(Not removing any rows from tableA)

Many thanks for any assistance offered

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>

<body>

<table width="100%" border="0" cellspacing="0" cellpadding="0" id="tableA">
  <tr>
    <th scope="col">Select</th>
    <th scope="col">Name</th>
    <th scope="col">Birth Year</th>
    <th scope="col">Birth Month</th>
  </tr>
  <tr>
    <td><input type="checkbox" /></td>
    <td>Alex</td>
    <td>1980</td>
    <td>October</td>
  </tr>
  <tr>
    <td><input type="checkbox" /></td>
    <td>Mike</td>
    <td>1976</td>
    <td>December</td>
  </tr>
  <tr>
    <td><input type="checkbox" /></td>
    <td>Robert</td>
    <td>1954</td>
    <td>May</td>
  </tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0" id="tableA">
  <tr>
    <th scope="col">Select</th>
    <th scope="col">Name</th>
  </tr>
  <tr>
    <td><input type="checkbox" /></td>
    <td>%Name%</td>
  </tr>
</table>
</body>
</html>

Open in new window

0
Comment
Question by:maccaj51
  • 2
3 Comments
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 39223485
Something like this ?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
	$('#tableA :checkbox').click(function() {
		if ($(this).is(':checked')) addRow($(this).closest('tr'));
		else $('#' + $(this).closest('tr').attr('id') + '_sub').remove();
	});
	$('#tableB tbody').on('click', ':checkbox', function() {
		if ($(this).not(':checked')) $(this).closest('tr').remove();
	});
});
function addRow(obj)
{
	var row = $('<tr/>').attr('id', obj.attr('id') + '_sub');
	row.append($('<td/>'));
	row.append($('<td/>').html(obj.children().eq(1).html()));
	$('#tableB').append(row);
}
</script>
</head>

<body>

<table width="100%" border="0" cellspacing="0" cellpadding="0" id="tableA">
  <tr>
    <th scope="col">Select</th>
    <th scope="col">Name</th>
    <th scope="col">Birth Year</th>
    <th scope="col">Birth Month</th>
  </tr>
  <tr id="row1">
    <td><input type="checkbox" /></td>
    <td>Alex</td>
    <td>1980</td>
    <td>October</td>
  </tr>
  <tr id="row2">
    <td><input type="checkbox" /></td>
    <td>Mike</td>
    <td>1976</td>
    <td>December</td>
  </tr>
  <tr id="row3">
    <td><input type="checkbox" /></td>
    <td>Robert</td>
    <td>1954</td>
    <td>May</td>
  </tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0" id="tableB">
  <tr>
    <th scope="col">Select</th>
    <th scope="col">Name</th>
  </tr>
  <tr>
    <td><input type="checkbox" /></td>
    <td>%Name%</td>
  </tr>
</table>
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:maccaj51
ID: 39223912
Brilliant!!! Many many thanks!!!
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 39224754
You are welcome - thanks for the points.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now