samedi 12 août 2017

How to checked checkbox in JSP from a JPA Manytomany

I have 2 tables Book and Categories, 1 book can have many categories, and 1 category can have many books. So I create a table name book_category(have book_id and category_id) to hold the data.

I use JPA, in BookEntity I have

private Set<CategoryEntity> categoryEntityList = new HashSet<>();
@ManyToMany(cascade = CascadeType.MERGE)
@JoinTable(name = "book.category_book", joinColumns = {@JoinColumn(name = "book_id")},inverseJoinColumns = {@JoinColumn(name = "category_id")})

Insert new book are okay, for example a book "How to fly" can be in Science and Entertainment. Now I want it to display in checkbox and have prechecked value when clicked to each book, like the image below: example checked checkbox

Here I tried

<c:forEach items="${dsCategory}" var="categoryList" >
        <c:forEach items="${bookResult.getCategoriesList()}" var="getCategoryID" >
            <input type="checkbox" name="checkbox_category" value="${categoryList.categoryId}" ${getCategoryID.categoryId == '${categoryList.categoryId}' ? 'checked' : ''}/> ${categoryList.categoryName}
        </c:forEach>

    </c:forEach>

but not success, it doubled the result but not checked: example checkbox Note: ${dsCategory} is a value I setAttribute in servlet to show all categories bookResult is to show all the attribute when click to each book (name, author, ...) and it has a list of categories for that book (getCategoriesList() ).




Aucun commentaire:

Enregistrer un commentaire