views:

362

answers:

2

I am using Ruby on Rails and have a table that I am trying to sort. The tablesorter jquery plugin and it is properly loaded in my files. I have jquery called in front of it at well. I have this code in my javascript.

$(document).ready(function()
{
    $("#myTable").tablesorter({widgets: ['zebra']});
    $("#business").tablesorter({sortList: [[0,0], [1,0]]});

}

);

I have 2 tables. I have my table which is static just to see if the code was working right. And it works just find. I can sort by clicking on any of the headers.

    <table id="myTable">
<thead>
<tr>
    <th>Last Name</th>
    <th>First Name</th>
    <th>Email</th>
    <th>Due</th>
    <th>Web Site</th>
</tr>
</thead>
<tbody>
<tr>
    <td>Smith</td>
    <td>John</td>
    <td>[email protected]</td>
    <td>$50.00</td>
    <td>http://www.jsmith.com&lt;/td&gt;
</tr>
<tr>
    <td>Bach</td>
    <td>Frank</td>
    <td>[email protected]</td>
    <td>$50.00</td>
    <td>http://www.frank.com&lt;/td&gt;
</tr>
<tr>
    <td>Doe</td>
    <td>Jason</td>
    <td>[email protected]</td>
    <td>$100.00</td>
    <td>http://www.jdoe.com&lt;/td&gt;
</tr>
<tr>
    <td>Conway</td>
    <td>Tim</td>
    <td>[email protected]</td>
    <td>$50.00</td>
    <td>http://www.timconway.com&lt;/td&gt;
</tr>
</tbody>
</table>

But I also have my other table which is pulling dynamically from a database. Which doesn't work at all and does nothing at all when I click on the headers.

 <table width="650" cellpadding="6" cellspacing="0" id="business">
        <thead>
        <tr>
          <th>Business Name</th>
          <th>Address</th>
          <th>Category</th>
          <th>Description</th>
        </tr>
        </thead>
  <% @businesses.each do |business|if !business.approved %>
      <tbody>
        <tr>
          <td><a class="Contact<%=h business.id %>" href="#"><%=h business.name %></a></td>
          <td><%=h business.address %></td>
          <td><%=h business.business_category.name %></td>
          <td><%=h business.description %></td>

        </tr>
      </tbody>
      <% end %>
    <% end %>
    </table>

Any Help would be great.

A: 

I assume you are making an ajax call to request the dynamic table with an id of business? If so you need to call the tablesorter method when the table has been added to the dom, typically within the success method of .ajax. If you could show how you are requesting & inserting the business table I can provide help you futher.

redsquare
Some of my code didn't show up. Is that what you needed?
Mike
A: 

Not sure if the repeated TBODY tags in the output of the problematic table are confusing the tablesorter.

thenobot