Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I want sort a column without reload the page, I'm trying to use Ajax but it's not working.

welcome_controller.rb

def index
  @keyword = params[:keyword]
  @category = params[:category]

  @search = HrCurriculumIntern.search(params[:search])
  @hr_curriculum_interns = @search.all

  respond_to do |format|
    format.js
    format.html
  end
end

views/welcome/index.html.erb

<% title %>

<div class="container">
  <ul class="tabs" data-tabs data-toggle="tab">
<li class="active"><a href="#main"><%= t('labels.main') %></a></li>
<li><a href="#interns"><%= t('labels.interns') %></a></li>
<li><a href="#analysts"><%= t('labels.analysts') %></a></li>
</ul>  

<div class="tab-content"> 
 <div id="main" class="active">
  <%= render "search" %>
</div>

<div id="interns" >
  <%#= render "hr_curriculum_interns/hr_curriculum_interns_filter" %>
  <%= render "hr_curriculum_interns/table_hr_curriculum_interns" %>
</div>

<div id="analysts" >
  <%= render "hr_curriculum_systems_analysts/table_hr_curriculum_systems_analysts" %>
</div>

In views/welcome/index.js.erb

$("#table_hr_curriculum_interns").html("<%=escape_javascript(render("hr_curriculum_interns/table_hr_curriculum_interns")) %>");

views/hr_curriculum_interns/_table_hr_curriculum_interns.html.erb

<%= stylesheet_link_tag "bootstrap" %>
<%= javascript_include_tag "bootstrap-tabs" %>

 <div class="container">
 <%= new_button HrCurriculumIntern.new, [:new, :hr_curriculum_intern] %>
   <table class="zebra-striped">
      <thead>
        <tr>
    <th><%= sort_link @search, :date_interview, t('attributes.date_interview') %></th>
    <th><%= sort_link @search, :name, t('attributes.name') %></th>
    <th><%= sort_link @search, :college, t('columns.college') %></th>
    <th><%= sort_link @search, :major, t('columns.major') %></th>      
    <th><%= sort_link @search, :answer_sent, t('columns.answer_sent') %></th>
    <th></th>
    <th></th>
    <th></th>
  </tr>
</thead>
<tbody>
  <%# @hr_curriculum_interns = HrCurriculumIntern.all %>
  <% @hr_curriculum_interns.each do |hr_curriculum_intern| %>
    <tr>
      <td><%= display_date(hr_curriculum_intern.date_interview) %></td>
      <td><%= smart_truncate(hr_curriculum_intern.name) %></td>     
      <td><%= smart_truncate(hr_curriculum_intern.college) %></td>
      <td><%= smart_truncate(hr_curriculum_intern.major) %></td>
      <td><%= hr_curriculum_intern.answer_sent %></td>
      <td><%= show_icon hr_curriculum_intern, hr_curriculum_intern, "data-default-action" => true %></td>
      <td> <%= edit_icon hr_curriculum_intern, edit_hr_curriculum_intern_path(hr_curriculum_intern) %></td>
      <td><%= link_to image_tag('icons/destroy.png'), hr_curriculum_intern, method: :delete, data: { confirm: t('messages.are_you_sure') } %></td>
    </tr>
  <% end %>
</tbody>

It's not workinks, when I clicked in any column name, the page reloads. What's wrong?

share|improve this question
    
open in Chrome Network (xhr) tab and see response from server. What do you see there? – Igor Kasyanchuk Jan 16 '14 at 16:53
    
I never used this :PP, very cool, Is it: Request URL:0.0.0.0:3000/?search[meta_sort]=date_interview.asc Request Method:GET ? – lorena Jan 16 '14 at 17:05
    
Share sort_link helper method snippet – Amit Patel Jan 29 '14 at 5:09

1 Answer 1

Why do you need to perform an AJAX request simply to re-order the data? Why not just sort it and redisplay it using client-side JS?

There are a couple of Railscasts which will show you how to do this:

http://railscasts.com/episodes/340-datatables

http://railscasts.com/episodes/228-sortable-table-columns

share|improve this answer
    
I use the tutorial 228-sortable-table-columns \o/, I will tryng using the 340 too. o/ – lorena Jan 16 '14 at 16:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.