jbox-web / ajax-datatables-rails

A wrapper around DataTable's ajax methods that allow synchronization with server-side pagination in a Rails app
MIT License
590 stars 227 forks source link

the pagination always return the same 10 records #92

Closed zaracato closed 9 years ago

zaracato commented 9 years ago

I use rails (~> 4.0), kaminari (~> 0.15), ajax-datatables-rails (0.3.0) And when in my datatable I push any page, the json is always the same. Could you help me? tks

zaracato commented 9 years ago

By the way, I try with WillPaginate with the same results. tks

antillas21 commented 9 years ago

Can you share your datatable class code?

zaracato commented 9 years ago

yes, off course

class CategoryDatatable < AjaxDatatablesRails::Base AjaxDatatablesRails::Extensions::Kaminari def sortable_columns

Declare strings in this format: ModelName.column_name

@sortable_columns ||= ['categories.name']

end

def searchable_columns

Declare strings in this format: ModelName.column_name

@searchable_columns ||= ['categories.name']

end

private

def data records.map do |record| [

comma separated list of the values for each cell of a table row

    # example: record.attribute,
    record.name
  ]
end

end

def get_raw_records

insert query here

Category.all

end

==== Insert 'presenter'-like methods below if necessary

end

antillas21 commented 9 years ago

Could you please replace the kaminari extension inside the datatable class and use the simple_paginator extension, like this?

class CategoryDatatable < AjaxDatatablesRails::Base
  # replace this:
  # AjaxDatatablesRails::Extensions::Kaminari
  # with this:
  AjaxDatatablesRails::Extensions::SimplePaginator
end

this will bypass using kaminari at all, and send all pagination related methods straight to ActiveRecord.

PS. Please, always format code in comments :smile: , that way it's easier for someone trying to help to read your code.

zaracato commented 9 years ago

there is something that i dont understand (maybe cause Im a noob), this is the get that the datatable make http://localhost:3000/categories.json?sEcho=3&iColumns=1&sColumns=&iDisplayStart=30&iDisplayLength=10&mDataProp_0=0&sSearch_0=&bRegex_0=false&bSearchable_0=true&bSortable_0=true&sSearch=&bRegex=false&iSortCol_0=0&sSortDir_0=asc&iSortingCols=1&_=1426823449762

But In your code I see params[:start], params[:lenght] to get the json, but I didn find where the param change the name to 'iDisplayLength' for example, maybe that is the problem (Im testing with a fork of your gem)

zaracato commented 9 years ago

I actually try with SimplePaginator with the same results , and tks , sorry for the format :P

antillas21 commented 9 years ago

:) Ok, that is an indication that you're using an old version of the jQuery.dataTables plugin.

You need to use jQuery.dataTables version 1.10 if you're using this gem's version 0.3.0 as stated in the README fort this gem: https://github.com/antillas21/ajax-datatables-rails#versions

zaracato commented 9 years ago

AAAAA, it works!! actually I have the 1.10.4 v of datatables but in my gemfile I had a wrong version of your gem, I put gem 'ajax-datatables-rails', '0.1.2' and it works!!, gracias paisa!

antillas21 commented 9 years ago

:) de nada, paisano.

Exanimous commented 9 years ago

I would like to make an update on this same issue. For me, my queries with this gem are always appended with LIMIT 10 OFFSET 0 when serverSide: false.

With serverSide: true it works as expected.

This is with Kaminari, ajax-datatables-rails (= 0.3.1) and jquery-datatables-rails (~> 3.3.0)

This problem occurs with all three paginator types.

antillas21 commented 9 years ago

That is very weird @Exanimous , kaminari or will_paginate modules may behave as you mention, but the SimplePaginator module should just work as it goes straight to the database through the default ORM (activerecord).

Can you share code for your datatables class?

Exanimous commented 9 years ago

Sure thing, here is the code the the current datatable that is proving troublesome.

