java - 使用 MaterializeCSS 和 Thymeleaf 进行复选框输入

标签 java html datatables thymeleaf materialize

嘿,我正在使用 Spring Boot 和 Spring Web 以及 Thymeleaf。对于前端,我使用 MaterializeCSS 和数据表 this CSS/JS 样式。

我想显示一个表,用户可以在其中选择通过 Thymeleaf 迭代解析的数据集。我尝试使用包含 CheckBox 元素列表的 CheckBoxWrapper 形式来完成此操作。复选框元素本身包含一个 boolean 值。 您可以在本问题末尾找到我的完整代码。

但是如果我尝试这样做(我认为?),Thymeleaf 会在复选框之后直接生成另一个隐藏输入字段。浏览器中的 HTML 看起来像 this 这会导致浏览器不显示该复选框。如果我(重新)将生成的隐藏输入移到标签之外,则复选框将正确显示。 See the difference on this picture

有没有办法让复选框正确显示,同时仍然能够将复选框输入到服务器? (与 thymeleaf )

如果您需要有关任何问题的其他信息,请告诉我。

代码 - HTML

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      lang="en">
<head>
    <meta charset="UTF-8">
    <title>This is some really important detail</title>
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet" />
    <link rel="stylesheet" th:href="@{/css/materialize.css}" />
    <link rel="stylesheet" th:href="@{/css/datatables.css}" />
</head>
<body>

<div class="container">
    <div id="admin" class="col s10 offset-s1">
        <div class="card material-table">
            <div class="table-header">
                <span class="table-title">Table Headline</span>
                <div class="actions">
                    <a href="#" class="search-toggle waves-effect btn-flat nopadding"><i class="material-icons">search</i></a>
                </div>
            </div>
            <form action="/submit" th:object="${checkBoxWrapper}">
                <table id="datatable">
                    <thead>
                    <tr>
                        <th>Check</th>
                        <th>Name</th>
                    </tr>
                    </thead>
                    <tbody>
                    <th:block th:each="element,status : ${persons}">
                        <tr>
                            <td>
                                <div>
                                    <label>
                                        <input type="checkbox" th:id="'checkbox'+${status.index}"
                                               th:field="*{list[__${status.index}__].val}"/>
                                        <span>Desc</span>
                                    </label>
                                </div>
                            </td>
                            <td>
                                <th:block th:text="${element.name}"> -</th:block>
                            </td>
                        </tr>
                    </th:block>
                    </tbody>
                </table>
            </form>
        </div>
    </div>
</div>

</body>
<script th:src="@{/webjars/datatables/1.10.20/js/jquery.dataTables.js}"></script>
<script th:src="@{/webjars/jquery/3.4.1/jquery.js}"></script>
<script th:src="@{/script/datatables.js}"></script>
<script th:src="@{/script/materialize.js}"></script>
</html>

代码 - Controller

@Controller
public class MyController {

    @GetMapping("/")
    public String home(Model model){

        List<Person> pList = new ArrayList<>();
        pList.add(new Person("Mick"));
        pList.add(new Person("Kevin"));
        pList.add(new Person("Joe"));
        model.addAttribute("persons", pList);

        List<CheckBox> cList = new ArrayList<>();
        for (int i = 0; i < pList.size(); i++) {
            cList.add(new CheckBox(false));
        }
        CheckBoxWrapper checkBoxWrapper = new CheckBoxWrapper(cList);
        model.addAttribute("checkBoxWrapper", checkBoxWrapper);

        return "/home";
    }

    @PostMapping("/submit")
    @ResponseBody
    public String submit(@ModelAttribute CheckBoxWrapper checkBoxWrapper){

        return "success";
    }
}

代码 - 复选框

public class CheckBox {

    private boolean val;

    //Getter, Setter, Constructor

}

代码-CheckBoxWrapper

public class CheckBoxWrapper {

    List<CheckBox> list = new ArrayList<>();

    //Getter, Setter, Constructor

}

代码 - 人员

public class Person{

    String name;

    //Getter, Setter, Constructor

}

最佳答案

如果您可以将隐藏的输入移动到复选框上方,那就很好:

<label>
   <input type="hidden" name="_list[0].val" value="on"><!-- First is ok! -->
   <input type="checkbox" /><!-- I still display -->
   <span>Red</span>
</label>

Codepen

请注意,这里的标记非常严格 - 因为每个唯一的复选框和范围配对都必须包含在自己的标签中。任何与此的变化都会破坏组件。

https://materializecss.com/checkboxes.html

关于java - 使用 MaterializeCSS 和 Thymeleaf 进行复选框输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60327892/

相关文章:

java - JMF 和 RTP 协议(protocol)

java 二维数组 - 排序和搜索

jquery - 我需要 CSS 中的玻璃形状

html - 网站在手机上太宽

java - 性能泄漏的数据表中 JasonArray 的最大容量是多少?

mysql - 使用 NodeJS 和 MySql 的 jQuery 数据表

javascript - 数据表按钮根据发送的变量更改/过滤内容

java - 解释语法 EditText editText = (EditText) findViewById(R.id.edit_message);

java - 如何在 .txt 文件中存储字符串,同时不清除该文件中已有的内容 java

php - 表格对齐 - 字段相距太远