Solved

Adding a row to a table based on its column value

Posted on 2014-04-18
9
489 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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
 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Add Clickable Links to Google Map Markers 3 30
Javascript Case Expression 7 31
age from date of birth 4 43
Jquery-Javascript setAttribute function help 7 11
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

831 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