class SubjectDatatable < AjaxDatatablesRails::Base

  include AjaxDatatablesRails::Extensions::Kaminari
  def_delegators :@view, :link_to, :subject_path, :edit_subject_path, :image_tag, :subject_entries_path, :subject_issues_path, :subject_campaigns_path,
                 :subject_post_path, :organisation_path

  def sortable_columns
    # Declare strings in this format: ModelName.column_name
    @sortable_columns ||=  %w(Subject.last_name Subject.date_of_birth Subject.date_of_death Organisation.name GeneralCategory.name PoliticalCategory.name EconomicCategory.name)
  end

  def searchable_columns
    # Declare strings in this format: ModelName.column_name
    @searchable_columns ||= %w(Subject.first_name Subject.last_name Subject.date_of_birth Subject.date_of_death Organisation.name GeneralCategory.name PoliticalCategory.name EconomicCategory.name)
  end

  private

  def data
    records.map do |record|
      [
        # comma separated list of the values for each cell of a table row
        # example: record.attribute,
        link_to(image_tag(record.image.url(:micro, class: "subject_image")) + " #{ record.full_name }", subject_path(record), remote: true),
        record.date_of_birth,
        record.date_of_death,
        display_organisation_link(record).join(', '),
        record.general_categories.map(&:name).join(', '),
        record.political_categories.map(&:name).join(', '),
        record.economic_categories.map(&:name).join(', '),
        record.aggregated_sentiment,
        record.aggregated_relative_sentiment,
        link_to('Edit', edit_subject_path(record), remote: true, class: "btn btn-primary btn-xs"),
        link_to('Entries', subject_entries_path(record), class: "btn btn-primary btn-xs"),
        link_to('Issues', subject_issues_path(record), class: "btn btn-primary btn-xs"),
        link_to('Campaigns', subject_campaigns_path(record), class: "btn btn-primary btn-xs"),
        link_to('Delete', record, method: :delete, data: { confirm: "Delete #{ record.full_name } ?" }, remote: true, class: "btn btn-danger btn-xs"),
        link_to('Posts', subject_post_path(record), class: "btn btn-primary btn-xs")
      ]
    end
  end

  def get_raw_records
    # insert query here
    Subject.includes(:organisations, :secondary_orgs, :general_categories, :political_categories, :economic_categories).references(:secondary_org, :general_categories, :political_categories, :economic_categories).distinct
    #Subject.includes(:general_categories, org_posts: [:organisation, :secondary_org]).references(:secondary_org, :general_categories).distinct
  end

  def display_organisation_link(record)
    record.all_active_organisations.map.each do |organisation|
      if organisation.present?
        link_to(organisation.name,
                organisation_path(organisation), remote: true)
      else
        ""
      end
    end
  end

  # ==== Insert 'presenter'-like methods below if necessary
end

And the coffeescript:

subjects_table = $('#subjects-table').DataTable
    pageLength: 25
    processing: true
    pagingType: "full_numbers"
    ajax: $('#subjects-table').data('source')
    columnDefs: [ {
      orderable: false,
      targets: [7, 8, 9, 10, 11, 12, 13, 14]
    } ]

A note that all the searching/sorting works perfectly (on both serverside and clientside), but only 10 records will ever be displayed if serverside is disabled.

Exanimous commented 9 years ago

After looking through the lib/ajax-datatables-rails/base.rb code, I decided to manually override the :length parameter to see if it fixed the limited records issue - sure enough it works.

There must be some sort of an issue preventing this value from being set properly when using clientside pagination.

ajax: {
  dataType: 'json',
  url: 'index'
  data: (d) ->
    d.dod_filter = subject_dod_filter
    d.length = 100
    return d
}

All it needed was to have this parameter specified. (Above sets to 100 records)

sahanmalagi commented 8 years ago

I am experiencing the same problem. Is there a resolution(besides the manual override?)

'start' is always Zero no matter which page I attempt to select. I am using ServerSide data and SimplePaginator(Same problem with Kaminari)

<<<<< Controller] -- Index -- Params: {"draw"=>"6", "columns"=>{"0"=>{"data"=>"0", "name"=>"", "searchable"=>"true", "orderable"=>"true", "search"=>{"value"=>"", "regex"=>"false"}}, "1"=>{"data"=>"1", "name"=>"", "searchable"=>"true", "orderable"=>"true", "search"=>{"value"=>"", "regex"=>"false"}}, "2"=>{"data"=>"2", "name"=>"", "searchable"=>"true", "orderable"=>"true", "search"=>{"value"=>"", "regex"=>"false"}}, "3"=>{"data"=>"3", "name"=>"", "searchable"=>"false", "orderable"=>"false", "search"=>{"value"=>"", "regex"=>"false"}}, "4"=>{"data"=>"4", "name"=>"", "searchable"=>"false", "orderable"=>"false", "search"=>{"value"=>"", "regex"=>"false"}}, "5"=>{"data"=>"5", "name"=>"", "searchable"=>"false", "orderable"=>"false", "search"=>{"value"=>"", "regex"=>"false"}}}, "order"=>{"0"=>{"column"=>"2", "dir"=>"desc"}}, "start"=>"0", "length"=>"100", "search"=>{"value"=>"", "regex"=>"false"}, "_"=>"1470615375314", "controller"=>"recipients", "action"=>"index", "format"=>"json"}

I am using version 0.2.1 of this gem & jquery-datatables-rails (3.0.0)

Thanks