[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Adding a row to a table based on its column value

Posted on 2014-04-18
9
Medium Priority
?
496 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 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 Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Suggested Courses
Course of the Month19 days, 4 hours left to enroll

834 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