meta_search + datatables
meta_search + datatables
innocent_rifle
Posts: 1Questions: 0Answers: 0
I'm using datatables in my rails 3.1 application. Datatables is set up to work server side. I'm using ajax requests to get data from server and then display it in the table.
I use meta_search as a filter to get data from the database.
Here is the code in my view:
[code]
= simple_form_for @search, :url => offer_coupons_path(@offer), :html => {:method => :get} do |f|
= f.select :redeemed_equals, [['All Coupons', '']] + [["Redeemed", true], ["Not Redeemed", false]]
[/code]
Here is the method in the controller:
[code]
def offer_coupons_list
search_params = params[:search] || {"meta_sort"=>"user_full_name.asc"}
@search = Coupon.search(search_params)
@coupons = @search.includes(:user).includes(:order => :gift).page(@page)
render :partial => 'coupons/offer_coupons_list'
end
[/code]
And here is javascript responsible for sending ajax request to the server:
[code]
$("#search_redeemed_equals").change(function(e){
table = $("#grid").dataTable();
var data = $("#search_redeemed_equals").parents("form:first").serialize();
$.ajax({
'url': 'coupons/offer_coupons_list',
'data': data,
'type': 'POST'
});
table.fnDraw();
e.preventDefault();
});
[/code]
The problem is that the script sends two ajax requests to the server. The first request is correct and returns filtered data. Another request is sent when fnDraw() is invoked and returns not filtered data. So the table is not refreshed. Request is sent each time any of datatables functions is invoked. How can i solve the problem?
I use meta_search as a filter to get data from the database.
Here is the code in my view:
[code]
= simple_form_for @search, :url => offer_coupons_path(@offer), :html => {:method => :get} do |f|
= f.select :redeemed_equals, [['All Coupons', '']] + [["Redeemed", true], ["Not Redeemed", false]]
[/code]
Here is the method in the controller:
[code]
def offer_coupons_list
search_params = params[:search] || {"meta_sort"=>"user_full_name.asc"}
@search = Coupon.search(search_params)
@coupons = @search.includes(:user).includes(:order => :gift).page(@page)
render :partial => 'coupons/offer_coupons_list'
end
[/code]
And here is javascript responsible for sending ajax request to the server:
[code]
$("#search_redeemed_equals").change(function(e){
table = $("#grid").dataTable();
var data = $("#search_redeemed_equals").parents("form:first").serialize();
$.ajax({
'url': 'coupons/offer_coupons_list',
'data': data,
'type': 'POST'
});
table.fnDraw();
e.preventDefault();
});
[/code]
The problem is that the script sends two ajax requests to the server. The first request is correct and returns filtered data. Another request is sent when fnDraw() is invoked and returns not filtered data. So the table is not refreshed. Request is sent each time any of datatables functions is invoked. How can i solve the problem?
This discussion has been closed.