首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jquery-datatables rails不排序

jquery-datatables rails不排序
EN

Stack Overflow用户
提问于 2015-01-14 02:24:53
回答 1查看 2K关注 0票数 0

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

宝石

代码语言:javascript
复制
gem 'jquery-datatables-rails'
gem 'will_paginate'

控制器

代码语言:javascript
复制
  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

数据表

代码语言:javascript
复制
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

视图

代码语言:javascript
复制
%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,
    });
  });
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-16 15:54:18

在数据表文件中,更新sort_column以包括要排序的列。

代码语言:javascript
复制
  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,它是唯一排序

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27934883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档