jquery datatables rails未排序
我在设置数据表的默认排序时遇到问题 宝石jquery datatables rails未排序,jquery,ruby-on-rails,ruby,datatables,Jquery,Ruby On Rails,Ruby,Datatables,我在设置数据表的默认排序时遇到问题 宝石 gem 'jquery-datatables-rails' gem 'will_paginate' 控制器 def index respond_to do |format| format.html format.csv { send_data clients.to_csv } format.xls format.json { render json: ClientsDatata
gem 'jquery-datatables-rails'
gem 'will_paginate'
控制器
def index
respond_to do |format|
format.html
format.csv { send_data clients.to_csv }
format.xls
format.json {
render json: ClientsDatatable.new(view_context)
}
end
end
数据表
class ClientsDatatable
include Rails.application.routes.url_helpers
delegate :params, :h, :link_to, :mail_to, :number_to_phone, to: :@view
def initialize(view)
@view = view
end
def as_json(options = {})
{
sEcho: params[:sEcho].to_i,
iTotalRecords: Client.count,
iTotalDisplayRecords: clients.total_entries,
aaData: data
}
end
private
def data
clients.map do |client|
[
client.id.to_s + " " +
link_to('+ Equipment', new_users_equipment_path(:client_id => client), class: 'btn btn-default') +
link_to('+ Notes', users_add_notes_path(client), class: 'btn btn-default') +
link_to('+ Quote', new_users_client_document_path(client), class: 'btn btn-default'),
client.full_name.titleize,
client.company_name.titleize,
client.state,
mail_to(client.email),
client.years_in_business,
number_to_phone(client.cell),
number_to_phone(client.work_phone),
client.equipment_desired,
client.currently_using,
client.created_by_user.name,
client.created_at.in_time_zone('Eastern Time (US & Canada)').strftime('%D %r'),
link_to('Edit', edit_users_client_path(client), class: 'btn btn-info') +
link_to('Show', users_client_path(client), class: 'btn btn-success'),
]
end
end
def clients
@clients ||= fetch_clients
end
def fetch_clients
if params[:has_filterd] == 'true'
clients = User.find(params[:current_user]).created_clients.order("#{sort_column} #{sort_direction}")
else
clients = Client.order("#{sort_column} #{sort_direction}")
end
clients = clients.page(page).per_page(per_page)
if params[:sSearch].present?
clients = clients.where("
lower(first_name) like :search or
lower(last_name) like :search or
lower(company_name) like :search or
lower(state) like :search or
lower(email) like :search or
lower(cell) like :search or
lower(work_phone) like :search or
lower(equipment_desired) like :search",
search: "%#{params[:sSearch].downcase}%"
)
end
clients
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[id]
columns[params[:iSortCol_0].to_i]
end
def sort_direction
params[:sSortDir_0] == "asc" ? "asc" : "desc"
end
end
%table#datatable.display{"data-source" => users_clients_url(format: "json", has_filterd: current_user.has_filterd, current_user: current_user, sSortDir_0: 'desc')}
%thead
%tr
%th
%th= t(:name)
%th= t(:company_name)
%th= t(:state)
%th= t(:email)
%th= t(:years_in_business)
%th= t(:cell)
%th= t(:work_phone)
%th= t(:equipment)
%th= t(:currently_using)
%th= t(:created_by)
%th= t(:created_at)
%th
%tbody
:javascript
$(document).ready(function(){
console.log('moo')
$('#datatable').DataTable({
bServerSide: true,
sPaginationType: "full_numbers",
sAjaxSource: $('#datatable').data("source"),
bSortable: false,
});
});
视图
class ClientsDatatable
include Rails.application.routes.url_helpers
delegate :params, :h, :link_to, :mail_to, :number_to_phone, to: :@view
def initialize(view)
@view = view
end
def as_json(options = {})
{
sEcho: params[:sEcho].to_i,
iTotalRecords: Client.count,
iTotalDisplayRecords: clients.total_entries,
aaData: data
}
end
private
def data
clients.map do |client|
[
client.id.to_s + " " +
link_to('+ Equipment', new_users_equipment_path(:client_id => client), class: 'btn btn-default') +
link_to('+ Notes', users_add_notes_path(client), class: 'btn btn-default') +
link_to('+ Quote', new_users_client_document_path(client), class: 'btn btn-default'),
client.full_name.titleize,
client.company_name.titleize,
client.state,
mail_to(client.email),
client.years_in_business,
number_to_phone(client.cell),
number_to_phone(client.work_phone),
client.equipment_desired,
client.currently_using,
client.created_by_user.name,
client.created_at.in_time_zone('Eastern Time (US & Canada)').strftime('%D %r'),
link_to('Edit', edit_users_client_path(client), class: 'btn btn-info') +
link_to('Show', users_client_path(client), class: 'btn btn-success'),
]
end
end
def clients
@clients ||= fetch_clients
end
def fetch_clients
if params[:has_filterd] == 'true'
clients = User.find(params[:current_user]).created_clients.order("#{sort_column} #{sort_direction}")
else
clients = Client.order("#{sort_column} #{sort_direction}")
end
clients = clients.page(page).per_page(per_page)
if params[:sSearch].present?
clients = clients.where("
lower(first_name) like :search or
lower(last_name) like :search or
lower(company_name) like :search or
lower(state) like :search or
lower(email) like :search or
lower(cell) like :search or
lower(work_phone) like :search or
lower(equipment_desired) like :search",
search: "%#{params[:sSearch].downcase}%"
)
end
clients
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[id]
columns[params[:iSortCol_0].to_i]
end
def sort_direction
params[:sSortDir_0] == "asc" ? "asc" : "desc"
end
end
%table#datatable.display{"data-source" => users_clients_url(format: "json", has_filterd: current_user.has_filterd, current_user: current_user, sSortDir_0: 'desc')}
%thead
%tr
%th
%th= t(:name)
%th= t(:company_name)
%th= t(:state)
%th= t(:email)
%th= t(:years_in_business)
%th= t(:cell)
%th= t(:work_phone)
%th= t(:equipment)
%th= t(:currently_using)
%th= t(:created_by)
%th= t(:created_at)
%th
%tbody
:javascript
$(document).ready(function(){
console.log('moo')
$('#datatable').DataTable({
bServerSide: true,
sPaginationType: "full_numbers",
sAjaxSource: $('#datatable').data("source"),
bSortable: false,
});
});
在数据表文件中,更新sort_列以包括要排序的列
def sort_column
columns = %w[id first_name company_name state email years_in_business cell work_phone equipment_desired currently_using created_by created_at id]
columns[params[:iSortCol_0].to_i]
end
如果您只有id,它是唯一一个排序的您还没有明确指出问题所在,或者当
bSortable
设置为false时,为什么会出现排序问题。我认为允许用户对每个列进行排序,而不是默认排序。我正在尝试设置“default id desc”,这是由数组、列索引和方向完成的。排序文档很容易阅读嘿,MZaragoza,我正在为我的rails应用程序搜索ajax/jquery排序/搜索解决方案,但我发现的大多数东西似乎都过时了。截至2015年11月,你能告诉我应该使用什么gem/jquery插件/等等吗?你看过https://github.com/rweng/jquery-datatables-rails
和