Fixed ajax datatable

This commit is contained in:
Ilion Beyst 2015-09-08 17:30:13 +02:00
parent e3a7996591
commit c3c13f428a
5 changed files with 45 additions and 11 deletions

View file

@ -27,7 +27,9 @@ gem "haml-rails", "~> 0.9"
# Pure for css # Pure for css
gem 'purecss-rails' gem 'purecss-rails'
# Use datatables # Use datatables
gem 'jquery-datatables-rails', '~> 3.3.0' gem 'jquery-datatables-rails'
gem 'ajax-datatables-rails'
# Use Select2 for selecting users # Use Select2 for selecting users
gem 'select2-rails' gem 'select2-rails'

View file

@ -36,6 +36,8 @@ GEM
minitest (~> 5.1) minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4) thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1) tzinfo (~> 1.1)
ajax-datatables-rails (0.3.1)
railties (>= 3.1)
annotate (2.6.10) annotate (2.6.10)
activerecord (>= 3.2, <= 4.3) activerecord (>= 3.2, <= 4.3)
rake (~> 10.4) rake (~> 10.4)
@ -249,6 +251,7 @@ PLATFORMS
ruby ruby
DEPENDENCIES DEPENDENCIES
ajax-datatables-rails
annotate annotate
byebug byebug
cancancan cancancan
@ -263,7 +266,7 @@ DEPENDENCIES
haml-rails (~> 0.9) haml-rails (~> 0.9)
high_voltage (~> 2.4.0) high_voltage (~> 2.4.0)
jbuilder (~> 2.0) jbuilder (~> 2.0)
jquery-datatables-rails (~> 3.3.0) jquery-datatables-rails
jquery-rails jquery-rails
mysql2 mysql2
omniauth-oauth2 omniauth-oauth2

View file

@ -1,7 +1,10 @@
class TransactionsController < ApplicationController class TransactionsController < ApplicationController
def index def index
@transactions = Transaction.all respond_to do |format|
format.html
format.json { render json: TransactionDatatable.new(view_context) }
end
end end
def new def new

View file

@ -0,0 +1,26 @@
class TransactionDatatable < AjaxDatatablesRails::Base
def sortable_columns
# Declare strings in this format: ModelName.column_name
@sortable_columns ||= []
end
def searchable_columns
# Declare strings in this format: ModelName.column_name
@searchable_columns ||= []
end
private
def data
records.map do |record|
[
record.id, record.debtor.name, record.creditor.name, record.amount
]
end
end
def get_raw_records
Transaction.all.eager_load(:debtor, :creditor)
end
end

View file

@ -1,4 +1,4 @@
%table#transactions.display %table#transactions.display{data: { source: transactions_path }}
%thead %thead
%tr %tr
%th id %th id
@ -6,13 +6,13 @@
%th creditor %th creditor
%th amount %th amount
%tbody %tbody
- @transactions.each do |transaction|
%tr
%td= transaction.id
%td= transaction.debtor.name
%td= transaction.creditor.name
%td= transaction.amount
:javascript :javascript
$(document).ready(function() { $(document).ready(function() {
$('#transactions').DataTable(); $('#transactions').DataTable({
processing: true,
serverSide: true,
ajaxSource: $('#transactions').data('source'),
pagingType: 'full_numbers'
});
}); });