ajax-datatables-rails icon indicating copy to clipboard operation
ajax-datatables-rails copied to clipboard

Query multiple columns for single source

Open crstin opened this issue 6 years ago • 7 comments

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,

crstin avatar Feb 25 '18 16:02 crstin

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

gs2589 avatar Apr 04 '18 14:04 gs2589

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 )

inyerade avatar Apr 19 '18 12:04 inyerade

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 avatar May 07 '19 22:05 Okomikeruko

@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

anke1460 avatar May 08 '19 09:05 anke1460

Is there any update on this issue? I've tried each of the suggestions and haven't gotten it to work.

DavidMoritz avatar Aug 10 '19 13:08 DavidMoritz

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 avatar Sep 05 '19 22:09 azyzio

@azyzio Your workaround works perfectly well. Thanks!

choy2007 avatar Sep 10 '19 08:09 choy2007