p:datatable loses sort column and order after ajax refresh

后端 未结 8 1631
攒了一身酷
攒了一身酷 2020-12-17 19:38

I have a button on a page that causes my data table to refresh via an AJAX request. Something like this:




        
相关标签:
8条回答
  • 2020-12-17 20:26

    I wrote an extension to @truemmer's solution. His reverts the sorting order back to the default, where as mine will reverts to the previous sort the user selected.

    function postAjaxSortTable(datatable)
    {
        var selectedColumn = datatable.jq.find('.ui-state-active');
        if(selectedColumn.length <= 0)
        {
            return;
        }
        var sortorder = "ASCENDING";
        if(selectedColumn.find('.ui-icon-triangle-1-s').length > 0)
        {
            sortorder = "DESCENDING";
        }
        datatable.sort(selectedColumn, sortorder);
    }
    

    Updating the same table as truemmer's works like this:

    <p:commandButton value="refresh" action="#{tableController.refreshPrices}" update="myTable" oncomplete="postAjaxSortTable(myTableWidget)" />
    

    EDIT: Primefaces 4.0 MultiSort support

    function postAjaxSortTable(datatable) {
        var selectedColumn = undefined;
    
        // multisort support
        if(datatable && datatable.cfg.multiSort) {
            if(datatable.sortMeta.length > 0) {
                var lastSort = datatable.sortMeta[datatable.sortMeta.length-1];
                selectedColumn = $(document.getElementById(lastSort.col));
            }
        } else {
            selectedColumn = datatable.jq.find('.ui-state-active');
        }
    
        // no sorting selected -> quit
        if(!selectedColumn || selectedColumn.length <= 0) {
            return;
        }
    
        var sortorder = selectedColumn.data('sortorder')||"DESCENDING";
        datatable.sort(selectedColumn, sortorder, datatable.cfg.multiSort);
    }
    
    0 讨论(0)
  • 2020-12-17 20:31

    First of all, there is an open ticket on the PrimeFaces Issue Tracker, which targets this question but was recently labeled as "won't fix".

    Nevertheless, I found a nice workaround. The sorting of PrimeFaces tables can be triggered by calling an undocumented JavaScript method on the datatable's widget. The following might not work for future releases of PrimeFaces, but it does for 3.4.2:

    Just add the following to your component, which triggers the update:

    oncomplete="myTableWidget.sort($(PrimeFaces.escapeClientId('#{p:component('sortColumnId')}')), 'ASCENDING')"
    

    The table might look like this:

    <p:dataTable id="myTable"
                 widgetVar="myTableWidget"
                 value="#{myArticles}"
                 var="article"
                 sortBy="#{article.price}"
                 sortOrder="ASCENDING">
        ...
    
        <p:column id="price" sortBy="#{article.price}">
            <f:facet name="header">
                <h:outputText value="Price" />
            </f:facet>
            <h:outputText value="#{article.price}" />
        </p:column>
    
    </p:dataTable>
    

    So updating the table could work like this.

    <p:commandButton value="refresh" action="#{tableController.refreshPrices}" update="myTable" oncomplete="myTableWidget.sort($(PrimeFaces.escapeClientId('#{p:component('price')}')), 'ASCENDING')" />
    
    0 讨论(0)
提交回复
热议问题