下拉列表更改时更改表内容

时间:2018-12-17 07:46:41

标签: javascript java spring spring-boot thymeleaf

我有这个:

@GetMapping("/records")
public String getRecords(Model model) {
    model.addAttribute("tallies", getAllTallies(null));
    model.addAttribute("categories", getCategories());
    return "records";
}

getCategories()仅返回Category的列表,而getAllTallies仅返回所有Tally的列表。

如果请求的参数category为null,则无论类别如何,它都将返回所有计数。但是,如果它不为null,则仅返回指定tallies中的所有category

这是getAllTallies

@GetMapping("/tallies")
@ResponseBody
public List<Tally> getAllTallies(@RequestParam(required = false) String category)

然后在我的records.html中:

<body>
<div>
    <select class="form-control" >
        <option value="0">Select Category</option>
        <option th:each="category : ${categories}" th:value="${category.id}" th:text="${category.category}"></option>
    </select>
</div>
<table cellpadding="10" border="1px">
    <thead>
    <tr>
        <th> Nominee </th>
        <th> Category </th>
        <th> Tally </th>
    </tr>
    </thead>
    <tbody>
    <tr th:if="${tallies.empty}">
        <td colspan="3"> No Books Available </td>
    </tr>
    <tr th:each="tally : ${tallies}">
        <td><span th:text="${tally.nominee}"> Nominee </span></td>
        <td><span th:text="${tally.category}"> Category </span></td>
        <td><span th:text="${tally.count}"> Tally </span></td>
    </tr>
    </tbody>
</table>
</body>

我想做的是每当我从下拉列表(类别)中选择一个新值时,表格也会根据类别而变化。 我在想如果没有jQuery或js ,只有html,这是否可能。但是我很难弄清楚如何通过下拉列表动态更新表。

1 个答案:

答案 0 :(得分:1)

否,如果没有jsjQuery,将无法实现这一目标。问题是,Thymeleaf在服务器端工作。因此,一旦呈现,它将不会再次呈现,除非您使用ajax刷新页面或从服务器请求响应。就您而言,简单的ajax就可以解决问题。

HTML

<body>
<div>
    <select id="categories" class="form-control" >
        <option value="0">Select Category</option>
        <option th:each="category : ${categories}" th:value="${category.id}" th:text="${category.category}"></option>
    </select>
</div>
<table cellpadding="10" border="1px">
    <thead>
    <tr>
        <th> Nominee </th>
        <th> Category </th>
        <th> Tally </th>
    </tr>
    </thead>
    <tbody id="tallies">
    <tr th:if="${tallies.empty}">
        <td colspan="3"> No Books Available </td>
    </tr>
    <tr th:each="tally : ${tallies}">
        <td><span th:text="${tally.nominee}"> Nominee </span></td>
        <td><span th:text="${tally.category}"> Category </span></td>
        <td><span th:text="${tally.count}"> Tally </span></td>
    </tr>
    </tbody>
</table>
</body>

为您的类别选择和tbody添加了ID。

控制器

@GetMapping("/tallies")
@ResponseBody
public List<Tally> getAllTallies(@RequestParam(value = "category", required = false) String category)

为您的参数添加了名称。

jQuery

$('#categories').on('change', function() {
   $.ajax({
            url: '/tallies',
            type: 'GET',
            data: {
                category: $(this).val();
            },
            success: function(tallies) {
                // First, let's crear our tbody.
                $('#tallies').empty();

                // Now, let's go through each one of the tallies.
                $(tallies).each( function() {
                    // Here you should add your missing fields. I am just adding one as an example.
                    $('#tallies').append('<tr><td><span>' + this.nominee + '</span></td></tr>');
                })
            }
})