I should have 3 checkbox OP, SE and PM. If one or two is checked or even all three, the view will filter according to the checked values. These are employee_type
and if I check OP, all employees that belong to OP type will show. I have this code using gem 'ransack'
on ruby on rails
On my index.html.erb
<%= search_form_for @filter do |f| %>
<%= f.check_box :employee_type, :value => 'OP', :id => 'checkOP' %>
<%= f.check_box :employee_type, :value => 'SE', :id => 'checkSE' %>
<%= f.check_box :employee_type, :value => 'PM', :id => 'checkPM' %>
<%= f.submit 'Filter' %>
<% end %>
<script type="text/javascript">
$('#checkOP').on('change', function() {
var val = this.checked ? this.value : '';
$('#index').html(val);
});
$('#checkSE').on('change', function() {
var val = this.checked ? this.value : '';
$('#index').html(val);
});
$('#checkPM').on('change', function() {
var val = this.checked ? this.value : '';
$('#index').html(val);
});
</script>
On my employees_controller
def index
@filter = Employee.search(params[:q])
@employees = @filter.result
end
There shouldn't be a button since it should have filtered the moment the checkbox is checked or unchecked but I use button just to test since there's something wrong with my code that won't auto-detect if the checkbox is checked or not.
There seems to have no solution in the internet or I just couldn't find the right words to search. Any help is much appreciated.
Aucun commentaire:
Enregistrer un commentaire