javascriptjspcheckboxdatatablesdandelion

Dandelion Datatables checkbox on table header


I want to have a column for checkboxes to select multiple rows. I managed to create a checkbox on each row but i also want to have a checkbox on the tag so that it automatically selects all checkboxes on the column when i check the checkbox on the header. How could i achieve that?

PS: Im using JSP

<form id="form">
    <div style="text-align:right; padding-bottom:1em;">
        <button type="submit">Submit form</button>
     </div>
    <datatables:table id="owners" data="${selections}" row="owner" theme="bootstrap2" 
                      cssClass="table table-striped" pageable="false" info="false" export="pdf">
        <datatables:column title="Name" cssStyle="width: 150px;" display="html">
            <spring:url value="/owners/{ownerId}.html" var="ownerUrl">
                <spring:param name="ownerId" value="${owner.id}"/>
            </spring:url>
            <a href="${fn:escapeXml(ownerUrl)}"><c:out value="${owner.firstName} ${owner.lastName}"/></a>
        </datatables:column>
        <datatables:column title="Name" display="pdf">
            <c:out value="${owner.firstName} ${owner.lastName}"/>
        </datatables:column>
        <datatables:column title="Address" property="address" cssStyle="width: 200px;"/>
        <datatables:column title="City" property="city"/>
        <datatables:column title="Telephone" property="telephone"/>
        <datatables:column title="Pets" cssStyle="width: 100px;">
            <c:forEach var="pet" items="${owner.pets}">
                <c:out value="${pet.name}"/>
            </c:forEach>
        </datatables:column>
        <datatables:column sortable="false" filterable="false">
            <input type="checkbox" name="check${owner.id}" value="${owner.id}">
        </datatables:column>
        <datatables:export type="pdf" cssClass="btn" cssStyle="height: 25px;" />
    </datatables:table>
</form>

Solution

  • You can use the <datatables:columnHead> tag to customize header columns.

    <form id="form">
        <div style="text-align:right; padding-bottom:1em;">
            <button type="submit">Submit form</button>
         </div>
        <datatables:table id="owners" data="${selections}" row="owner" theme="bootstrap2" 
                          cssClass="table table-striped" pageable="false" info="false" export="pdf">
            ...
            <datatables:column sortable="false" filterable="false">
                <datatables:columnHead>
                    <input type="checkbox" onclick="$('#owners').find(':checkbox').attr('checked', this.checked);" />
                </datatables:columnHead>
                <input type="checkbox" name="check${owner.id}" value="${owner.id}">
            </datatables:column>
            ...
        </datatables:table>
    </form>
    

    Everything located inside the <datatables:columnHead> tag will be placed in the corresponding column header.

    Reference: http://dandelion.github.io/datatables/docs/ref/jsp/columnhead.html