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
588 stars 228 forks source link

Error when there are more than 9 columns #185

Closed luis77 closed 6 years ago

luis77 commented 7 years ago

I am inserting data into the datatable, but when I enter more than nine columns I get the following error: DataTables warning: table id=clientes-table - Ajax error. For more information about this error, please see http://datatables.net/tn/7

antillas21 commented 7 years ago

The error code you are receiving and linking to, has nothing to do with the quantity of columns in your datatable... please read the documentation of the url you're linking to in order to diagnose and solve your issue.

On Jan 1, 2017 5:29 PM, "luis77" notifications@github.com wrote:

I am inserting data into the datatable, but when I enter more than nine columns I get the following error: DataTables warning: table id=clientes-table - Ajax error. For more information about this error, please see http://datatables.net/tn/7

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/antillas21/ajax-datatables-rails/issues/185, or mute the thread https://github.com/notifications/unsubscribe-auth/AAUXZ14cHdmtNZJFuN5RWrzU3N77kgllks5rOFLlgaJpZM4LYz1Z .

luis77 commented 7 years ago

@antillas21 They are not the columns in my database. Are the columns inside the datatable, when placing more than 9 columns in the datatable I get that error, but when deleting any column does not continue to appear said error. And in the link I can not find a solution

ajahongir commented 7 years ago

can you put your datatable and js file here?

luis77 commented 7 years ago

sure @ajahongir , I have been following this tutorial https://github.com/antillas21/ajax-datatables-rails/wiki/Part-1----The-Installation the records are in seeds.rb like the tutorial. this is my UserDatatable

class UserDatatable < AjaxDatatablesRails::Base
  # uncomment the appropriate paginator module,
  # depending on gems available in your project.
  include AjaxDatatablesRails::Extensions::Kaminari
  # include AjaxDatatablesRails::Extensions::WillPaginate
  # include AjaxDatatablesRails::Extensions::SimplePaginator

  def sortable_columns
    # list columns inside the Array in string dot notation.
    # Example: 'users.name'
    @sortable_columns ||= ['users.name' ,'users.phone']
  end

  def searchable_columns
    # list columns inside the Array in string dot notation.
    # Example: 'users.name'
    @searchable_columns ||= ['users.name' ,'users.phone']
  end

  private

  def data
    records.map do |record|
      [
        record.name,
        record.phone,
        record.address,
        record.name,
        record.phone,
        record.address,
        record.name,
        record.name,
        record.address
      ]
    end
  end

  def get_raw_records
    User.all
  end

end
ajahongir commented 7 years ago

what about your js file? Also you can use latest version of this gem on branch 0-4-0. here is sample project - https://github.com/ajahongir/ajax-datatables-rails-v-0-4-0-how-to

ajahongir commented 7 years ago

and what error are you receiving?

luis77 commented 7 years ago

in the console I get this error: `` Started GET "/assets/application.self-f8806224e027f3e3f0138ea9ce99319e298dfdb323304d1f1be6eae8e8c74724.js?body=1" for 127.0.0.1 at 2017-01-02 02:14:04 -0400 [2017-01-02 02:14:16] ERROR WEBrick::HTTPStatus::RequestURITooLarge [2017-01-02 02:14:16] ERROR TypeError: can't convert nil into an exact number /home/luis/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/core_ext/time/calculations.rb:233:in-'

this is the users.coffee

$ -> $('#users-table').dataTable processing: true serverSide: true ajax: $('#users-table').data('source') pagingType: 'full_numbers'


my application js

//= require jquery //= require jquery_ujs //= require turbolinks //= require dataTables/jquery.dataTables //= require_tree .

ajahongir commented 7 years ago

you are useing 'webrick' as rails server, this to use 'thin' or puma.

luis77 commented 7 years ago

@ajahongir Does not work with webrick? Will you have any guide or documentation on how to do the datatablet of the link that you sent me with rails 4?

ajahongir commented 7 years ago
  1. actually you can work with webrick but you have to change some settings. you are having this problem - http://stackoverflow.com/questions/8333158/webrick-requesturitoolarge-should-i-update-or-use-a-different-server

  2. here is readme, you can find some info for 0-4-0 -https://github.com/antillas21/ajax-datatables-rails/tree/v-0-4-0

luis77 commented 7 years ago

@ajahongir Excellent thanks, indeed it was by webrick. now it works

luis77 commented 7 years ago

@ajahongir the version 0-4-0 of the gem is compatible with slqserver?

ajahongir commented 7 years ago

well, gem supports ActiveRecord. official docs

Active Record will perform queries on the database for you and is compatible with most database systems, including MySQL, MariaDB, PostgreSQL and SQLite. Regardless of which database system you're using, the Active Record method format will always be the same.

