1

我想要一列复选框来选择多行。我设法在每一行上创建了一个复选框,但我还希望在标签上有一个复选框,以便当我选中标题上的复选框时它会自动选择列上的所有复选框。我怎么能做到这一点?

PS:我使用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>
4

1 回答 1

0

您可以使用<datatables:columnHead>标签来自定义标题列。

<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>

位于<datatables:columnHead>标签内的所有内容都将放置在相应的列标题中。

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

于 2015-05-17T23:03:05.230 回答