Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ts/jf - added filter on tag page #274

Merged
merged 5 commits into from
Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package edu.ucsb.cs56.mapache_search.controllers;

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
Expand Down Expand Up @@ -64,4 +66,20 @@ public String tagSearch(@ModelAttribute Tag tag, Model model, OAuth2Authenticati
return "tags";
}

@GetMapping("/tags/filter")
public String tagSort(@ModelAttribute Tag tag, Model model, OAuth2AuthenticationToken token, String filter) {
Iterable<Tag> tags = tagRepository.findAll();
List<Tag> allTags = new ArrayList<Tag>();
for (Tag t : tags) {
allTags.add(t);
}
if (filter.equals("alphabetical")) {
Collections.sort(allTags, (t1, t2)->{
return t1.getName().toLowerCase().compareTo(t2.getName().toLowerCase());
});
model.addAttribute("alphabetical", true);
}
model.addAttribute("tags", allTags);
return "tags";
}
}
20 changes: 20 additions & 0 deletions src/main/resources/static/js/tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// tags.js
var $document = $(document);

function tag_page_ready() {
$filterBoxes = $(".filter-checkbox");

function filterBy(e) {
$this = $(this);
if(this.checked) {
var newURL = window.location.protocol + "//";
newURL += window.location.host;
newURL += "/tags/filter?filter=" + $this.val();
window.location.href = newURL;
}
}

$filterBoxes.on("change", filterBy);
}

$document.ready(tag_page_ready);
8 changes: 8 additions & 0 deletions src/main/resources/templates/fragments/tag_search_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
<button class="btn btn-outline-primary" type="submit">Search</button>
</div>
</div>

<div class="filer-checkboxes">
<h5> Filter by: </h5>
<input type="checkbox" class ="filter-checkbox" name="sortByAlphabetical" th:checked="${alphabetical}" value="alphabetical">
<span> Alphabetical </span>
<!--<input type="checkbox" class ="filter-checkbox" name="sortByArticles" var="articles">
<span> Number of articles </span>-->
</div>
</div>

<div class="container">
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/templates/tags.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ <h5>
<div th:replace="fragments/bootstrap_footer.html"></div>
</div>
<div th:replace="fragments/bootstrap_scripts.html"></div>
<script src="/static/js/tags.js" th:src="@{/js/tags.js}"></script>
</body>

</html>