Solved

Spit table

Posted on 2014-01-17
6
190 Views
Last Modified: 2014-02-04
Im trying to create a function to split a table into two and put some content inbetween them, so I can display some more information.

So what Ive done is:-
$(document).ready(function () {
    $("#newTable").click(function () {
        // clone table one to create table two
        var $tableOne = $('table').attr('id', 'newTable1');
        var $tableTwo = $tableOne.clone().attr('id', 'newTable2');

        var $split = $(this).index()+1;

        // remove any rows after the split-nth index - 1 from tableOne
        $tableOne.find('tr:gt(' + $split - 1 + ')').remove();
        $tableTwo.find('tr:lt(' + $split - 1 + ')').remove();

        var $newHTML = $tableOne + "<strong>Hello</strong>" + $tableTwo;
        
        $('#newTable').replaceWith($newHTML);

    });
});

Open in new window


I'm getting NaN error but it doesn't say which line its on, and doesn't split the table.

Ive put it onto jsFiddle can someone please have a look and advise what Im doing wrong please?

Thank you
0
Comment
Question by:tonelm54
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
this is presumably the issue:
' string '  + number - number + 'string' 

Open in new window

as you are mixing data types here ...

I would suggest this code:
$(document).ready(function () {
    $("#newTable").click(function () {
        // clone table one to create table two
        var $tableOne = $('table').attr('id', 'newTable1');
        var $tableTwo = $tableOne.clone().attr('id', 'newTable2');

        var $split =  $(this).index().toString();

        // remove any rows after the split-nth index - 1 from tableOne
        $tableOne.find('tr:gt(' + $split + ')').remove();
        $tableTwo.find('tr:lt(' + $split  + ')').remove();

        var $newHTML = $tableOne + "<strong>Hello</strong>" + $tableTwo;
        
        $('#newTable').replaceWith($newHTML);

    });
}); 

Open in new window

0
 
LVL 12

Expert Comment

by:mkishline
Comment Utility
Guy Hengel is right in that the error you are receiving comes from the "-" on lines 10 and 11 of your code. There were a few issues after the error was resolved that I've corrected below:

$(document).ready(function () {
	$("#newTable").click(function () {
		// clone table one to create table two
		var $tableOne = $(this).clone().attr('id', 'newTable1');
		var $tableTwo = $tableOne.clone().attr('id', 'newTable2');

		var $split = $(this).index()+1;

		// remove any rows after the split-nth index - 1 from tableOne
		$tableOne.find('tr:gt(' + $split + ')').remove();
		$tableTwo.find('tr:lt(' + ($split + 1) + ')').remove();

		var $newHTML = $('<table>').append($tableOne).html() + "<strong>Hello</strong>" + $('<table>').append($tableTwo).html();
		
		$('#newTable').replaceWith($newHTML);

	});
});

Open in new window


Hope this helps.
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
I'm not sure if you are wanting to split from the row after where you click or on the row you click - this seperates the rows after where you click

http://jsfiddle.net/GaryC123/sNQTF/3/

$(document).ready(function () {
	$("#newTable tr").click(function () {
		// clone table one to create table two
		var $tableTwo = $("#newTable").clone().attr('id', 'newTable2');
		var $split = $(this).index()+1;

		// remove any rows after the split-nth index - 1 from tableOne
		$("#newTable").find('tr:gt(' + ($split-1) + ')').remove();
		$tableTwo.find('tr:lt(' + ($split ) + ')').remove();

		var $newHTML = "<strong>Hello</strong>" + $('<table>').append($tableTwo).html();
		
		$($newHTML).insertAfter($("#newTable"));
	});
}); 

Open in new window

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
Another approach
<!doctype html>
<html>
<head>
<title>Test</title>
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(function() {
  $('#table1 tr').click(function() {
    var rows = $('#table1 tr');
    var table2 = $('<table />');
    for(var i = $(this).index(); i < rows.length; i++) {
      table2.append($('#table1 tr:eq(' + i + ')').remove());
    }
    $('#table1').after(table2).after($('<strong/>').html('Hello World'));
  });
});
</script>
<style type="text/css">
</style>
</head>
<body>
<table id="table1">
  <tr>
    <td>Row 1 column 1</td><td>Row 1 column 2</td>
  </tr>
  <tr>
    <td>Row 2 column 1</td><td>Row 2 column 2</td>
  </tr>
  <tr>
    <td>Row 3 column 1</td><td>Row 3 column 2</td>
  </tr>
  <tr>
    <td>Row 4 column 1</td><td>Row 4 column 2</td>
  </tr>
  <tr>
    <td>Row 5 column 1</td><td>Row 5 column 2</td>
  </tr>
</table>
</body>
</html>

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Your row splitting Julian is a bit screwy.
0
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
Comment Utility
You are right (thanks Gary) - hows this
<!doctype html>
<html>
<head>
<title>Test</title>
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(function() {
  $('#table1 tr').click(function() {
    var rows = $('#table1 tr');
    var table2 = $('<table />');
	var idx = $(this).index();

    for(var i = rows.length-1; i >= idx; i--) {
      table2.prepend($('#table1 tr:eq(' + i + ')').remove());
    }
    $('#table1').after(table2).after($('<strong/>').html('Hello World'));
  });
});
</script>
<style type="text/css">
</style>
</head>
<body>
<table id="table1">
  <tr>
    <td>Row 1 column 1</td><td>Row 1 column 2</td>
  </tr>
  <tr>
    <td>Row 2 column 1</td><td>Row 2 column 2</td>
  </tr>
  <tr>
    <td>Row 3 column 1</td><td>Row 3 column 2</td>
  </tr>
  <tr>
    <td>Row 4 column 1</td><td>Row 4 column 2</td>
  </tr>
  <tr>
    <td>Row 5 column 1</td><td>Row 5 column 2</td>
  </tr>
</table>
</body>
</html>

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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)

744 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