我需要通过bean以编程方式为DataTable设置sortBy.DataTable具有单一排序模式,静态列和自定义延迟模型,可按自定义表达式排序,例如="#{contractor.companyName-MULTY_LANG}".我尝试了很多方法来做到这一点,只有一种方法可行(下面的代码),但工作不正确 - 组件显示没有突出显示有序字段.任何人都可以回答以编程方式为DataTable设置sortBy的正确方法吗?
@PostConstruct
public void init() {
final DataTable d = (DataTable) FacesContext.getCurrentInstance().getViewRoot().findComponent(TABLE_COMPONENT_KEY);
String tableSortByExpression = "#{contractor.companyName-MULTY_LANG}"; // expression from some column
SortMeta sm1 = new SortMeta();
for (UIComponent child : d.getChildren()) {
Column column = (Column)child;
ValueExpression columnSortByVe = column.getValueExpression("sortBy");
if (columnSortByVe != null) {
String columnSortByExpression = columnSortByVe.getExpressionString();
if (tableSortByExpression != null && tableSortByExpression.equals(columnSortByExpression)) {
d.setSortColumn(column);
d.setSortOrder("ASCENDING");
break;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我会感激任何帮助.
Primefaces 5.1.Tomcat 7. Mojarra 2.2.8