在加载惰性 p:dataTable 时选择第一行

问题描述 投票:0回答:3

需要一些帮助选择 PrimeFaces

p:dataTable
页面加载时的第一行,这是到目前为止我的代码:

<p:dataTable id="dtbList" value="#{Controller.items}" var="item" widgetVar="dataTableList" lazy="true" 
selection="#{Controller.selectedValue}" rowKey="#{item.key}" 
scrollable="true" scrollHeight="133">

<p:ajax event="rowSelectRadio" listener="#{Controller.handleSelectList}" update="dtbList" 
oncomplete="resetScroll();"/>
...
...
...
</p:dataTable>

我尝试使用ajax事件没有成功

<p:ajax event="page" onsuccess="PF('dataTableList').selectRow(0);"/>

想知道我哪里做错了。

jsf primefaces primefaces-datatable
3个回答
0
投票

如果您知道第一行的 rowKey(id),请尝试在 dataTable 组件中添加属性 dynamic="true",如下所示:

<p:dataTable id="dtbList"
value="#{Controller.items}" var="item"
widgetVar="dataTableList" lazy="true"
selection="#{Controller.selectedValue}"
rowKey="#{item.key}"
scrollable="true" scrollHeight="133"
dynamic="true">
...
...
...
</p:dataTable>

并且,在你的bean(控制器)中创建一个由@PostConstruct注释的方法,这样你就可以选择最好的元素。 像这样:

@PostConstruct
public void init() {
    this.selectedValue = new SelectedValueEntityType();
    this.selectedValue.setKey('yourKey');
}

0
投票

page
事件是在分页时触发的,因此不会在初始加载时触发。

您可能希望使控制器成为您已实现的

LazyDataModel
的属性(假设您这样做了;如果您正在使用
JpaLazyDataModel
,请扩展它)。现在您可以通过
load
方法访问控制器。现在,您可以简单地检查是否未进行选择,并将在
load
方法中获取的第一个项目设置为控制器的选定项目。

另请参阅:https://primefaces.github.io/primefaces/12_0_0/#/components/datatable


0
投票

我之前也遇到过同样的问题。

首先,您需要以编程方式构建

datatable
组件。

例如,backbean 将类似于以下内容

public class Controller {
    private DataTable dataTable;
    private CustomLazyDataModel customLazyDataModel;

    @PostConstruct
    public void init() {
        ExpressionFactory exFactory = FacesContext.getCurrentInstance().getApplication().getExpressionFactory();
        ELContext elContext = FacesContext.getCurrentInstance().getApplication().getELContext();

        dataTable = new DataTable();
        dataTable.setId("dtbList");
        dataTable.setValueExpression("value", exFactory.createValueExpression(elContext,"#{Controller.customLazyDataModel}", CustomLazyDataModel.class));
    
        // This for selecting first row.
        dataTable.setSelection(new YourPOJOClass[]{customLazyDataModel.getDatasource().get(0)}); 
    }
}

这是您的自定义惰性数据模型的示例

@Getter
@Setter
public class CustomLazyDataModel extends LazyDataModel<YourPOJOClass> {
    // the loaded data source
    private List<YourPOJOClass> datasource;
    @Getter(AccessLevel.NONE)
    @Setter(AccessLevel.NONE)
    private boolean buildScreenPhase = false;

    // use this initialization function to create an instance of lazy
    // data model
    public static CustomLazyDataModel forBuildScreen() {
        CustomLazyDataModel customLazyDataModel = new CustomLazyDataModel();
        // pass needed data instead of null
        customLazyDataModel.load(0, 7, null, null, null);
        customLazyDataModel.buildScreenPhase = true;
        return customLazyDataModel;
    }

    @Override
    public List<RowModel> load(int first, int pageSize, String sortField,
                      SortOrder sortOrder, Map<String, String> filters) {
        // Your loading logic here from db or something else

        // to enhance loading data performance we will use
        // buildScreenPhase flag
        
        if (!buildScreenPhase) {
            // load data
        } else {
            buildScreenPhase = false;
        }
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.