我搜索了很多问题,尝试了很多东西。下面我有产生问题的片段步骤。首先对任何列应用过滤器值,然后对表界面上存在的任何列过滤器值应用排序,但在加载方法时,filters.isEmpty() 为真。
<p:dataTable id="tbl" value="#{service}"
selection="#{bean.selectedObj}"
filteredValue="#{bean.listFilteredObj}"
var="obj"
multiViewState="true"
rows="50"
widgetVar="tbl1" filterEvent="keyup"
draggableColumns="true"
emptyMessage="No Obj found"
paginator="true"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="50,100"
paginatorAlwaysVisible="true"
rowKey="#{opp.oppId}"
tableStyle="table-layout:auto;" resizableColumns="true" resizeMode="expand"
rowSelectMode="add"
lazy="true"
rowIndexVar="index"
class="fixed-scrollbar"
style="margin: -10px!important; height: 85vh">
<p:ajax event="rowSelectCheckbox" listener="#{service.onRowSelectCheckbox}" update=":dtForm:tbl:btnClose :dtForm:tbl:btnUpdate" />
<p:ajax event="rowUnselectCheckbox" listener="#{service.onRowUnselectCheckbox}" update=":dtForm:tbl:btnClose :dtForm:tbl:btnUpdate" />
<p:ajax event="toggleSelect" listener="#{service.onToggleSelect}" update=":dtForm:tbl:btnClose :dtForm:tbl:btnUpdate" />
<p:ajax event="page" listener="#{objFilter.onPageChange}" />
<p:ajax event="page" listener="#{service.onPagination}" update=":dtForm:tbl"/>
<p:ajax event="colReorder" listener="#{service.onColumnReorder}"/>
<p:column exportable="false" toggleable="false" selectionMode="multiple" style="width: 10px;text-align: center;"/>
<p:column headerText="Id" style="display: none" exportable="true" visible="false" toggleable="false">
<p:outputLabel value="#{obj.Id}" />
</p:column>
<c:forEach var="column" items="#{service.listOppColumns}">
<p:column headerText="#{column.columnHeader}" id="#{column.columnName}"
visible="#{column.columnVisibleFlag==1}"
filterMatchMode="#{column.columnFilterMatchMode}"
filterBy="#{column.columnName=='insDate' || column.columnName=='updDate' ? globalParams.formatDateTime(obj[column.columnName], 'dt') : column.columnName=='oppFollowUp' ? globalParams.formatDateTime(obj[column.columnName], 'da') : opp[column.columnName]}"
sortBy="#{obj[column.columnName]}" exportable="#{column.columnVisibleFlag==1}"
field="#{column.columnName}">
<c:choose>
<c:when test="#{column.columnName=='objFol'}">
<h:link outcome="ObjView.xhtml?id=#{obj.Id}" value="#{globalParams.formatDateTime(opp[column.columnName],'da')}">
<f:param name="frm" value="#{view.viewId}" />
<f:param name="moduleId" value="0" />
</h:link>
</c:when>
<c:when test="#{column.columnName=='objDate'}">
<h:link outcome="objView.xhtml?id=#{obj.objId}" value="#{globalParams.formatDateTime(obj[column.columnName],'da')}">
<f:param name="frm" value="#{view.viewId}" />
<f:param name="moduleId" value="0" />
</h:link>
</c:when>
<c:when test="#{column.columnName=='objPro'}">
<h:link outcome="ObjView.xhtml?id=#{obj.Id}" value="#{globalParams.formatDateTime(opp[column.columnName],'da')}">
<f:param name="frm" value="#{view.viewId}" />
<f:param name="moduleId" value="0" />
</h:link>
</c:when>
<c:when test="#{column.columnName=='insDate'}">
<h:link outcome="ObjView.xhtml?id=#{obj.Id}" value="#{globalParams.formatDateTime(opp[column.columnName],'dt')}">
<f:param name="frm" value="#{view.viewId}" />
<f:param name="moduleId" value="0" />
</h:link>
</c:when>
<c:when test="#{column.columnName=='updDate'}">
<h:link outcome="OpportunityView.xhtml?opid=#{opp.oppId}" value="#{globalParams.formatDateTime(opp[column.columnName],'dt')}">
<f:param name="frm" value="#{view.viewId}" />
<f:param name="moduleId" value="0" />
</h:link>
</c:when>
<c:otherwise>
<h:link outcome="ObjView.xhtml?id=#{obj.Id}"
value="#{obj[column.columnName]}" >
<f:param name="frm" value="#{view.viewId}" />
<f:param name="moduleId" value="0" />
</h:link>
</c:otherwise>
</c:choose>
</p:column>
</c:forEach>
</p:dataTable>
服务.java
...
...
...
@Override
public List<ViewObjList> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
globalFlt = new StringBuilder("");
StringBuilder order = new StringBuilder("");
StringBuilder tableFlt;
tableFlt = tableFlt(filters);
System.out.println("\nfilter is null = "+filters.isEmpty());
if (sortField != null) {
if (!sortField.trim().equals("")) {
order.append(sortField);
if (sortOrder.equals(SortOrder.DESCENDING)) {
order.append(" DESC");
}
}
} else {
order.append("objFol DESC");
}
obj.list = lazyQueryExecute(first, pageSize, globalFlt.toString(), tableFlt.toString(), order.toString());
obj.listFiltered = obj.list;
return obj.listFiltered;
}
private List<ViewOppList> lazyQueryExecute(int first, int pageSize, String globalFlt, String tableFlt, String order) {
StringBuilder queryList = new StringBuilder("from ViewOppList ");
String and = "and (";
queryList.append("where (").append(DepList()).append(") ");
if (!tableFlt.trim().equals("")) {
queryList.append(and).append(tableFlt).append(") ");
}
if (!globalFlt.trim().equals("")) {
queryList.append(and).append(globalFlt).append(") ");
}
String rowCount = "select count(*) " + queryList;
long rowC = (long) dao.qryObject(rowCount);
setRowCount((int) rowC);
if (!order.trim().equals("")) {
queryList.append(" order by ").append(order);
}
showTotal.append(queryList);
opp.setOppSubTotal((BigDecimal) dao.qryObject(showTotal.toString()));
System.out.println("query = " + queryList);
return dao.qryListMaxLim(queryList.toString(), Dao.query.HQL, first, pageSize);
}
@Override
public ViewObjList getRowData(String rowKey) {
if (obj.listFiltered != null) {
for (ViewObjList objo : obj.listFiltered) {
if (objo.getId() == Integer.parseInt(rowKey)) {
return objo;
}
}
}
return null;
}
@Override
public String getRowKey(ViewObjList obj) {
return String.valueOf(obj.getId());
}
private StringBuilder tableFlt(Map<String, Object> filters) {
StringBuilder tableFlt = new StringBuilder("");
if (!filters.isEmpty()) {
for (Map.Entry<String, Object> entry : filters.entrySet()) {
String field = entry.getKey();
String value = (String) entry.getValue();
if (!field.trim().equals("") && !value.trim().equals("")) {
if (field.equalsIgnoreCase("globalFilter")) {
globalFlt.append(globalSearch(value));
} else {
// colmn LIKE '%value% and column1 Like '%value1%'
tableFlt.append(field).append(" LIKE '%").append(value).append("%' and ");
}
System.out.println("\n\n\n field = " + field + ": vlue = " + value);
}
}
if (tableFlt.length() > 3) {
tableFlt.delete(tableFlt.length() - 4, tableFlt.length());
}
}
return tableFlt;
}
我只展示了重现问题所需的小片段。在此代码中,我们首先应用过滤器,该过滤器将显示正确的查询列表(filters.isEmpty() = false),但在记录过滤器后,在加载方法中从表中应用排序时,即使过滤值出现在数据表中。我正在使用 prime faces 版本 6.2 和 jsf 2.2 并使用 hql 查询从 MySQL 数据表中获取数据
任何帮助将不胜感激在此先感谢。