luis77 commented 7 years ago

@ajahongir The readme you sent me works for version 0-4-0? I'm trying to use it but it shows me the following error: `` NoMethodError (undefined method[]' for nil:NilClass): app/datatables/cliente_datatable.rb:17:in data' app/controllers/clientes_controller.rb:10:inblock (2 levels) in index' app/controllers/clientes_controller.rb:8:in `index'


 I do not know what could be doing wrong. this is my cliente_datatable.rb

class ClienteDatatable < AjaxDatatablesRails::Base

  def view_columns
    # Declare strings in this format: ModelName.column_name
    # or in aliased_join_table.column_name format
    @view_columns ||= {
       IdCli: { source: "Cliente.IdCli", cond: :eq },
       Nombre: { source: "Cliente.Nombre", cond: :like },
    }
  end

  private

  def data
    records.map do |record|
      {
        # example:
         IdCli: record.IdCli,
         Nombre: record.Nombre
      }
    end
  end

  def get_raw_records
    # insert query here
    Cliente.all
  end

  # ==== These methods represent the basic operations to perform on records
  # and feel free to override them

  # def filter_records(records)
  # end

  # def sort_records(records)
  # end

  # def paginate_records(records)
  # end

  # ==== Insert 'presenter'-like methods below if necessary
end
ajahongir commented 7 years ago

could you put your js file too?

luis77 commented 7 years ago

@ajahongir Sure, clientes.coffe

$ ->
  $('#clientes-table').dataTable
    responsive: true
    processing: true
    serverSide: true
    ajax: $('#clientes-table').data('source')
    pagingType: 'full_numbers'
ajahongir commented 7 years ago

I think this error binding error - while you are trying to bind client side and server side columns.

try to define columns, and dont forget assign 'data' field to column - it binds with your datatable.rb column. you can look here - https://github.com/ajahongir/ajax-datatables-rails-v-0-4-0-how-to/blob/master/app/assets/javascripts/cities.js.coffee

luis77 commented 7 years ago

@ajahongir Ok I change the clientes.coffee like this:

$ ->
  $('#clientes-table').dataTable
    responsive: true
    processing: true
    serverSide: true
    ajax: $('#clientes-table').data('source')
    pagingType: 'full_numbers'
    columns: [
        data: "IdCli"
      ,
        data: "Nombre"
    ]

and I get this error:

Started GET "/clientes.json?draw=1&columns%5B0%5D%5Bdata%5D=IdCli&columns%5B0%5D%5Bname%5D=&columns%5B0%5D%5Bsearchable%5D=true&columns%5B0%5D%5Borderable%5D=true&columns%5B0%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B0%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B1%5D%5Bdata%5D=Nombre&columns%5B1%5D%5Bname%5D=&columns%5B1%5D%5Bsearchable%5D=true&columns%5B1%5D%5Borderable%5D=true&columns%5B1%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B1%5D%5Bsearch%5D%5Bregex%5D=false&order%5B0%5D%5Bcolumn%5D=0&order%5B0%5D%5Bdir%5D=asc&start=0&length=10&search%5Bvalue%5D=&search%5Bregex%5D=false&_=1484166472787" for 127.0.0.1 at 2017-01-11 16:28:15 -0400
Processing by ClientesController#index as JSON
  Parameters: {"draw"=>"1", "columns"=>{"0"=>{"data"=>"IdCli", "name"=>"", "searchable"=>"true", "orderable"=>"true", "search"=>{"value"=>"", "regex"=>"false"}}, "1"=>{"data"=>"Nombre", "name"=>"", "searchable"=>"true", "orderable"=>"true", "search"=>{"value"=>"", "regex"=>"false"}}}, "order"=>{"0"=>{"column"=>"0", "dir"=>"asc"}}, "start"=>"0", "length"=>"10", "search"=>{"value"=>"", "regex"=>"false"}, "_"=>"1484166472787"}
  Usuario Load (136.4ms)  EXEC sp_executesql N'SELECT  [usuarios].* FROM [usuarios] WHERE [usuarios].[IdUsuario] = @0  ORDER BY [usuarios].[IdUsuario] ASC OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY', N'@0 int', @0 = 102  [["IdUsuario", 102]]
Completed 500 Internal Server Error in 142ms (ActiveRecord: 136.4ms)

ArgumentError (wrong number of arguments (given 1, expected 2..3)):
  app/datatables/cliente_datatable.rb:7:in `<class:ClienteDatatable>'
  app/datatables/cliente_datatable.rb:1:in `<top (required)>'
  app/controllers/clientes_controller.rb:19:in `block (2 levels) in index'
  app/controllers/clientes_controller.rb:17:in `index'