Skip to content

Pagination not working with will_paginate 3.0.5 on 2.1.10.0.2 #119

@sushant-personal

Description

@sushant-personal

Hi

I have following datatable.js file:

// Call datatables, and return the API to the variable for use in our code
// Binds datatables to all elements with a class of datatable

var dtable = $('#usersDatatable').dataTable({
    "sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
    "sPaginationType": "bootstrap",
    "bProcessing": true,
    "bServerSide": true,
    "bDeferRender": true,
    "sAjaxSource": $('#usersDatatable').data('source'),
    //"ajax": "../data/userarrays.txt",
    "aoColumns": [
        null,
        null,
        { "bSortable": false },
        { "bSortable": false }
    ]
});

var dtable_api = dtable.api();

// Grab the datatables input box and alter how it is bound to events
$(".dataTables_filter input")
    .unbind() // Unbind previous default bindings
    .bind("keypress keyup input", function(e) { // Bind our desired behavior
        // If the length is 3 or more characters, or the user pressed ENTER, search
        if(e.keyCode == 13 || e.keyCode == 36) {
            // Call the API search function
            dtable_api.search(this.value).draw();
        }
        // Ensure we clear the search if they backspace far enough
        // Sushant: commented for now
        /*if(this.value == "") {
            dtable_users_api.search("").draw();
        }*/
        return;
    });

Following is my datatable.rb file:

class UserDatatable

  delegate :params, :link_to, :user_path, :form_for, :concat, :content_tag, :current_user, to: :@view

  def initialize(view)
    @view = view
  end

  def as_json(options = {})
    {
        sEcho: params[:sEcho].to_i,
        iTotalRecords: User.all.count,
        iTotalDisplayRecords: users.total_entries,
        aaData: data
    }
  end

  private

  def users
    @users ||= fetch_users
  end

  def fetch_users
    users = User.all.order("#{sort_column} #{sort_direction}")
    users = users.page(page).per_page(per_page)
    if params[:sSearch].present?
      users = User.all.where("name like :search", search: "%#{params[:sSearch]}%")
    end
    users
  end

  def data
    users.map do |record|
      [
        record.name,
        link_to(record.email, user_path(record)),
        role_form(record),
        delete_button(record)
      # other attributes
        # comma separated list of the values for each cell of a table row
        # example: record.attribute,
      ]
    end
  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[name email]
    columns[params[:iSortCol_0].to_i]
  end

  def sort_direction
    params[:sSortDir_0] == "desc" ? "desc" : "asc"
  end

  # ==== Insert 'presenter'-like methods below if necessary
  def role_form(instance)

  end

  def delete_button(instance)
  end
end

The default iDisplayLength is set to 10. I have 12 records in DB. It displays correctly saying:
Showing 1 TO 10 of 12 entries
and
Previous<1><2>Next for pages

But the url formed for Page 2 (and Next) is:
<urlToView>/#

When I click on the Next (or Page 2) there is no trip back to the server, the table keeps displaying the first 10 entries, only the
Showing 1 TO 10 of 12 entries

changes to:
Showing 11 TO 12 of 12 entries

I am not getting any server side error or js error. I am on Rails 4.1.0. What am I missing?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions