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

search and order queries not being applied in get_raw_records #368

Closed mrudult closed 3 years ago

mrudult commented 4 years ago

This is my datatable class:

class BrandDatatable < ApplicationDatatable

  def view_columns
    @view_columns ||= {
      display_name: {source: "Brand.display_name", cond: :like, searchable: true},
      active: "Brand.active",
      brand_reference_no: "Brand.brand_reference_no",
      created_at: "Brand.created_at"
    }
  end

  private

    def data
      records.map do |record|
        [
          record.display_name,
          record.active,
          record.brand_reference_no,
          record.created_at.to_s.to_date
        ]
      end
    end

    def get_raw_records
      Brand.all
    end
end

This is request being sent to the controller from the frontend:

Started GET "/app/management/brands?draw=3&columns%5B0%5D%5Bdata%5D=0&columns%5B0%5D%5Bname%5D=&columns%5B0%5D%5Bsearchable%5D=true&columns%5B0%5D%5Borderable%5D=true&columns%5B0%5D%5Bsearch%5D%5Bvalue%5D=tera&columns%5B0%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B1%5D%5Bdata%5D=1&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&columns%5B2%5D%5Bdata%5D=2&columns%5B2%5D%5Bname%5D=&columns%5B2%5D%5Bsearchable%5D=true&columns%5B2%5D%5Borderable%5D=true&columns%5B2%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B2%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B3%5D%5Bdata%5D=3&columns%5B3%5D%5Bname%5D=&columns%5B3%5D%5Bsearchable%5D=true&columns%5B3%5D%5Borderable%5D=true&columns%5B3%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B3%5D%5Bsearch%5D%5Bregex%5D=false&order%5B0%5D%5Bcolumn%5D=0&order%5B0%5D%5Bdir%5D=asc&start=0&length=50&search%5Bvalue%5D=&search%5Bregex%5D=false" for ::1 at 2020-08-07 19:59:20 +0530
Processing by BrandsController#index as JSON
  Parameters: {"draw"=>"3", "columns"=>{"0"=>{"data"=>"0", "name"=>"", "searchable"=>"true", "orderable"=>"true", "search"=>{"value"=>"tera", "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"=>"true", "orderable"=>"true", "search"=>{"value"=>"", "regex"=>"false"}}}, "order"=>{"0"=>{"column"=>"0", "dir"=>"asc"}}, "start"=>"0", "length"=>"50", "search"=>{"value"=>"", "regex"=>"false"}}
   (0.4ms)  SELECT COUNT(*) FROM `brands`
  CACHE  (0.0ms)  SELECT COUNT(*) FROM `brands`
  Brand Load (1.0ms)  SELECT  `brands`.* FROM `brands` LIMIT 10 OFFSET 0
Completed 200 OK in 9ms (Views: 2.9ms | ActiveRecord: 3.0ms)

As we can see, no search queries is getting generated or build by get_raw_records. Any help will be highly appreciated!

We are trying to upgrade our app from Rails 4.x to 5.x and ajax-datatable-rails from 0.3 to 1.2. Everything was working fine in 0.3

n-rodriguez commented 4 years ago

See the migration doc : https://github.com/jbox-web/ajax-datatables-rails/blob/master/doc/migrate.md

mrudult commented 4 years ago

@n-rodriguez I did follow the migration guide. I'm still getting the same issue. Also, according to the docs, the following should work:

# after
respond_to do |format|
  format.json { render json: BrandDatatable.new(params, view_context: view_context) }
end

But I get - NoMethodError (undefined method 'fetch' for nil:NilClass):

However, if I do:

respond_to do |format|
  format.json { render json: BrandDatatable.new(params: params, view_context: view_context) }
end

Then it works (without the search and ordering)

n-rodriguez commented 4 years ago

BTW this is wrong :


  def view_columns
    @view_columns ||= {
      display_name: {source: "Brand.display_name", cond: :like, searchable: true},
      active: "Brand.active",
      brand_reference_no: "Brand.brand_reference_no",
      created_at: "Brand.created_at"
    }
  end

See : https://github.com/jbox-web/ajax-datatables-rails#a-declare-columns-mapping

mrudult commented 4 years ago

@n-rodriguez Tried that too, but to no avail.