在Thymeleaf中,用th:each怎么实现由ajax控制的分页列表展示

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Thymeleaf中,用th:each怎么实现由ajax控制的分页列表展示相关的知识,希望对你有一定的参考价值。

如题,在html中要用到controller传过来的参数(参数为页码)遍历,但是js改不了服务端的参数,不想全局刷新的情况下只能用ajax了,但是应该怎么操作?ajax服务器返回的是json流吧,改不了session,request这些域里面的参数值的吧?怎么办?

参考技术A 你好,
后端写个Post,前端AJAX 提交 POST到数据后 后端根据数据对session信息进行修改

Thymeleaf: th:each 排除任何特定值

【中文标题】Thymeleaf: th:each 排除任何特定值【英文标题】:Thymeleaf: th:each with exclusion of any particular value 【发布时间】:2020-03-20 15:52:15 【问题描述】:

我想知道是否有可能做一个 th:each 排除

我的意思是...例如...我们的数据库和模型中有三个状态 FREE,RESERVED,TAKEN (Enum)。

我想做一个 th:each 这些状态,除了一个(在数据库中为特定对象选择的那个,我不想选择已经采用的选项)

有没有类似于我刚刚为这个例子发明th:except 的东西?

<select>
<option th:each="i: $state" th:except="$i.RESERVED" th:text="$i" th:value="$i" ></option>
</select>

所以我可以在这种情况下使用它:

<tr th:each="spot : $spots">
  <td th:text="$spot.name" th:value="$spot.id"></td>
  <td>
    <select>
      <option th:each="i: $state" th:except="$spot.i" th:text="$i" th:value="$i" ></option>
    </select>
  </td>
<tr>

我知道我可能可以在控制器中执行此操作,但我想知道是否有任何“th:thing”可以用来超级快速和轻松地执行此操作!

甚至是一个“th:where”,我可以将它放在与 th:each 相同的标签中......

【问题讨论】:

【参考方案1】:

您可以为此目的使用“th:unless”。鉴于你的例子:

<tr th:each="spot : $spots">
  <td th:text="$spot.name" th:value="$spot.id"></td>
  <td>
    <select>
      <option th:each="i: $state" th:unless="$spot == i" th:text="$i" th:value="$i" ></option>
    </select>
  </td>
<tr>

从您的示例$spot.i 中不清楚该变量是什么,所以我假设您想将spot 的值与i 进行比较。

【讨论】:

【参考方案2】:

Ops,我一周前解决了它,但我忘了评论我是如何做到的。

这比我想象的要容易得多,我无法解释如何,但 Spring 会自动完成很多事情。

这是我的控制器:

@GetMapping("/bajaAnimal")
public String pagBaja(Model model) 

    List<Spot> spotsList = repository.findAllSpotsByState(Status.RESERVED) //IF YOU FIND BY ONE STATUS, IT WILL EXCLUDE IT FROM OPTIONS!
    model.addAttribute("spots", spotsList);

    Status[] statusOptions = Status.values();
    model.addAttribute("statuses", statusOptions );

return "animales/bajaAnimal";

我的 HTML:

<th:block th:each="spot: $spots">
    <select class="form-control" th:name="selectEstado" th:id="selectEstado" required>
        <option value="" selected="selected">Change status</option>
        <th:block th:each="i : $statuses">
            <th:block th:if="$i != spot.status">
                <option th:value="$i" th:text="$i"></option>
            </th:block>
        </th:block>
    </select>
</th:block>

【讨论】:

以上是关于在Thymeleaf中,用th:each怎么实现由ajax控制的分页列表展示的主要内容,如果未能解决你的问题,请参考以下文章

Thymeleaf th:each 用 th:if 过滤

Thymeleaf: th:each 排除任何特定值

Thymeleaf (th:each + th:selected) :从每个循环访问选定属性中的变量

thymeleaf已知数组下标怎么取值

Thymeleaf th:each 在元素之间添加逗号

Spring 和 Thymeleaf:将对象从 th:each 表发送到控制器