?
Solved

Adding a row to a table based on its column value

Posted on 2014-04-18
9
Medium Priority
?
495 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 54

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 54

Accepted Solution

by:
Scott Fell,  EE MVE earned 2000 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 54

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 54

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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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 how to dynamically set the form action using jQuery.
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…
Suggested Courses

650 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