1

I want to create a table displaying db information using ejs and nodes.js.

<table class="table table-striped table-hover" id="loaded-table">
    <thead>
        <tr>
            <th>First</th>
            <th>Second</th>
            <th>Third</th>
            <th>Fourth</th>
            <th>Fifth</th>
        </tr>
    </thead>
    <tbody>
    <% for(var i=0; i < data.length; i++) { %>
        <tr>
            <td> <%= data[i].prsnl_firstName %> </td>
            <td> <%= data[i].prsnl_lastName %> </td>
            <td> <%= data[i].prsnl_city %> </td>
            <td> <%= data[i].prsnl_email %> </td>
            <td> <%= data[i].prsnl_age %> </td>
        </tr>
    <% } %>
    </tbody>
</table>

But I also want it to be sortable. Like in wikipedia, where you click on one of the th and the table is sorted by that column. The problem is, trying to use a jquery plugin doesn't work because the table is literally none existent at the moment of executing the code. Is there a way to do so?

1 Answer 1

1

Ok, you can use your JQuery plugin (Example http://tablesorter.com/docs/), but you have to make sure you execute the sortable behavior after the table is created, you have to scenarios:

1) The table is created when the page is rendered, for this case you will need run using document on ready

$(document).ready(function(){ 
       $("#myTable").tablesorter(); 
});

2) If your table is created after some trigger, just run $("#myTable").tablesorter(); or equivalent after the trigger.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm sorry, something isn't working. when i do it with a normal table it works but for a generated table, it shows the table but doesn't do anything

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.