【问题标题】:dynamic dataTable and sortBy not working动态数据表和 sortBy 不起作用
【发布时间】:2014-11-06 01:01:52
【问题描述】:

我正在使用 primefaces 和 dataTable(惰性模式),我需要动态/在代码中生成 dataTables 的定义。我能够构建数据表、列、标签、值……但排序不起作用。

我试过了:

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 或模型层中的任何其他内容无关)。

【问题讨论】:

    标签: jsf primefaces


    【解决方案1】:

    试试这个代码

       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 类的一个实例。

    【讨论】:

    • setValueExpression 是解决方案!谢谢!最终的工作版本是column.setValueExpression("sortBy", ef.createValueExpression(context, "#{name}", Object.class));(表达式必须改成这个,不知道为什么,但它可以工作)
    猜你喜欢
    • 1970-01-01
    • 2014-11-06
    • 2011-07-25
    • 1970-01-01
    • 2017-03-02
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    相关资源
    最近更新 更多