write my own filtering form and parse with an object

This commit is contained in:
Felix Van der Jeugt 2015-09-09 17:14:06 +02:00
parent 028f52ecde
commit 747d014b85
2 changed files with 56 additions and 17 deletions

View file

@ -3,12 +3,32 @@ module DataTable
extend ActiveSupport::Concern extend ActiveSupport::Concern
def apply_filter(user, params) def apply_filter(user, params)
p selection_to_json(user, user.transactions) p AjaxRequest.new(params)
selection_to_json(user, user.transactions) selection_to_json(user, user.transactions)
end end
private private
class AjaxRequest
def initialize(params)
# Parsing according to https://datatables.net/manual/server-side
@draw = params.require(:draw).to_i
@start = params.require(:start).to_i
@length = params.require(:length).to_i
@columns = Hash.new
params.require(:columns).each do |i, column|
@columns[column.require(:data).to_sym] = {
name: column[:name],
searchable: column[:searchable] == 'true',
orderable: column[:orderable] == 'true',
search_value: column.require(:search)[:value]
}
end
end
end
def selection_to_json(user, selection) def selection_to_json(user, selection)
{ data: selection.map { |transaction| { { data: selection.map { |transaction| {
amount: transaction.signed_amount_for(user), amount: transaction.signed_amount_for(user),

View file

@ -1,23 +1,32 @@
%h2= @user.name %h2= @user.name
%table#transactions.display{data: { source: user_path(@user) }} %table#transactions.display{data: { source: user_path(@user) }}
%thead %thead
%tr
%td Amount
%td Origin
%td Message
%td Peer
%td Time
%tr %tr
%th Amount %th Amount
%th Origin %th Origin
%th Message %th Message
%th Peer %th Peer
%th Time %th Time
%tr
%td.bound.input-listen
%input.lower-bound{type: 'number', placeholder: 'lower', class: 'pure-u-1-5'}
= "<= Amount <="
%input.upper-bound{type: 'number', placeholder: 'upper', class: 'pure-u-1-5'}
%td.input-listen
%input{type: 'text', placeholder: 'Filter on Origin'}
%td.input-listen
%input{type: 'text', placeholder: 'Filter on Message'}
%td.input-listen
%input{type: 'text', placeholder: 'Filter on Peer'}
%td.bound.input-listen
%input.lower-bound{type: 'date', placeholder: 'after'}
= "<= Time <="
%input.upper-bound{type: 'date', placeholder: 'before'}
%tbody %tbody
:javascript :javascript
$(document).ready(function() { $(document).ready(function() {
$('#transactions').dataTable({ var table = $('#transactions').DataTable({
processing: true, processing: true,
serverSide: true, serverSide: true,
searching: true, searching: true,
@ -31,14 +40,24 @@
{ data: 'peer', name: 'Peer' }, { data: 'peer', name: 'Peer' },
{ data: 'time', name: 'Time' } { data: 'time', name: 'Time' }
] ]
}).columnFilter({ });
sPlaceHolder: 'head:before',
aoColumns: [ var tds = $('.input-listen');
{ type: 'number-range' }, var i = 0;
{ type: 'text' }, table.columns().every(function() {
{ type: 'text' }, var column = this
{ type: 'text' }, tds.eq(i).find('input').on('keyup change', function() {
{ type: 'date-range' } var value = null;
] var td = $(this).parent();
if(td.hasClass('bound')) {
value = td.find('.lower-bound').val() + '~' + td.find('.upper-bound').val();
} else {
value = $(this).val()
}
if(column.search() !== value) {
column.search(value).draw();
}
});
i = i + 1;
}); });
}); });