【问题标题】:Can't set initial sort order for DataTable programmatically无法以编程方式为 DataTable 设置初始排序顺序
【发布时间】:2015-04-04 22:50:20
【问题描述】:

我需要通过 bean 以编程方式为 DataTable 设置 sortBy。 DataTable 具有单一排序模式、静态列和自定义惰性模型,可按自定义表达式排序,如 = "#{contractor.companyName-MULTY_LANG}"。我尝试了很多方法来做到这一点,只有一种方法有效(下面的代码),但不能正常工作 - 组件显示时没有突出显示有序字段。谁能回答如何以编程方式为 DataTable 设置 sortBy 的正确方法?

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

我将不胜感激。


Primefaces 5.1。 Tomcat 7. Mojarra 2.2.8

【问题讨论】:

  • 您不能以首选的默认顺序从数据库中读取它吗?它不会突出显示该列,但我认为它工作正常且直观
  • 首先尝试 PF 6.1(你真的需要它以编程方式完成吗?你不能使用 xhtml 吗?
  • @Kukeltje > 你不能使用 xhtml 吗? => 我们做了一个dataTable.reset(); 来清除所有字段过滤器和其他工作人员@Kukeltje > 首先尝试 PF 6.1 => 我们正在使用这个版本
  • 我没看到"We did dataTable.reset(); to clear all field filters and other staff"与"Can't you use xhtml?"相关。你想实现什么(除了现在的排序问题)不能使用 xhtml 完成?
  • 确实,XHTML 中的sortOrder="#{bean.sortOrder}" 有什么问题?

标签: jsf jsf-2 primefaces datatable


【解决方案1】:

首先扩展 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);
    }
}

然后尝试扩展 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);
        }
    }
}

此外,将渲染类添加到 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>

希望对你有帮助

【讨论】:

  • 这是哪个 PF 版本? 5.1?
  • 我使用的版本也是PF 5.1。通过使用这个扩展类并覆盖 LazyDataModel 的加载函数,您可以编辑和操作数据表和数据模型的所有属性。我使用上述类不仅用于排序,还用于以编程方式修改数据表的其他属性
猜你喜欢
  • 1970-01-01
  • 2010-09-18
  • 2012-12-01
  • 2012-09-20
  • 2014-05-28
  • 1970-01-01
  • 2011-03-21
  • 1970-01-01
  • 2019-10-16
相关资源
最近更新 更多