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

Support for Derived/Dynamic Columns #424

Open jonmchan opened 4 months ago

jonmchan commented 4 months ago

I created a virtual or derived column with the following code:

  def view_columns
    # Declare strings in this format: ModelName.column_name
    # or in aliased_join_table.column_name format
    @view_columns ||= {
      ...
      br_baths: { source: 'Property.br_baths', searchable: false },
    }
  end

  def data
    records.map do |record|
      {
        ...
        br_baths: "#{record.bedrooms} BR/#{record.consolidated_bathrooms} Baths",
      }
    end
  end

The table shows up properly, however the search is broken because the Property.br_baths column does not exist.

This fails with error:

 ActiveRecord::StatementInvalid (PG::UndefinedColumn: ERROR:  column properties.br_baths does not exist

Is there any way to modify the query that is generated from search or make this field not searchable? I was expecting the searchable: false to prevent this field from being searched but it is not working. I could be mistaken, but I was noticing that there are no tests for searchable: false. It would seem that all the columns are being included in the search. This would be one solution, the other solution is to somehow generate custom SQL for this such that instead of searching br_baths, it was searching bedrooms and bathrooms indepedently.

Thank you for any support and feedback.

jonmchan commented 4 months ago

After examining the code, I have my doubts if searchable: false was ever implemented. I couldn't find a test for it. I'm going to take a stab and see what I can do with it.

searchable: false works, I did add more tests to test it and understand its functionality. These tests may be helpful to make sure searchable: false does not regress.