Skip to content

Commit

Permalink
Update app to sort products using AJAX.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelsey Langlois and LDevinMacKrell committed Dec 7, 2017
1 parent 0415e9f commit 93869ad
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ To install on your own machine, follow the instructions below:
* Administrators may add, edit, and delete products
* Administrators may set a product on sale until a specified date
* Users can raincheck a product for later purchase
* Users can sort product index by one of three methods: alphabetical, price (low to high), and price (high to low)
* Accounts
* Accounts and Users have a one-to-one association
* Administrators may change an account's "Preferred" status
Expand Down
15 changes: 15 additions & 0 deletions app/assets/javascripts/sort_products.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
$(document).ready(function() {
$('#sort-method-button').hide();
$('#sort_method').change(function() {
form = $('#sort-form')[0];
Rails.fire(form, 'submit');
});
});

$(document).on('turbolinks:load', function() {
$('#sort-method-button').hide();
$('#sort_method').change(function() {
form = $('#sort-form')[0];
Rails.fire(form, 'submit');
});
});
6 changes: 3 additions & 3 deletions app/views/products/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@

<h1>All Books</h1>

<%= form_tag(products_path, method: "get") do %>
<%= select_tag(:sort_method, options_for_select([['Alphabetical', 'alphabetical'], ['Price (Low to high)', 'price_ascending'], ['Price (High to low)', 'price_descending']], @sort_method) ) %>
<%= submit_tag "Sort", class: "btn btn-default" %>
<%= form_tag products_path, method: "get", id: "sort-form", remote: true do %>
<%= select_tag :sort_method, options_for_select([['Alphabetical', 'alphabetical'], ['Price (Low to high)', 'price_ascending'], ['Price (High to low)', 'price_descending']], @sort_method), remote: true %>
<%= submit_tag "Sort", id: "sort-method-button", class: "btn btn-default" %>
<% end %>

<div class="all-books">
Expand Down

0 comments on commit 93869ad

Please sign in to comment.