Skip to content

Commit 207c69e

Browse files
committed
Add AjaxDatatablesRails::Base#column_id and AjaxDatatablesRails::Base#column_data methods
These helper methods help to retrieve: * column id from view_columns hash for the named column * column search value for the named column
1 parent 013f85b commit 207c69e

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

lib/ajax-datatables-rails/base.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ def global_search_delimiter
111111
GLOBAL_SEARCH_DELIMITER
112112
end
113113

114+
def column_id(name)
115+
view_columns.keys.index(name.to_sym)
116+
end
117+
118+
def column_data(column)
119+
id = column_id(column)
120+
params.dig('columns', id.to_s, 'search', 'value')
121+
end
122+
114123
def raw_records_error_text
115124
<<-ERROR
116125

spec/ajax-datatables-rails/base_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,25 @@ def paginate_records(records)
222222
expect(datatable.datatable.send(:per_page)).to eq(20)
223223
end
224224
end
225+
226+
describe '#column_id' do
227+
let(:datatable) { ComplexDatatable.new(sample_params) }
228+
229+
it 'should return column id from view_columns hash' do
230+
expect(datatable.send(:column_id, :username)).to eq(0)
231+
expect(datatable.send(:column_id, 'username')).to eq(0)
232+
end
233+
end
234+
235+
describe '#column_data' do
236+
let(:datatable) { ComplexDatatable.new(sample_params) }
237+
before { datatable.params[:columns]['0'][:search][:value] = 'doe' }
238+
239+
it 'should return column data from params' do
240+
expect(datatable.send(:column_data, :username)).to eq('doe')
241+
expect(datatable.send(:column_data, 'username')).to eq('doe')
242+
end
243+
end
225244
end
226245
end
227246
end

0 commit comments

Comments
 (0)