From 582238e1bd2af7221904c8da752f9825182797a5 Mon Sep 17 00:00:00 2001 From: Dave Morehouse Date: Wed, 19 Apr 2023 08:03:18 -0500 Subject: [PATCH] Fix server-side out of order ajax responses by returning the passed in draw (counter) parameter. --- lib/ajax-datatables-rails/base.rb | 4 +++- spec/ajax-datatables-rails/base_spec.rb | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/ajax-datatables-rails/base.rb b/lib/ajax-datatables-rails/base.rb index 989f57e..83dc206 100644 --- a/lib/ajax-datatables-rails/base.rb +++ b/lib/ajax-datatables-rails/base.rb @@ -73,11 +73,13 @@ def additional_data # JSON structure sent to jQuery DataTables def as_json(*) + draw_resp = (params[:draw].present?) ? { draw: params[:draw].to_i } : { } + { recordsTotal: records_total_count, recordsFiltered: records_filtered_count, data: sanitize_data(data), - }.merge(additional_data) + }.merge(draw_resp).merge(additional_data) end # User helper methods diff --git a/spec/ajax-datatables-rails/base_spec.rb b/spec/ajax-datatables-rails/base_spec.rb index 02ce8ac..17d3738 100644 --- a/spec/ajax-datatables-rails/base_spec.rb +++ b/spec/ajax-datatables-rails/base_spec.rb @@ -181,6 +181,7 @@ def paginate_records(records) data = datatable.as_json expect(data[:recordsTotal]).to eq 5 expect(data[:recordsFiltered]).to eq 5 + expect(data[:draw]).to eq 1 expect(data[:data]).to be_a(Array) expect(data[:data].size).to eq 5 end @@ -192,6 +193,7 @@ def paginate_records(records) data = datatable.as_json expect(data[:recordsTotal]).to eq 5 expect(data[:recordsFiltered]).to eq 5 + expect(data[:draw]).to eq 1 expect(data[:data]).to be_a(Array) expect(data[:data].size).to eq 5 expect(data[:foo]).to eq 'bar'