提问人:user1795370 提问时间:3/18/2015 更新时间:3/18/2015 访问量:738
JQuery 在表单中包装数据表时消失
JQuery disappears when wrapping datatable in a form
问:
我正在尝试创建一个在每行都有一个selectBooleanCheckbox的h:datatable,因此我将dataTable包装在h:form元素中。我的 dataTable 还需要支持分页和搜索。这工作正常,直到我将表包装在 h:form 中,此时所有用于分页和搜索的 JQuery 元素都消失了。我已将整个表格包装在一个表单中,而不仅仅是一列,因为页面上有提交按钮需要包装在与表格相同的表格中(据我所知)。
这是我的xhtml:
<h:form>
<h:dataTable value="#{bean.tableProperties()}"
var="property" styleClass="responsive small-table"
id="propertiesSelect">
<h:column headerClass="column10">
<f:facet name="header">Properties</f:facet>
<div class="medium-12 columns checkbox" align="right">
<h:selectBooleanCheckbox id="propertySelect"
value="#{bean.selectedProperties[property.propertyId]}">
</h:selectBooleanCheckbox>
<h:outputLabel for="propertySelect" class="">
<h:outputText value=""/>
</h:outputLabel>
</div>
</h:column>
<h:column headerClass="">
<f:facet name="header"></f:facet>
<span class="">
<c:if test="#{property.name != null}">
<span class="">#{property.name}</span>,
</c:if>
</span>
</h:column>
</h:dataTable>
<div class="row actions">
<div class="medium-6 columns">
<h:commandButton class="button radius secondary small expand cancel"
value="#{localization['global.button.cancel']}"
action="#{bean.exit}" immediate="true"/>
</div>
<div class="medium-6 columns">
<h:commandButton class="button radius small expand"
value="#{localization['global.button.continue']}"
action="#{bean.submit()}"/>
</div>
</div>
<f:event listener="#{bean.validateProperties}" type="postValidate" param=""/>
</h:form>
</div>
</div>
这是 JQuery:
$(document).ready(function() {
$("#propertiesSelect").DataTable({
searching: true,
"aLengthMenu": [
[5, 10, 15, -1],
[5, 10, 15, "All"]
],
"iDisplayLength": 10,
"bLengthChange": true,
"bPaginate": true,
"bSort": true,
"bInfo": true
});
});
因此,如果我删除表单,JQuery 可以工作,但 selectBooleanCheckboxes 和 commandButtons 不会,反之亦然。
我怎样才能做到这两点?
答:
你能检查你的HTML输出吗,表格是如何生成的,我的意思是表格的顺序是什么,它应该按照文档的建议
<table id="xyz">
<thead>
<th></th>
</thead>
<tbody></tbody>
</table>
确定顺序后,DataTables 将获取该表并根据启动转换为所需的表。此外,如果我在处理这样的要求时检查日志,我发现了一些线索。还有一件事,我不认为数据表具有搜索选项,如果您需要搜索选项,则可以使用或默认情况下DataTables具有过滤器选项。"bFilter":true,
桌子周围不需要表格。您可以简单地根据类名在jQuery中捕获按钮单击,并采取适当的操作。使用 Datatables API 从数据模型中获取所需的值。
或者,如果您选择在数据表周围使用表单(我不建议这样做),您将被迫在按钮单击事件(例如向上/向下分页按钮)上使用 event.preventDefault()。请记住,表单内的任何按钮单击都将提交该表单,这在分页按钮单击时不起作用!
评论