Sorting is not working in datatable in PrimeFaces?

后端 未结 7 2447
感情败类
感情败类 2020-12-15 07:13

Sorting is not working in datatable in PrimeFaces. Please suggest.

See below my .xhtml file



  

        
7条回答
  •  天涯浪人
    2020-12-15 07:23

    I don't know if it's your case. There's a primefaces issue. A bug on sort of datatable. Take a look here: https://code.google.com/archive/p/primefaces/issues/2476

    There are some workarounds:

    1. you can use a lazyDataModel for your datatable, it works.

    2. you can force the sort order.

    In your datatable catch the sort event

    
    
    

    In your managed bean you set a order by string to add to your query

    private String orderBy = "";
    
    public void sortListener(SortEvent event) {
        String orderColumn = event.getSortColumn().getValueExpression("sortBy").getExpressionString();
    
        //you will get the content of the attribute SortBy of the column you clicked on, like #{entry.carno}
        orderColumn = orderColumn.replace("#{entry.", "");
        orderColumn = orderColumn.replace("}", "");
        orderBy = " order by " + orderColumn + (event.isAscending()? " asc " : " desc ");
    }
    
    public List getList(){
        String query = "[...your query...]" + orderBy;
        ...[execute your query and get your ordered list]
    }
    

提交回复
热议问题