下拉框 th:each 遍历List<Map>
时间: 2024-04-23 22:28:03 浏览: 13
如果你想在 Thymeleaf 的下拉框中遍历一个 List<Map>,你可以使用 th:each 属性来实现。
假设你有一个名为 `list` 的 List<Map>,每个 Map 对象都包含一个键值对。你可以使用下面的代码来生成一个下拉框:
```html
<select>
<option th:each="item : ${list}" th:value="${item.key}" th:text="${item.value}"></option>
</select>
```
在这个示例中,我们使用 th:each 属性来遍历 List<Map> 中的每个 Map 对象,并生成一个 option 标签。`th:value` 属性设置为 Map 的 key,`th:text` 属性设置为 Map 的 value。
请确保在模型中将 List<Map> 对象命名为 `list`,以便在 Thymeleaf 模板中使用。
希望这个示例能帮助到你!如果你有任何其他问题,请随时提问。
相关问题
将js数组中的数据显示到html的下拉框、table、<ul>+<li>等标签上
好的,我可以为您提供一些参考代码:
1. 将数组数据显示在下拉框中:
```html
<select id="mySelect"></select>
```
```javascript
const myArray = ["apple", "banana", "orange"];
const mySelect = document.getElementById("mySelect");
for(let i = 0; i < myArray.length; i++) {
let option = document.createElement("option");
option.text = myArray[i];
mySelect.add(option);
}
```
2. 将数组数据显示在table中:
```html
<table id="myTable">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
```
```javascript
const myArray = [
{ col1: "Value 1", col2: "Value 2", col3: "Value 3" },
{ col1: "Value 4", col2: "Value 5", col3: "Value 6" },
{ col1: "Value 7", col2: "Value 8", col3: "Value 9" }
];
const myTableBody = document.getElementById("myTable").getElementsByTagName("tbody")[0];
for(let i = 0; i < myArray.length; i++) {
let row = myTableBody.insertRow(i);
let cell1 = row.insertCell(0);
let cell2 = row.insertCell(1);
let cell3 = row.insertCell(2);
cell1.innerHTML = myArray[i].col1;
cell2.innerHTML = myArray[i].col2;
cell3.innerHTML = myArray[i].col3;
}
```
3. 将数组数据显示在ul+li标签中:
```html
<ul id="myList"></ul>
```
```javascript
const myArray = ["apple", "banana", "orange"];
const myList = document.getElementById("myList");
for(let i = 0; i < myArray.length; i++) {
let li = document.createElement("li");
li.innerHTML = myArray[i];
myList.appendChild(li);
}
```
希望这些代码可以帮助您实现您的需求。
<select>标签下拉框 显示层级
<select>标签下拉框可以显示层级,具体实现方式是在<option>标签中使用缩进或者连字符表示层级关系。
例如,假设有如下的层级结构:
- 水果
- 苹果
- 红富士
- 嘎啦果
- 香蕉
- 橙子
- 蔬菜
- 西红柿
- 黄瓜
- 茄子
- 辣椒
那么在<select>标签中可以这样实现层级显示:
```html
<select>
<option>水果</option>
<option> 苹果</option>
<option> 红富士</option>
<option> 嘎啦果</option>
<option> 香蕉</option>
<option> 橙子</option>
<option>蔬菜</option>
<option> 西红柿</option>
<option> 黄瓜</option>
<option> 茄子</option>
<option> 辣椒</option>
</select>
```
在上面的代码中,使用了 表示空格,通过不同数量的空格来表示不同的层级关系。这样,在下拉框中就能够清晰地看到每个选项的层级关系。