import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
.stream()
.reduce(0, (a, b) -> a > b ? a : b) + 1;
- LOG.info("Adding new card #{}", next);
- form.cards.put(next, new HashMap<>());
+ Map<String, Boolean> content = new LinkedHashMap<>();
+ content.put(form.names.get(0), Boolean.FALSE);
+ LOG.info("Adding new card #{} with content: {}", next, content);
+ form.cards.put(next, content);
+
+ for (Integer id : form.cards.keySet())
+ for (String entry : form.cards.get(id).keySet())
+ LOG.info("{} - {}: {}", id, entry, form.cards.get(id).get(entry));
+
return "form";
}
{
Map<String, Boolean> content = form.cards.remove(card);
LOG.info("Removed card #{} with content: {}", card, content);
+
+ for (Integer id : form.cards.keySet())
+ for (String entry : form.cards.get(id).keySet())
+ LOG.info("{} - {}: {}", id, entry, form.cards.get(id).get(entry));
+
return "form";
}
@RequestMapping(path = "/", params = "add")
public String addRow(@ModelAttribute Form form, @RequestParam Integer add)
{
- LOG.info("Adding row {} to card #{}", form.row.get(add), add);
- form.cards.get(add).put(form.row.get(add), Boolean.FALSE);
+ LOG.info("Adding row {} to card #{}", form.names.get(add), add);
+ form.cards.get(add).put(form.names.get(add), Boolean.FALSE);
+
+ for (Integer id : form.cards.keySet())
+ for (String entry : form.cards.get(id).keySet())
+ LOG.info("{} - {}: {}", id, entry, form.cards.get(id).get(entry));
+
return "form";
}
String row = parts[1];
Boolean value = form.cards.get(card).remove(row);
LOG.info("Removed row {} with value {} from card #{}", row, value, card);
+
+ if (form.cards.get(card).isEmpty())
+ {
+ LOG.info("Card #{} is empty: Removing card!", card);
+ form.cards.remove(card);
+ }
+
+ for (Integer id : form.cards.keySet())
+ for (String entry : form.cards.get(id).keySet())
+ LOG.info("{} - {}: {}", id, entry, form.cards.get(id).get(entry));
+
return "form";
}
}
public class Form
{
Map<Integer, Map<String, Boolean>> cards = new TreeMap<>();
- Map<Integer, String> row = new HashMap<>();
+ Map<Integer, String> names = new HashMap<>();
public Map<Integer, Map<String, Boolean>> getCards()
this.cards = cards;
}
- public Map<Integer, String> getRow()
+ public Map<Integer, String> getNames()
{
- return row;
+ return names;
}
- public void setRow(Map<Integer, String> row)
+ public void setNames(Map<Integer, String> names)
{
- this.row = row;
+ this.names = names;
}
}
</head>
<body>
<nav class="navbar navbar-dark bg-primary navbar-expand navbar-dark flex-column">
- <h2 class="navbar-brand">Demo: Multidimensional Map With Dynamic Binding</h2>
+ <h1 class="navbar-brand">Demo: Multidimensional Map With Dynamic Binding</h1>
</nav>
<main class="container mt-5">
<form action="#" th:action="@{/}" th:object="${form}" method="get">
- <div class="card" th:each="card : *{cards.entrySet()}">
- <div class="card-header"><h1 th:text="|Card #${card.key}|">Card #X</h1></div>
+ <div class="card my-3" th:each="card : *{cards.entrySet()}">
+ <div class="card-header"><h2 th:text="|Card #${card.key}|">Card #X</h2></div>
<div class="card-body">
<div class="card-text">
<div class="form-group" th:each="row : ${card.value.entrySet()}">
<button type="submit" name="remove" class="btn btn-primary" th:value="|${card.key}:${row.key}|">Remove</button>
</div>
<div class="form-group">
- <input type="text" th:field="*{row[__${card.key}__]}" />
+ <input type="text" th:field="*{names[__${card.key}__]}" />
<button type="submit" name="add" th:value="${card.key}" class="btn btn-primary">Add Row</button>
</div>
</div>
</div>
</div>
</div>
- <p><button type="submit" name="card" value="add" class="btn btn-primary">Add Card</button></p>
+ <div class="card my-3">
+ <div class="card-header"><h2>Add A New Card...</h2></div>
+ <div class="card-body">
+ <div class="card-text">
+ <input type="text" th:field="*{names[0]}" />
+ <button type="submit" name="card" value="add" class="btn btn-primary my-3">Add Card</button>
+ </div>
+ </div>
+ </div>
</form>
</main>
</body>