12
votes

I need to set sortBy for DataTable programmatically by bean. DataTable has single sort Mode, static columns, and custom lazy model to sort by custom expressions like = "#{contractor.companyName-MULTY_LANG}". I tried a lot of ways to do this and only one way works (code below), but works not correctly - Component showed without highlight ordered field. Can anybody answer how the correct way to set sortBy for DataTable programmatically?

@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;
                }
            }
        }
    }

I'll be appreciate for any help.


Primefaces 5.1. Tomcat 7. Mojarra 2.2.8

1
Can't you read it from the DB in the preferred default order? It won't highlight the column, but I think it works fine and intuitivelyJaqen H'ghar
Try PF 6.1 first (and you you actually need it be done programmatically? Can't you use xhtml?Kukeltje
@Kukeltje > Can't you use xhtml? => We did a dataTable.reset(); to clear all field filters and other staff @Kukeltje > Try PF 6.1 first => We are using this versionIgnacio Rubio
I fail to see where "We did dataTable.reset(); to clear all field filters and other staff" is related to "Can't you use xhtml?" What are you trying to achieve (besides the sorting problem now) that cannot be done using xhtml?Kukeltje
Indeed, what's wrong with sortOrder="#{bean.sortOrder}" in XHTML?BalusC

1 Answers

2
votes

First of all extend org.primefaces.component.datatable.feature.SortFeature

public class CustomSortFeature extends SortFeature {
    @SuppressWarnings("rawtypes")
    @Override
    public void encode(FacesContext context, DataTableRenderer renderer, DataTable table) throws IOException {
        table.setFirst(0);

        if(table.isLazy()) {
            table.loadLazyData();
            singleSort(context, table);
        }
        else {
            if(table.isMultiSort())
                multiSort(context, table);
            else
                singleSort(context, table);

            if(table.isPaginator()) {
                RequestContext requestContext = RequestContext.getCurrentInstance();

                if(requestContext != null) {
                    requestContext.addCallbackParam("totalRecords", table.getRowCount());
                }
            }

            //save state
            Object filteredValue = table.getFilteredValue();
            if(!table.isLazy() && table.isFilteringEnabled() && filteredValue != null) {
                table.updateFilteredValue(context, (List) filteredValue);
            }
        }

        renderer.encodeTbody(context, table, true);
    }
}

Then try to extend org.primefaces.component.datatable.DataTableRenderer

@FacesRenderer(componentFamily = "org.primefaces.component", rendererType = "org.primefaces.component.DataTableRenderer")
public class CustomDataTableRenderer extends DataTableRenderer {
    public List<DataTableFeatureKey> FEATURE = new ArrayList<DataTableFeatureKey>(){/**
         * 
         */
        private static final long serialVersionUID = 1L;

    {
        add(DataTableFeatureKey.DRAGGABLE_COLUMNS);
        add(DataTableFeatureKey.FILTER);
        add(DataTableFeatureKey.PAGE);
        add(DataTableFeatureKey.SORT);
        add(DataTableFeatureKey.RESIZABLE_COLUMNS);
        add(DataTableFeatureKey.SELECT);
        add(DataTableFeatureKey.ROW_EDIT);
        add(DataTableFeatureKey.CELL_EDIT);
        add(DataTableFeatureKey.ROW_EXPAND);
        add(DataTableFeatureKey.SCROLL);
        add(DataTableFeatureKey.DRAGGABLE_ROWS);
    }};
    @Override
    protected void preRender(FacesContext context, DataTable table){
        table.setCaseSensitiveSort(false);
        if(table.isLazy()) {
            if(table.isLiveScroll())
                table.loadLazyScrollData(0, table.getScrollRows());
            else
                table.loadLazyData();
        }

        boolean defaultSorted = (table.getValueExpression("sortBy") != null || table.getSortBy() != null);
        if(defaultSorted && !table.isLazy()) {
            table.setDefaultSortByVE(table.getValueExpression("sortBy"));
            table.setDefaultSortOrder(table.getSortOrder());
            table.setDefaultSortFunction(table.getSortFunction());

            CustomSortFeature sortFeature = (CustomSortFeature) table.getFeature(DataTableFeatureKey.SORT);

            if(table.isMultiSort())
                sortFeature.multiSort(context, table);
            else
                sortFeature.singleSort(context, table);  

            table.setRowIndex(-1);
        }

        if(table.isPaginator()) {
            table.calculateFirst();
        }

        Columns dynamicCols = table.getDynamicColumns();
        if(dynamicCols != null) {
            dynamicCols.setRowIndex(-1);
        }
    }

    @Override
    public void encodeEnd(FacesContext context, UIComponent component) throws IOException{
        DataTable table = (DataTable) component;
        if(table.shouldEncodeFeature(context)) {
            for(Iterator<DataTableFeatureKey> it = FEATURE.iterator(); it.hasNext();) {
                DataTableFeature feature = table.getFeature(it.next());

                if(feature.shouldEncode(context, table)) {
                    feature.encode(context, this, table);
                }
            }
        }
        else {  
            preRender(context, table);

            encodeMarkup(context, table);
            encodeScript(context, table);
        }
    }
}

Furthermore, add render class to faces-config

<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">
        <render-kit>
            <renderer>
                <component-family>org.primefaces.component</component-family>
                <renderer-type>org.primefaces.component.DataTableRenderer</renderer-type>
                <renderer-class>com.edtoktay.CustomDataTableRenderer</renderer-class>
            </renderer>
        </render-kit>
</faces-config>

Hope this helps