Closed trselva closed 4 years ago
I am not sure what "search on each column" mean. Assume that you want to have a pair of filters where you select the column name and specify the search query for that column:
filter(:column_selection, :enum, select: :columns_select, dummy: true)
filter(:column_search, :string) do |value, scope, grid|
if (grid.column_selection)
scope.where(grid.column_selection => value)
end
end
def columns_select
# you may limit the list of columns available in selection
columns.map do |column|
[column.header, column.name]
end
end
I meant that having a text filed/drop down selection below to column header (not in form) and press enter key or any button to filter by the input value on that column. Likewise, search field to be provided for each column below to column header.
Also I tried using the code above as it may be helpful, but it gives me an error.
undefined method `where' for "India":String Did you mean? when
Request
Parameters:
{"utf8"=>"✓", "country_grid"=>{"column_selection"=>"name", "column_search"=>"India", "condition"=>["", "=", ""]}, "commit"=>"Search"}
Swap scope
and value
in the filter args. Updated my example accordingly.
Thanks bogdan. Is it possible with datagrid to achieve the column search that I meant ?
I don't know what you mean.
I want to have filter (drop-down or text field) as shown in screenshot (highlighted in red line) . this feature is in wice_grid gem. So I wanted to know if it is possible to implement in datagrid.
I am not familiar with wice_grid. If you won't explain what you want in details, I have no way to help you.
Ok. Let me brief on it.
Basically the requirement is to have a table search. There should be a Search Field, based on the input in search field, it will filter the rows that matching the input value. something like shown in the link here:- https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_filter_table
class CustomersGrid
include Datagrid
scope { Customer }
filter(:name, :string) do |value|
where("lower(name) like ?", "%#{value.downcase}%")
end
column(:name)
column(:country)
end
The lower
call depends on the database you are using. Google for lowercasing the string for your RDBMS.
Thanks and never mind please. Actually I want to search a table, not specific to any column. If I search any string, the grid table should display the rows as long as that string exists in row.
I tried something like this.
filter(:column_search, :string) do |value, scope, grid|
tab_search = []
columns.map do |column|
tab_search.push(column.name)
end
puts tab_search
tab_search.each do |s|
scope.where(s => value)
end
end
But it gives me below error. ActionView::Template::Error (Can not apply :column_search filter: result [:country, :id, :created, :name, :tag] no longer match Datagrid::Drivers::ActiveRecord.):
filter(:column_search, :string) do |value, scope, grid|
scope.where(
columns.map do |column|
"#{column.name} like :value"
end.join(" OR "),
value: "%#{value}%",
)
end
Great , Thank you so much Bogdan. Let me use this and do more with what I needed.
Is it possible to have column level search or drop down filter in column level ?
There is a report which has multiple columns. User want to apply search on each column OR drop down filter on each column.