Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with columns definition #159

Closed
nevedimko opened this issue Jun 27, 2016 · 12 comments
Closed

Problem with columns definition #159

nevedimko opened this issue Jun 27, 2016 · 12 comments
Assignees
Labels

Comments

@nevedimko
Copy link

I want to use named columns. Like

$(function() {
  return $('#table_id').dataTable({
    processing: true,
    serverSide: true,
    ajax: 'ajax_url',
    columns: [
      {data: 'id' },
      {data: 'name' },
      {data: 'phone' }
    ]
  });
});

I set the following in my_datatable.rb

def data
  records.map do |record|
    {
      'id' => record.id,
      'name' => record.name,
      'phone' => record.phone
    }
  end
end

And after that I get an error:
no implicit conversion from nil to integer

@antillas21
Copy link
Collaborator

I think this functionality is part of what @ajahongir implemented on the v-0-4-0 branch.

@ajahongir
Copy link
Collaborator

@nevedimko can you show your my_datatable.rb?

@nevedimko
Copy link
Author

@ajahongir Yes, sure. It looked like this:

class SubjectDatatable < AjaxDatatablesRails::Base

  def sortable_columns
    # Declare strings in this format: ModelName.column_name
    @sortable_columns ||= [
      'Subject.id',
      'Subject.name',
      'Subject.suggest'
    ]
  end

  def searchable_columns
    # Declare strings in this format: ModelName.column_name
    @searchable_columns ||= [
      'Subject.id',
      'Subject.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,
        'id' => record.id,
        'name' => record.name,
        'suggest' => record.suggest
      ]
    end
  end

  def get_raw_records
    Subject.all
  end

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

I'm trying to make friends angular-datatables with ajax-datatables-rails

Also I have a problem with using .group(:id) in get_raw_records function. After that count of all results and count of filtered results break down and looks like NaN

Also when I'm using a custom select like .select("COUNT(related_model.id) as count") in get_raw_records function, then I can't use it to search for.

@ajahongir
Copy link
Collaborator

well, I recomend you use latest version(0.4) of the gem and you will have search & sort out of the box. try to define like this:

class SubjectDatatable < AjaxDatatablesRails::Base

  def view_columns
    @view_columns ||= {
      id: { source: "Subject.id", cond: :eq },
      name: { source: "Subject.name" },
      suggest: { source: "Subject.suggest" }
    }
  end

  private

  def data
    records.map do |record|
      [
        # comma separated list of the values for each cell of a table row
        # example: record.attribute,
        'id' => record.id,
        'name' => record.name,
        'suggest' => record.suggest
      ]
    end
  end

  def get_raw_records
    Subject.all
  end

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

also look at this sample project https://github.com/ajahongir/ajax-datatables-rails-v-0-4-0-how-to

@ajahongir
Copy link
Collaborator

@nevedimko
Copy link
Author

@ajahongir Thanks for your response. I tried to work with version 0.4 and this is my_datatable.rb

class SubjectDatatable < AjaxDatatablesRails::Base

  def view_columns
    @view_columns ||= {
      id: { source: "Subject.id", cond: :eq },
      name: { source: "Subject.name" },
      suggest: { source: "Subject.suggest" },
      usage_count: { source: "usage_count" }
    }
  end

  private

  def data
    records.map do |record|
      [
        # comma separated list of the values for each cell of a table row
        # example: record.attribute,
        'id' => record.id,
        'name' => record.name,
        'suggest' => record.suggest,
        'usage_count' => record[:usage_count]
      ]
    end
  end

  def get_raw_records
    Subject.select('subjects.*, COUNT(users.id) AS usage_count')
      .joins('LEFT JOIN users ON subjects.name = users.major')
      .group('subjects.id')
  end

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

If I set order[0][column]=0 in query params, then I see no implicit conversion from nil to integer

If order[0][column]=id, then Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ASC LIMIT 10 OFFSET 0' at line 1: SELECT subjects.*, COUNT(users.id) AS usage_count FROM subjects LEFT JOIN users ON subjects.name = users.major GROUP BY subjects.id ORDER BY ASC LIMIT 10 OFFSET 0

@ajahongir
Copy link
Collaborator

@nevedimko make sure that using right version of the gem. try to include like this:
gem 'ajax-datatables-rails', github: 'antillas21/ajax-datatables-rails', branch: 'v-0-4-0'

@nevedimko
Copy link
Author

Great. Thanks.
I used this gem:
gem 'ajax-datatables-rails', github: 'ajahongir/ajax-datatables-rails', branch: 'v-0-4-0'

Now there's just one issue:
Showing 1 to 10 of 866 entries (filtered from NaN total entries)

Total count works incorrect with my get_raw_records function.

@ajahongir
Copy link
Collaborator

ajahongir commented Jun 30, 2016

@nevedimko try to override as_json method as

    def as_json(options = {})
      {
        recordsTotal: get_raw_records.count(:all).count,
        recordsFiltered: get_raw_records.model.from("(#{filter_records(get_raw_records).except(:limit, :offset, :order).to_sql}) AS foo").count,
        data: data
      }
    end

@nevedimko
Copy link
Author

I already did so.
@ajahongir Thank you. You helped me a lot

@ajahongir
Copy link
Collaborator

ajahongir commented Jun 30, 2016

круто

@n-rodriguez
Copy link
Member

Fixed in master branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants