1

I have a table which has been rendered in my view as shown below:

<table border="1", id="pretty" class="tablesorter">
    <thead>
    <tr>
        <th>LeaveID</th>
        <th>Student ID</th>
        <th>Leave Start Date</th>
        <th>Leave End Date</th>
        <th>Leave Reason</th>
        <th>Leave Duration</th>
        <th>Academic Response</th>
        <th>Warden Response</th>
        <th>Leave Status</th>
    </tr>
    </thead>
    <% @leaves.each do |leave| %>
    <tbody>
    <tr>
        <td><%= link_to leave.leave_id_prefix+(leave.leave_id).to_s()+'/'+leave.academic_session, generateReport_path(:id_param => leave.student_id) %><br></td>
        <td><%= leave.student_id %></td>
        <td><%= leave.leave_from %><br></td>
        <td><%= leave.leave_to %><br></td>
        <td><%= leave.leave_reason %><br></td>
        <td><%= leave.leave_duration %></td>
        <td><%= leave.academic_status %></td>
        <td><%= leave.warden_status %></td>
        <td><%= leave.status%></td>
    </tr>
    </tbody>
    <% end %>

</table>

and JavaScript in the View is as shown below to make the rendered table a dataTable.

    <script>
      $(document).ready(function(){
          $("#pretty").dataTable();
      });
    </script>

I have followed the Ryan Bates' Railcasts http://railscasts.com/episodes/340-datatables?view=asciicast for this purpose.

I have also downloaded the required js and css files to my app.

But I always get this error of

    TypeError: Object [object Object] has no method 'dataTable' 

How should I go about it.. Any insight will be welcomed..!

3
  • make sure jquery is loaded first and then your datatable js... Commented Mar 8, 2013 at 10:59
  • thanks @bipen but it seems dataTable is not working properly.. I am not able to sort the data or search through data.. Do i have to add something into dataTable function.? Commented Mar 8, 2013 at 11:14
  • @ph3n0m_aks have you found any solution to your problem? I am getting the same error as you. Commented Apr 2, 2014 at 15:51

4 Answers 4

1

Make sure your Jquery files is above and called before the following code

<script>
   $(document).ready(function(){
       $("#pretty").dataTable();
   });
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

I am loading jquery-1.9.0.js before jquery.dataTables.js (1.1 beta) still getting this same error.
0

UPDATE:

Just found the error in your code:

Replace

<table border="1", id="pretty" class="tablesorter">

with

<table id="pretty" border="1" class="tablesorter">

2 Comments

i am using 1.9.2, and it's working fine.. But I am not able to incorporate the sort, searching capability of dataTables onto my table.. any ideas there??
it's still not working.. I am not able to paginate or sort or filter any data from my datatable. I dunno where I am going wrong..!
0

make sure you load your jquery script first and the dataTables js script later...

for sorting

Using the aaSorting initialization parameter, you can get the table exactly how you want to present the information.

 $('#example').dataTable( {
    "aaSorting": [[ 4, "desc" ]]
});

you can go thorugh the documentation here

Comments

0

your <tbody> tag should be out of loop, this creates of <tbody> for each <tr>, In this case dataTable will not work.

<tbody>
  <% @leaves.each do |leave| %>
    <tr>
      <td><%= ..... %></td>
      <td><%= ..... %></td>
      <td><%= ..... %></td>
      <td><%= ..... %></td>
      <td><%= ..... %></td>
    </tr>
  <% end %>
</tbody>

Comments

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.