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

问题描述 投票:0回答:1

我有这个:

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

getCategories()只返回一个Categorys列表,而getAllTallies只返回所有Tallys的列表。

如果请求参数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,这是可能的。但是我很难弄清楚如何通过下拉列表动态更新表格。

javascript java spring spring-boot thymeleaf
1个回答
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添加了ids。

调节器

@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>');
                })
            }
})
© www.soinside.com 2019 - 2024. All rights reserved.