Solved

Adding a row to a table based on its column value

Posted on 2014-04-18
9
487 Views
Last Modified: 2014-04-19
Hello Experts,

In a very long table the 2nd column is a one letter text e.g. "A".

01 | A | yellow
02 | A | black
03 | B | red
04 | B | ping
05 | B | yellow
06 | c | black

What I am trying to do is add a row before each first letter changes so....

insert ROW

01 | A | yellow
02 | A | black

insert ROW

03 | B | red
04 | B | ping
05 | B | yellow

insert ROW

06 | c | black

Here is my code which is step 1 .. getting the letter out of the column and i am not sure how to go on from here... should i use a loop?

var letterTD = $('.tdlight:eq(1)', $(this));

if (letterTD.length == 0)
return;
		
 var text = letterTD.text();

Open in new window


Thanks many .....
0
Comment
Question by:Refael
  • 5
  • 4
9 Comments
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 40009794
This seems to work
var oldValue='A';
$('tr td:nth-child(2)').each(function(){
   var newValue = $(this).text();
   if (newValue != oldValue){
     $(this).closest('tr').before( '<tr><td>1</td><td>2</td><td>3</td></tr>');
   }
});

Open in new window

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-git1.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
<table>
  <tr><td>01</td><td>A</td><td>yellow</td></tr>
  <tr><td>02</td><td>B</td><td>black</td></tr>
  <tr><td>03</td><td>C</td><td>red</td></tr>
  
  </table>
</body>
</html>

Open in new window

0
 

Author Comment

by:Refael
ID: 40009808
Hi Scott Fell (padas)

Thanks.... there is a problem.... I am getting the new row too many times.
0
 

Author Comment

by:Refael
ID: 40009810
Oh i think i know why... there are many rows with the same "A" value... so it should know to apply it only to the first one... see my example above.
0
 
LVL 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 500 total points
ID: 40009827
Ooops forgot a step.  See my working sample http://jsbin.com/wahag/1/edit?html,js,output

var oldValue='A';
$('tr td:nth-child(2)').each(function(){
   var newValue = $(this).text();
   if (newValue != oldValue){
     $(this).closest('tr').before( '<tr><td>1</td><td>2</td><td>3</td></tr>');
     oldValue=newValue;
   }
});

Open in new window

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Refael
ID: 40009844
Hi Scott Fell (padas)

Thank you so much! I added a filter to escape hidden rows and now it works perfect!
Always learning.....
0
 

Author Closing Comment

by:Refael
ID: 40009845
Works perfect. Thank you!
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 40009852
Thank you!

For anybody else using this solution, remember to wrap the jquery code in a ready function

$( document ).ready(function() {
  // code here
});

Open in new window

0
 

Author Comment

by:Refael
ID: 40009863
Yes that's obvious, i think :-)

One more comment... maybe i should post it in a new Q....

Is it possible to hide the children rows of each section e.g. A, B ?

What i mean is if you see my example above ... so all the rows with the "A" should be hidden leaving only the inserted row we crated (header) ... same for "B" and etc...

I am currently using a slice to hide these rows... counting them up each time we add new rows which is not really the best solution.

Then i am using their ID to show/hide their children on click. (something like an accordion).

Let me know if you can help me with this. Thank you once more.
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 40010262
http://jsbin.com/wahag/2/edit

This will hide all the rows we just checked. If you want something different, then a new question would be nice.

var oldValue='A';
$('tr td:nth-child(2)').each(function(){
   var newValue = $(this).text();
   if (newValue != oldValue){
     $(this).closest('tr').before( '<tr><td>1</td><td>2</td><td>3</td></tr>');
     oldValue=newValue;
   }
  $(this).closest('tr').hide();
});

Open in new window

0

Featured Post

Free Trending Threat Insights Every Day

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.

Join & Write a Comment

Suggested Solutions

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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)

760 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

22 Experts available now in Live!

Get 1:1 Help Now