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

AjaxDatatablesRails::Error::InvalidSearchColumn (Check that column '12' exists in view_columns): #385

Closed zedalaye closed 3 years ago

zedalaye commented 3 years ago

Hello,

Since I upgraded AjaxDatatablesRails to the latest master I have this error:

AjaxDatatablesRails::Error::InvalidSearchColumn (Check that column '12' exists in view_columns):

My Datatable class is defined as:

class CompanyInvoicesDatatable < ApplicationDatatable
  def_delegators :view, :l, :t,
                        :number_to_currency,
                        :content_tag, :image_tag, :link_to,
                        :company_admin_order_path, :company_admin_invoice_path, :invoices_path_for,
                        :clone_company_admin_invoice_path,
                        :localized_country_name

  def initialize(params, opts = {})
    @company = opts[:company]
    @start_of_period = opts[:start_of_period]
    @end_of_period = opts[:end_of_period]
    super
  end

  def view_columns
    @view_columns ||= {
      :'0'  => { source: 'Invoice.number' },
      :'1'  => { source: 'Order.number' },
      :'2'  => { source: 'Invoice.created_at' },
      :'3'  => { source: 'Invoice.paid_on' },
      :'4'  => { source: 'Profile.registration_code' },
      :'5'  => { source: 'Invoice.full_name' },
      :'6'  => { source: 'Order.postal_code' },
      :'7'  => { source: 'Order.country' },
      :'8'  => { source: 'Invoice.postal_code' },
      :'9'  => { source: 'Invoice.country' },
      :'10' => { source: 'Invoice.total_amount', cond: filter_amount },
      :'11' => { source: 'Invoice.total_points', cond: filter_points },
      # :'12' => { }
    }
  end

  private

  def data
    records.map do |record|
      {
         '0' => link_to_invoice(record),
         '1' => link_to_order(record),
         '2' => l(record.created_at, format: :short),
         '3' => format_paid_on(record),
         '4' => link_to_profile(record),
         '5' => record.full_name,
         '6' => record.order.try(:postal_code),
         '7' => country_for(record.order),
         '8' => record.postal_code,
         '9' => country_for(record),
         '10' => number_to_currency(record.total),
         '11' => record.total_points,
         '12' => invoice_links(record),
         'DT_RowId' => record.id
      }
    end
  end

  ...

  def invoice_links(invoice)
    if invoice.pdf.present? || !invoice.numbered?
      download = link_to(image_tag('pdf.svg', size: '17x18'), company_admin_invoice_path(invoice, format: :pdf), target: :blank, title: t('hints.export_pdf'), style: "padding-right: 2px")
    else
      download = ''.html_safe
    end
    clone = link_to(content_tag(:span, '', class: 'glyphicon glyphicon-duplicate'), clone_company_admin_invoice_path(invoice), class: 'btn btn-primary btn-xs')
    download + clone
  end

 ...

end

The column #12 is reserved for displaying tool buttons (like a delete button) so there is no data in this column.

When I uncomment the "column 12 definition" in view_columns, the error disappears but the datatable is not searchable, I get an error in column.rb line #39

How do I fix that ?

Thank you.

-- Pierre Y.

zedalaye commented 3 years ago

Don't know if it was the right way to solve this issue, I changed my view_columns definition to make "virtual column" not searchable :

  def view_columns
    @view_columns ||= {
      ...
      :'12' => { searchable: false }
    }
  end
n-rodriguez commented 3 years ago

Don't know if it was the right way to solve this issue, I changed my view_columns definition to make "virtual column" not searchable :

It's definitely the way to go.

What I do in this case is binding the column to { source: Model.id, searchable: false, orderable: false }