【发布时间】:2019-07-30 11:11:20
【问题描述】:
我正在开发一个包含课程的虚拟学习平台。目前,我有一些课程,现在我想按标题按升序或降序对它们进行排序。现在我无法将百里香叶与我的排序功能联系起来。
课程Html页面代码:
<!-- Courses -->
<div class="courses">
<div class="container">
<div class="select">
<select id="orderSelect" class="form-control input-sm">
<option selected value="ratingAsc">Order by Rating Ascending</option>
<option value="ratingDesc">Order by Rating Descending</option>
<option value="titleAsc"> Order by Title Ascending</option>
<option value="titleDesc">Order by Title Descending</option>
<option value="totalVotesAsc">Order by Total Votes Ascending</option>
<option value="totalVotesDesc">Order by Total Votes Descending</option>
<option value="membersAsc">Order by Members Amount Ascending</option>
<option value="membersDesc">Order by Members Amount Descending</option>
</select>
</div>
<div class="row courses_row">
<!-- Course -->
<div class="col-lg-4 course_col" th:each="course: ${allCourses}">
<div class="course">
<div class="course_image"><img src="../static/img/course_6.jpg" th:src="@{/img/course_6.jpg}"
alt=""></div>
<div class="course_body">
<div class="course_title">
<a href="course.html" th:href="@{'/courses/'+${course.id}}" th:text="${course.title}"
th:alt="CourseTitle">
Course title
</a>
</div>
<div class="course_info">
<ul>
<li><a href="instructors.html" th:href="@{instructors}">Sarah Parker</a></li>
<li><a href="#" th:alt="Topic">English</a></li>
</ul>
</div>
<div class="course_text">
<p th:text="${course.description}" th:alt="Description">Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Fusce enim nulla.</p>
</div>
</div>
<div class="course_footer d-flex flex-row align-items-center justify-content-start">
<div class="course_students"><i class="fa fa-user" aria-hidden="true"></i><span>10</span>
</div>
<div class="course_rating ml-auto"><i class="fa fa-star"
aria-hidden="true"></i><span>4,5</span></div>
</div>
</div>
</div>
</div>
<div class="row flex-row">
<ul class="nav-pills">
<li class="col-lg-4 mt-auto"
th:each="i:${#numbers.sequence(0,allCourses.totalPages -1)}">
<a th:href="@{/courses(page=${i})}" th:text="${i+1}" class="nav-link"
th:classappend="${currentPage}==${i}?'active':''"></a>
</li>
</ul>
</div>
</div>
</div>
这里是控制器:
@Controller
public class CoursesController {
private CourseService courseService;
@Autowired
public CoursesController(CourseService courseService) {
this.courseService = courseService;
}
@GetMapping("courses")
public String showCoursesPageSorted(Model model,@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "titleAsc") String sortBy){
model.addAttribute("allCourses", courseService.findBy(page,sortBy));
model.addAttribute("currentPage",page);
model.addAttribute("currentSort",sortBy);
return "courses";
}
到目前为止,我想出的唯一解决方案是为每个过滤器创建一个新的 HTML 页面,但它看起来不正确..
【问题讨论】:
-
最佳选择是在数据库查询级别(最佳)或对象级别(良好)进行排序。
-
我正在使用 JpaRepository 在 CourseRepository 接口中进行排序,如下所示: public interface CourseRepository extends JpaRepository
{ Course getById(long id); Page findAllByOrderByAvgRatingDesc(Pageable pageable); Page findAllByOrderByAvgRatingAsc(Pageable pageable); Page findAllByOrderByTotalVotesAsc(Pageable pageable); Page findAllByOrderByTotalVotesDesc(Pageable pageable); Page findAllByOrderByTitleAsc(Pageable pageable); Page findAllByOrderByTitleDesc(Pageable pageable); }
标签: spring jpa thymeleaf option selected