Skip to content

Commit 8415c52

Browse files
authored
Merge pull request #355 from PavelBezpalov/add_support_for_json_params
Add support for json params
2 parents b6151b9 + b84e3c0 commit 8415c52

File tree

4 files changed

+45
-14
lines changed

4 files changed

+45
-14
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,18 +761,23 @@ then in your views :
761761
then in your Coffee/JS :
762762

763763
```coffee
764+
# send params in form data
764765
$ ->
765766
$('#posts-datatable').dataTable
766767
ajax:
767768
url: $('#posts-datatable').data('source')
768769
type: 'POST'
769770
# ...others options, see [here](#5-wire-up-the-javascript)
770771

772+
# send params as json data
771773
$ ->
772774
$('#users-datatable').dataTable
773775
ajax:
774776
url: $('#users-datatable').data('source')
777+
contentType: 'application/json'
775778
type: 'POST'
779+
data: (d) ->
780+
JSON.stringify d
776781
# ...others options, see [here](#5-wire-up-the-javascript)
777782
```
778783

lib/ajax-datatables-rails/datatable/datatable.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,13 @@ def page
7070
def get_param(param)
7171
return {} if options[param].nil?
7272

73-
options[param].to_unsafe_h.with_indifferent_access
73+
if options[param].is_a? Array
74+
hash = {}
75+
options[param].each_with_index { |value, index| hash[index] = value }
76+
hash
77+
else
78+
options[param].to_unsafe_h.with_indifferent_access
79+
end
7480
end
7581

7682
def db_adapter

spec/ajax-datatables-rails/datatable/datatable_spec.rb

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
describe AjaxDatatablesRails::Datatable::Datatable do
44

55
let(:datatable) { ComplexDatatable.new(sample_params).datatable }
6+
let(:datatable_json) { ComplexDatatable.new(sample_params_json).datatable }
7+
let(:order_option) { {'0'=>{'column'=>'0', 'dir'=>'asc'}, '1'=>{'column'=>'1', 'dir'=>'desc'}} }
8+
let(:order_option_json) { [{'column'=>'0', 'dir'=>'asc'}, {'column'=>'1', 'dir'=>'desc'}] }
69

7-
describe 'order methods' do
8-
let(:order_option) { {'0'=>{'column'=>'0', 'dir'=>'asc'}, '1'=>{'column'=>'1', 'dir'=>'desc'}} }
9-
10+
shared_examples 'order methods' do
1011
it 'should be orderable' do
1112
expect(datatable.orderable?).to eq(true)
1213
end
@@ -36,6 +37,28 @@
3637
end
3738
end
3839

40+
shared_examples 'columns methods' do
41+
it 'should have 4 columns' do
42+
expect(datatable.columns.count).to eq(6)
43+
end
44+
45+
it 'child class' do
46+
expect(datatable.columns.first).to be_a(AjaxDatatablesRails::Datatable::Column)
47+
end
48+
end
49+
50+
describe 'with query params' do
51+
it_behaves_like 'order methods'
52+
it_behaves_like 'columns methods'
53+
end
54+
55+
describe 'with json params' do
56+
let(:order_option) { order_option_json }
57+
let(:datatable) { datatable_json }
58+
it_behaves_like 'order methods'
59+
it_behaves_like 'columns methods'
60+
end
61+
3962
describe 'search methods' do
4063
it 'should be searchable' do
4164
datatable.options[:search][:value] = 'atom'
@@ -52,16 +75,6 @@
5275
end
5376
end
5477

55-
describe 'columns methods' do
56-
it 'should have 4 columns' do
57-
expect(datatable.columns.count).to eq(6)
58-
end
59-
60-
it 'child class' do
61-
expect(datatable.columns.first).to be_a(AjaxDatatablesRails::Datatable::Column)
62-
end
63-
end
64-
6578
describe 'option methods' do
6679
describe '#paginate?' do
6780
it {

spec/support/helpers/params.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ def sample_params
5151
}
5252
)
5353
end
54+
55+
def sample_params_json
56+
hash_params = sample_params.to_unsafe_h
57+
hash_params["columns"] = hash_params["columns"].values
58+
hash_params["order"] = hash_params["order"].values
59+
ActionController::Parameters.new(hash_params)
60+
end
5461
# rubocop:enable Metrics/MethodLength
5562

5663
def nulls_last_sql(datatable)

0 commit comments

Comments
 (0)