I have a Hashmap object allList in the form with type HashMap<String,ArrayList<Item>>. I want to display it my JSP page as jquery accordion. Below is the code I have tried.
<script type="text/javascript">
$(function() {
$( "#accordion" ).accordion({
heightStyle: "fill",
collapsible: true
});
});
</script>
<div id="accordion">
<c:forEach items="${allList}" var="myLs">
<h3>${myLs.key}</h3>
<div>${myLs.value}</div> // This is giving me toString of Item.
</c:forEach>
</div>
I am able to display hashmap’s keys as header. But I am unable to figure out how to display the corresponding arraylist object as ordered list. Please help me out.
public class Item implements java.io.Serializable, Comparable<Object> {
private Long id;
private String itemName;
private Double unitCost;
private String status;
private int quantity;
public Item() {
}
//getters and setters
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Item)) {
return false;
}
final Item item = (Item) o;
if (getItemName() != null && item.getItemName() == null)
return false;
if (getItemName() == null && item.getItemName() != null)
return false;
if (!getItemName().equals(item.getItemName()))
return false;
return true;
}
public int hashCode() {
return getItemName().hashCode();
}
public String toString() {
return "Item - Id: "+getId+", Name : "+getItemName;
}
public int compareTo(Object o) {
if (o instanceof Item) {
return getItemName().compareTo(((Item) o).getItemName());
}
return 0;
}
}
You would use a second forEach loop:
I think you’re confusing yourself by your bad naming choices. You should’ name a
Map<String, ArrayList<Item>>allList, since it’s not a list, but a map. And you shouldn’t name a map entrymyLssince it doesn’t mean anything. I would refactor the code to (assuming the key in the map, for example, represents the owner of the items)