jquery-datatables-rails 未排序

jquery-datatables-rails not sorting

我在设置数据表的默认排序时遇到问题

宝石

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,
    });
  });

在数据表文件中更新 sort_column 以包含您要排序的列

  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,它是唯一可以排序的