Javascript not working/Server-side processing/Rails app
Javascript not working/Server-side processing/Rails app
Hi,
I am new in datatable and I am would like to insert a datatable with serve-side processing and some other features.
I followed this tutorial : http://railscasts.com/episodes/340-datatables?autoplay=true in order to insert my datatable in my app and it worked.
Here is my assets/datatables/addresses_datatable.rb file:
[code] class AddressesDatatable
delegate :params, :h, :link_to, :number_to_currency, to: :@view
def initialize(view)
@view = view
end
def as_json(options = {})
{
sEcho: params[:sEcho].to_i,
iTotalRecords: addresses.count,
iTotalDisplayRecords: addresses.total_entries,
aaData: data
}
end
private
def data
addresses.map do |address|
[
h(address.user_id),
h(address.first_name),
]
end
end
def addresses
@addresses ||= fetch_addresses
end
def fetch_addresses
addresses = Address.order("#{sort_column}").page(page).per_page(per_page)
if params[:sSearch].present?
addresses = addresses.where("user_id like first_name like :search", search: "%#{params[:sSearch]}%")
end
addresses
end
def page
params[:iDisplayStart].to_i/per_page + 1
end
def per_page
params[:iDisplayLength].to_i > 0 ? params[:iDisplayLength].to_i : 10
end
def sort_column
columns = %w[user_id first_name]
columns[params[:iSortCol_0].to_i]
end
end
[/code]
Here is my app/views/addresses/index.html.erb file:
[code] Resender's addresses
Resender id
first anme
[/code]
and here is my asset/javascript/addresses.js file :
[code]
$(document).ready(function() {
var oTable = $('#addresses').dataTable({
sPaginationType: "full_numbers",
bSortClasses: false,
bProcessing: true,
bServerSide: true,
sAjaxSource: $('#addresses').data('source')
});
} );
[/code]
and here is my addresses_controller.rb
[code]
class AddressesController < ApplicationController
def index
if signed_in?
respond_to do |format|
format.html
format.json { render json: AddressesDatatable.new(view_context) }
end
end
end
end
[/code]
When I try to add some additional features to my datatable like in this example : https://datatables.net/release-datatable/examples/advanced_init/events_post_init.html, my code doesn't work :
[code]
$(document).ready(function() {
$('#addresses tbody tr').each( function() {
var sTitle;
var nTds = $('td', this);
var sBrowser = $(nTds[0]).text();
sTitle = sBrowser+' this is the user s id';
this.setAttribute( 'title', sTitle );
} );
var oTable = $('#addresses').dataTable({
sPaginationType: "full_numbers",
bSortClasses: false,
bProcessing: true,
bServerSide: true,
sAjaxSource: $('#addresses').data('source')
});
oTable.$('tr').tooltip( {
"delay": 0,
"track": true,
"fade": 250
} );
} );
[/code]
I've tried many different examples and each time the only thing I managed to do is to display the simple datatable. Is that because of the server side behaviour of my datatable?
Thank you for your help.
Clément
I am new in datatable and I am would like to insert a datatable with serve-side processing and some other features.
I followed this tutorial : http://railscasts.com/episodes/340-datatables?autoplay=true in order to insert my datatable in my app and it worked.
Here is my assets/datatables/addresses_datatable.rb file:
[code] class AddressesDatatable
delegate :params, :h, :link_to, :number_to_currency, to: :@view
def initialize(view)
@view = view
end
def as_json(options = {})
{
sEcho: params[:sEcho].to_i,
iTotalRecords: addresses.count,
iTotalDisplayRecords: addresses.total_entries,
aaData: data
}
end
private
def data
addresses.map do |address|
[
h(address.user_id),
h(address.first_name),
]
end
end
def addresses
@addresses ||= fetch_addresses
end
def fetch_addresses
addresses = Address.order("#{sort_column}").page(page).per_page(per_page)
if params[:sSearch].present?
addresses = addresses.where("user_id like first_name like :search", search: "%#{params[:sSearch]}%")
end
addresses
end
def page
params[:iDisplayStart].to_i/per_page + 1
end
def per_page
params[:iDisplayLength].to_i > 0 ? params[:iDisplayLength].to_i : 10
end
def sort_column
columns = %w[user_id first_name]
columns[params[:iSortCol_0].to_i]
end
end
[/code]
Here is my app/views/addresses/index.html.erb file:
[code] Resender's addresses
Resender id
first anme
[/code]
and here is my asset/javascript/addresses.js file :
[code]
$(document).ready(function() {
var oTable = $('#addresses').dataTable({
sPaginationType: "full_numbers",
bSortClasses: false,
bProcessing: true,
bServerSide: true,
sAjaxSource: $('#addresses').data('source')
});
} );
[/code]
and here is my addresses_controller.rb
[code]
class AddressesController < ApplicationController
def index
if signed_in?
respond_to do |format|
format.html
format.json { render json: AddressesDatatable.new(view_context) }
end
end
end
end
[/code]
When I try to add some additional features to my datatable like in this example : https://datatables.net/release-datatable/examples/advanced_init/events_post_init.html, my code doesn't work :
[code]
$(document).ready(function() {
$('#addresses tbody tr').each( function() {
var sTitle;
var nTds = $('td', this);
var sBrowser = $(nTds[0]).text();
sTitle = sBrowser+' this is the user s id';
this.setAttribute( 'title', sTitle );
} );
var oTable = $('#addresses').dataTable({
sPaginationType: "full_numbers",
bSortClasses: false,
bProcessing: true,
bServerSide: true,
sAjaxSource: $('#addresses').data('source')
});
oTable.$('tr').tooltip( {
"delay": 0,
"track": true,
"fade": 250
} );
} );
[/code]
I've tried many different examples and each time the only thing I managed to do is to display the simple datatable. Is that because of the server side behaviour of my datatable?
Thank you for your help.
Clément
This discussion has been closed.