如何在JSF数据表中显示行索引 [英] How to display the row index in a JSF datatable

查看:126
本文介绍了如何在JSF数据表中显示行索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 列A列中的行索引B 
1 xxx
2 yyy

我以为可以使用隐式el变量,如#{rowIndex},但是这不起作用。



我发现一个解决方案是为数据表创建绑定,并使用如下绑定:

 < h:dataTable var =itemvalue =#{controller.items}binding =#{controller.dataTable} > 
< h:column>#{controller.dataTable.rowIndex}< / h:column>
< h:column> value< / h:column>
< / h:dataTable>

但是,这个解决方案是复杂的,当我在页面中有很多嵌套数据表时,效果不佳。



任何想法如何以更好的方式解决这个问题?

解决方案

现有的解决方案并没有把我当成一个坏的。只要您引用了嵌套表的模型,rowIndex就可以在嵌套表中使用。

 < h:dataTable border =1value =#{nestedDataModel}var =nested> 
< h:column>
< h:dataTable border =1value =#{nested}var =item>
< h:column>
< h:outputText value =#{nested.rowIndex}/>
< / h:column>
< h:column>
< h:outputText value =#{item}/>
< / h:column>
< / h:dataTable>
< / h:column>
< / h:dataTable>

示例模型:

  public class NestedDataModel extends DataModel implements Serializable {

private List< List< String>> nestedDataModel = populateModel();
private int index;

私人列表<列表< String>> populateModel(){
列表<列表< String>> list = new ArrayList< List< String>>(); (int x = 0; x <3; x ++)
{
列表< String> nestedTableData = new ArrayList< String>(); (int y = 0; y <3; y ++){
nestedTableData.add(Foo x =+ x +y =+ y)
}
list.add(nestedTableData);
}
返回列表;
}

@Override
public int getRowCount(){
return nestedDataModel.size();
}

@Override
public Object getRowData(){
列表< String> list = nestedDataModel.get(index);
返回新的ListDataModel(list);
}

@Override
public int getRowIndex(){
return index;
}

@Override
public Object getWrappedData(){
return nestedDataModel;
}

@Override
public boolean isRowAvailable(){
return index> = 0&&指数nestedDataModel.size();
}

@Override
public void setRowIndex(int arg0){
index = arg0;
}

@Override
public void setWrappedData(Object arg0){
throw new UnsupportedOperationException();
}

}

嵌套数据表一般应该避免 - 如果你不小心(例如使他们成为一个表单的孩子),这可以导致一个O(N ^ 2)遍历表中的每个生命周期阶段的孩子(在生命周期中有6个阶段) )






对于模型外部的东西,可以在受管Bean中使用一个简单的计数器: p>

  public class RowCounter实现Serializable {

private transient int row = 0;

public int getRow(){
return ++ row;
}

}

配置:

 < managed-bean> 
< managed-bean-name> rowCounter< / managed-bean-name>
< managed-bean-class> tablerows.RowCounter< / managed-bean-class>
< managed-bean-scope>请求< / managed-bean-scope>
< / managed-bean>

查看:

 code>< f:view> 
< h:dataTable border =1value =#{tableDataBean.tableDataModel}
var =rowBean>
< h:column>
< h:outputText value =#{rowCounter.row}/>
< / h:column>
< h:column>
< h:outputText value =#{rowBean}/>
< / h:column>
< / h:dataTable>
< / f:view>

这是因为bean是请求范围并绑定到表单外的只读控件。它不会在嵌套的dataTable中工作,除非你希望行计数器对视图是全局的。但是,我不相信行索引应该是视图的一个函数。



对于嵌套的dataTable,你最好提供行索引行豆。如果您决定做数据集分页,则可以让您更有控制力。


In a JSF dataTable I want to display the row index next to the rows... like:

Column A    Column B
1           xxx
2           yyy

I thought that I could use an implicit el variable like #{rowIndex} but this is not working.

A solution I found is to create a binding for the data table and use the binding like:

<h:dataTable var="item" value="#{controller.items}" binding="#{controller.dataTable}">
    <h:column>#{controller.dataTable.rowIndex}</h:column>
    <h:column>value</h:column>
</h:dataTable>

but this solution is complex and doesn't work well when I have many nested dataTables in a page.

Any ideas on how to solve this in a better way?

解决方案

The existing solution does not strike me as a bad one. The rowIndex should work in nested tables so long as you're referencing the model of the nested table.

    <h:dataTable border="1" value="#{nestedDataModel}" var="nested">
        <h:column>
            <h:dataTable border="1" value="#{nested}" var="item">
                <h:column>
                    <h:outputText value="#{nested.rowIndex}" />
                </h:column>
                <h:column>
                    <h:outputText value="#{item}" />
                </h:column>
            </h:dataTable>
        </h:column>
    </h:dataTable>

Sample model:

public class NestedDataModel extends DataModel implements Serializable {

    private List<List<String>> nestedDataModel = populateModel();
    private int index;

    private List<List<String>> populateModel() {
        List<List<String>> list = new ArrayList<List<String>>();
        for(int x=0; x<3; x++) {
            List<String> nestedTableData = new ArrayList<String>();
            for(int y=0; y<3; y++) {
                nestedTableData.add("Foo x="+x+" y="+y);
            }
            list.add(nestedTableData);
        }
        return list;
    }

    @Override
    public int getRowCount() {
        return nestedDataModel.size();
    }

    @Override
    public Object getRowData() {
        List<String> list = nestedDataModel.get(index);
        return new ListDataModel(list);
    }

    @Override
    public int getRowIndex() {
        return index;
    }

    @Override
    public Object getWrappedData() {
        return nestedDataModel;
    }

    @Override
    public boolean isRowAvailable() {
        return index >= 0 && index < nestedDataModel.size();
    }

    @Override
    public void setRowIndex(int arg0) {
        index = arg0;
    }

    @Override
    public void setWrappedData(Object arg0) {
        throw new UnsupportedOperationException();
    }

}

Nesting dataTables should generally be avoided - if you're not careful (e.g. make them children of a form), this can lead to a O(N^2) pass over the table children for each phase of the lifecycle on a submit (and there are 6 phases in the lifecycle).


For something that is external to the model, you could use a simple counter in a managed bean:

public class RowCounter implements Serializable {

    private transient int row = 0;

    public int getRow() {
        return ++row;
    }

}

Config:

<managed-bean>
    <managed-bean-name>rowCounter</managed-bean-name>
    <managed-bean-class>tablerows.RowCounter</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

View:

<f:view>
    <h:dataTable border="1" value="#{tableDataBean.tableDataModel}"
        var="rowBean">
        <h:column>
            <h:outputText value="#{rowCounter.row}" />
        </h:column>
        <h:column>
            <h:outputText value="#{rowBean}" />
        </h:column>
    </h:dataTable>
</f:view>

This works because the bean is request-scope and bound to a read-only control outside a form. It would not work in a nested dataTable unless you wanted the row counter to be global to the view. But then, I'm not convinced that the row index should be a function of the view.

For a nested dataTable, you would be better off providing the row index from the row bean. It gives you more control if you decide to do things like pagination over data sets too.

这篇关于如何在JSF数据表中显示行索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