ajax-datatables-rails
ajax-datatables-rails copied to clipboard
Query multiple columns for single source
Is ist possible to combine queries into a single column in view_columns
?
first_name: { source: 'User.first_name' },
last_name: { source: 'User.last_name' },
name: { source: {{query_both_above}} },
In def data
there would be:
name: record.User.first_name + ' ' + record.User.last_name,
This issue talks about it. I have not been able to get it to work. Once we figure it out, I'd be happy to write up a guide for the readme https://github.com/jbox-web/ajax-datatables-rails/issues/165
Now I use other aproach of #165
I define the "new column" like
STATUS_COL = "CASE WHEN end_at < '#{ Time.zone.now }' THEN 'vencido' WHEN enable_broadcast = FALSE THEN 'pausado' WHEN start_at > '#{ Time.zone.now }' THEN 'por anunciar' ELSE 'anunciando' END"
Then I add to select the new column
def get_raw_records
Notice.select("notices.* , #{ STATUS_COL } AS status" )
end
And use the filter like
def filter_status
->(column) { ::Arel::Nodes::SqlLiteral.new( STATUS_COL ).matches("#{ column.search.value }%") }
end
This is a working code work with order and search,
I think you can change the definition of de new column to some like
CONCAT( first_name , ' ' , last_name )
I got a column to work by simply doing this in the @view_colums
declaration:
:name => { source: "Customer.last_name, Customer.first_name" }
So it sorts, and searches, based on the order of the sources.
@Okomikeruko I done it like your method, but it does'nt works, and it will sort and searches by Customer.first_name of last defined
Is there any update on this issue? I've tried each of the suggestions and haven't gotten it to work.
A possible workaround:
- Make your aggregate column searchable via first_name
- Add another column searchable via last name, in your view, in the
add data-visible='false'
. This way the column won't be visible to the user, but the backend will see it for search purposes
@azyzio Your workaround works perfectly well. Thanks!