OK…
GemFile
gem 'jquery-datatables-rails', github: 'rweng/jquery-datatables-rails'
gem 'ajax-datatables-rails', git: 'git://github.com/antillas21/ajax-datatables-rails.git', branch: 'master'
route.rb
resources :insurances do
resources :groups, controller: 'insurances/groups'
end
insurances/groups_controller.rb
class Insurances::GroupsController < ApplicationController
before_action :authenticate_user!
def index
respond_to do |format|
format.html
format.json{ render json: InsuranceGroupsDatatable.new(view_context, { only_for_current_insurance_id: @insurance.id }) }
end
end
....
end
views/insurances/groups/_index.html.erb
<table id="insurance-groups" class="compact hover order-column cell-border" data-source="<%= insurance_groups_path(@insurance.id, format: :json) %>">
<thead>
<tr>
<th>0 id</th>
<th>1 insurance_id</th>
<th>2 </th>
<th>3 </th>
<th>4 </th>
<th>5 </th>
<th>6 </th>
<th>7 </th>
<th>8 </th>
<th>9 </th>
<th>10 </th>
<th>11 </th>
<th>12 </th>
<th>13 </th>
<th>14 </th>
</tr>
</thead>
<tbody>
<!--data from JSON -->
</tbody>
</table>
assets/javascrips/groups.js.erb
$(document).ready(function() {
var oInsuranceGroupstable = $('#insurance-groups').DataTable({
responsive: true,
processing: true,
serverSide: true,
ajax: $('#insurance-groups').data('source'),
pagingType: "full_numbers",
lengthMenu: [[10, 15, 25, 50, 100, -1], [10, 15, 25, 50, 100, "Wszystkie"]],
"order": [[ 2, "asc" ]],
columns: [
{ "targets": [0],
"title": "ID",
"visible": false,
"searchable": false,
"sortable": false },
{ "targets": [1],
"title": "INSURANCE_ID",
"visible": false,
"searchable": false,
"sortable": false },
{ "targets": [2],
"title": "Nr" },
{ "targets": [3],
"title": "Kwotacja" },
{ "targets": [4],
"title": "System świadczeń" },
{ "targets": [5],
"title": "Zakres" },
{ "targets": [6],
"title": "Grupa ryzyka" },
{ "targets": [7],
"title": "Świadczenie" },
{ "targets": [8],
"title": "Leczenie" },
{ "targets": [9],
"title": "Zasiłek ambulat." },
{ "targets": [10],
"title": "Szpital" },
{ "targets": [11],
"title": "Zawał" },
{ "targets": [12],
"title": "Niezdolność" },
{ "targets": [13],
"title": "Śmierć 100%" },
{ "targets": [14],
"title": "Roczna za osobę" }
],
language: {
decimal: ",",
thousands: " ",
//processing: "<img src='/assets/spinner.gif'>",
processing: "<img src='<%= asset_path('spinner.gif') %>'>",
lengthMenu: "Pokaż _MENU_ pozycji",
info: "Pozycje od _START_ do _END_ z _TOTAL_ łącznie",
infoEmpty: "Pozycji 0 z 0 dostępnych",
infoFiltered: "(filtrowanie spośród _MAX_ dostępnych pozycji)",
infoPostFix: "",
search: "Szukaj :",
loadingRecords: "Wczytywanie...",
zeroRecords: "Nie znaleziono pasujących pozycji",
emptyTable: "Brak danych",
paginate: {
first: "<<",
previous: "<",
next: ">",
last: ">>"
}
}
});
});
i ostatni wg przykładu
datatables/insurance_groups_datatable.rb
class InsuranceGroupsDatatable < AjaxDatatablesRails::Base
# uncomment the appropriate paginator module,
# depending on gems available in your project.
include AjaxDatatablesRails::Extensions::Kaminari
# include AjaxDatatablesRails::Extensions::WillPaginate
# include AjaxDatatablesRails::Extensions::SimplePaginator
def_delegators :@view, :link_to, :h, :mailto
def sortable_columns
# list columns inside the Array in string dot notation.
# Example: 'users.email'
@sortable_columns ||= [
'groups.id',
'groups.insurance_id',
'groups.number',
'groups.quotation',
'groups.tariff_fixed',
'groups.full_range',
'groups.risk_group',
'groups.assurance',
'groups.treatment',
'groups.ambulatory',
'groups.hospital',
'groups.infarct',
'groups.inability',
'groups.death_100_percent',
'groups.sum_after_year'
]
end
def searchable_columns
# list columns inside the Array in string dot notation.
# Example: 'users.email'
@searchable_columns ||= [
'groups.id',
'groups.insurance_id',
'groups.number',
'groups.quotation',
'groups.tariff_fixed',
'groups.full_range',
'groups.risk_group',
'groups.assurance',
'groups.treatment',
'groups.ambulatory',
'groups.hospital',
'groups.infarct',
'groups.inability',
'groups.death_100_percent',
'groups.sum_after_year'
]
end
private
def data
# comma separated list of the values for each cell of a table row
# example: record.attribute,
records.map do |record|
[
record.id,
record.insurance_id,
record.number,
record.quotation_name,
record.tariff_fixed_name,
record.full_range_name,
record.risk_group,
record.assurance,
record.treatment,
record.ambulatory,
record.hospital,
record.infarct,
record.inability,
record.death_100_percent? ? 'Tak' : 'Nie',
record.sum_after_year
]
end
end
def get_raw_records
# insert query here
#Group.all
Group.all.where(insurance_id: options[:only_for_current_insurance_id])
end
# ==== Insert 'presenter'-like methods below if necessary
end