?
Solved

Adding a row to a table based on its column value

Posted on 2014-04-18
9
Medium Priority
?
494 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 53

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
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 
LVL 53

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 53

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 53

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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)
Suggested Courses

765 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