tags:

views:

84

answers:

2

Hi All, I am stuck with the problem of table expanding. I am populating data into a table, and the last column of the table has more content in it, So using jquery is to possible to get all the text in the last column of a table row, append the details into the next row ? Why i am trying this weird thing is to use jQuery table expand plugin... any tips.. please post...

Sample table data before jQuery manipulation

<table width="700" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td>001</td>
        <td>Name</td>
        <td>1210 W. Valley Dr, Los Angeles, 91742</td>
      </tr>
      <tr>
        <td>002</td>
        <td>Name</td>
        <td>PO BOX 1719, Chicago, 60608</td>
      </tr>
      <tr>
        <td>003</td>
        <td>Name</td>
        <td>RR 3 BOX 78, Kaufman, 75142</td>
      </tr>
    </table>

Result table data after jQuery manipulation

<table width="700" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>001</td>
    <td>Name</td>
    <td>View</td>
  </tr>
  <tr>
    <td colspan="3">1210 W. Valley Dr, Los Angeles, 91742</td>
  </tr>
  <tr>
    <td>002</td>
    <td>Name</td>
    <td>View</td>
  </tr>
  <tr>
    <td colspan="3">PO BOX 1719, Chicago, 60608</td>
  </tr>
  <tr>
    <td>003</td>
    <td>Name</td>
    <td>View</td>
  </tr>
  <tr>
    <td colspan="3">RR 3 BOX 78, Kaufman, 75142</td>
  </tr>
</table>
+1  A: 

I think this solves your problem.

$(function(){    
    $("#tbl1 tr" ).each ( function() {
     var text = $(this).find ( "td:last-child").text();
     $(this).find ( "td:last-child").text('view');
     $(this).after ( "<tr><td colspan='3'>" + text + "</td></tr>" );
    });
});
rahul
I liked the `alert` :)
Doug Neiner
Thanks :-) works like a charm. But a small prob, When i test it locally it works fine, but in the application i get an error saying "missing ) after argument list"... apart from this, there is no other script on the page..
Sullan
A: 

jQuery has 50 million ways to do one task, but this will do what you want:

$(function(){
    $('table tr td:nth-child(3)').each(function(){                
        var $this = $(this);
        $this.clone().attr('colspan', 3)
             .wrap('<tr></tr>').parent().insertAfter($this.parent());
        $this.text('View');
    })
})

This clones the original td, wraps it in a tr and inserts it after the parent of the original td. Finally, it resets the text to View

Doug Neiner
This is script does not work. I suggest using $("<td><tr>" + $this.text() + "</tr></td>").attr("colspan", 3).insertAfter($this.parent()); This should fix the $this.clone()... line (Your code is having difficulty accessing it's parentNode)
MillsJROSS
I tested before posting. What browser is it having difficulty in?
Doug Neiner
Thanks... works like a charm.. But only one issue which i face is the table alignment in IE 6 and 7, the colspan='4' is not rendered properly as the first row gets extended due to the content.. when i copy the rendered code using firebug and see the preview, then i dont see a problem with the alignment.. any idea as to why it happens....
Sullan
Have you added an extra column? You only have three in your example so I used `3`. I would try adding the second parameter in quotes. So `attr('colspan', '3')` instead of without, but I don't know that it would make a difference.
Doug Neiner
i have 4 columns, so i changed the colspan val to 3, but still i am facing the same problem in IE...
Sullan