【发布时间】:2020-09-09 03:02:21
【问题描述】:
我有一个表单,用户可以在其中添加任意数量的 Table 对象,它还可以包含任意数量的 Columns 对象(例如构建表格在 SQL 中).. 我尝试了下面的代码,但没有任何效果,当我尝试绑定两个列表时,表单不再出现。
控制器
@ModelAttribute("page")
public Page getTable() {
TableColumn column = new TableColumn();
List<TableColumn> columns = new ArrayList<>();
columns.add(column);
Table table = new Table();
table.setColumns(columns);
List<Table> tables = new ArrayList<>();
tables.add(table);
Page page = new Page();
page.setTables(tables);
return page;
}
@GetMapping("/scriptsqlgenerator")
public String viewForm(@ModelAttribute("page") Page page) {
return "ScriptSqlNext";
}
@PostMapping("/scriptsqlgenerator")
public String generateScript(@ModelAttribute("page") Page page) {
page.tables.forEach((t) ->{
System.out.println(t.getName());
t.getColumns().forEach((c) -> {
System.out.println(c.getName());
System.out.println(c.getType());
System.out.println(c.getIndex());
System.out.println(c.getNotnull());
});
});
}
HTML
<form th:object="${page}" class="list-group" th:action="@{/filegenerated}" method="get">
<a class="list-group-item list-group-item-action" data-toggle="collapse" data-target="#target1"> Create Table </a>
<div id="target1" class="collapse" style="margin: 30px;">
<div id="tablelist">
<div class="form-inline itemtable" th:each="table, itemStat :${page.tables}">
<div class="form-group mb-2 d-none">
<input th:field="*{tables[__${itemStat.index}__].id}" type="text" class="form-control">
</div>
<div class="form-group mb-2">
<input th:field="*{tables[__${itemStat.index}__].name}" type="text" class="form-control" placeholder="Table name">
</div>
<input type="button" class="btn btn-danger mb-2 ml-2" onclick="addRow()" value="Add column">
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th scope="col" class="d-none">Id</th>
<th scope="col">Column Name</th>
<th scope="col">Type</th>
<th scope="col">Index</th>
<th scope="col">Null</th>
</tr>
</thead>
<tbody id="columnlist">
<tr class="item" th:each="column,status :
${table.columns}">
<td><input th:field="*{tables[__${itemStat.index}__].columns[__${status.index}__].name}" type="text" class="form-control" required></td>
<td><select th:field="*{tables[__${itemStat.index}__].columns[__${status.index}__].type}" id="inputState" class="form-control" required>
<option value="" selected
disabled>Choose</option>
<option th:value="${type}">int</option>
<option th:value="${type}">varchar</option>
</select>
</td>
<td><select th:field="*{tables[__${itemStat.index}__].columns[__${status.index}__].index}" id="inputState" class="form-control" required>
<option value="" selected
disabled>Choose</option>
<option th:value="${index}">on</option>
<option th:value="${index}">off</option>
</select>
</td>
<td><select th:field="*{tables[__${itemStat.index}__].columns[__${status.index}__].notnull}" id="inputState" class="form-control" required>
<option value="" selected
disabled>Choose</option>
<option th:value="${notnull}">on</option>
<option th:value="${notnull}">off</option>
</select>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<button class="btn btn-danger mb-2 text-center" type="button" id="addTable" style="margin-top: 30px;">Add table</button>
</div>
<div class="text-center">
<button type="submit" class="btn btn-outline-danger btn-lg" style="margin-
top: 50px;">Generate File</button>
</div>
</form>
对于 JS 部分,我使用一些代码来实现 addRow() 方法,该方法将向表中添加更多列,addTable() 方法将添加另一个表对象,但对我的情况没有任何作用
【问题讨论】:
标签: javascript java spring spring-mvc thymeleaf