我使用的是素数面和dataTable(惰性模式),我需要在代码中生成dataTables的定义。我能够构建dataTable、列、标签、值。。。但排序不起作用。
我试过了:
column.setSortBy("name"); // where name is name of property/column
column.setSortBy("#{row['name']}"); // where row is map and name is name of property/column - this is what im using in XML version and it working very well
column.setSortBy(ef.createValueExpression(context, "#{row['name']}", Object.class));
DataTable看起来像是禁用了排序(标签上没有箭头)。。。
当我在XML中有定义时,一切都很好(这与lazyDataModel或模型层中的任何其他东西无关)。
试试这个代码
FacesContext context = FacesContext.getCurrentInstance();
ExpressionFactory expressionFactory = context.getApplication().getExpressionFactory();
ValueExpression valueExpression = expressionFactory.createValueExpression(context.getELContext(), "#{row['name']}", String.class);
column.setValueExpression("sortBy", valueExpression);
其中我使用了String.class
,因为我假设"#{row['name']}"计算为类String
的实例。