如何从JSF数据表中的selectOneMenu中获取值? [英] How can I get values out of a selectOneMenu inside a JSF datatable?

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

问题描述

我有一个JSF数据表,它有一堆行,每行都有一个selectOneMenu,如下所示:

  < h:form 
< h:dataTable id =myTable
binding =#{myBean.dataTable}
value =#{myBean.dataTableRows}var =行
first =0rows =0dir =LTRframe =hsidesrules =all>

< h:column>
< f:facet name =header>
< h:outputText value =样品名称/>
< / f:facet>
< h:outputText value =#{row.sampleName}/>
< / h:column>

< h:column>
< f:facet name =header>
< h:outputText value =Role/>
< / f:facet>
< h:selectOneMenu value =#{row.role}
id =roleInput>
< f:selectItems value =#{myBean.allRoles}/>
< / h:selectOneMenu>
< / h:column>

< / h:dataTable>

< h:commandButton value =保存action =#{myBean.save}/>
< / h:form>

然而,我似乎无法弄清楚如何将选定的角色从保存方法。换句话说,我想保存每一行的值。我看到这篇文章,它解释了如何保存输入文本框:



http://balusc.blogspot.com/2006/06/using-datatables.html#EditableDatatable



但它似乎不适用于h:selectOneMenu情况。有没有人有这样的代码?



谢谢!

解决方案

p>我看到你的表具有绑定到你的bean。在你的bean中,你可以使用getDataTable()方法并访问它。 Java doc说:

  public Object getRowData()

返回表示数据的数据对象当前选择的行索引(如果有)。

所以如果你的代码如下:

 列表< String> selectedRowData =(List< String>)getDataTable()。getRowData()

然后可以访问所有用户选择的字段我使用这个在我自己的项目和它的工作。唯一的区别是Im转换为我自己的类型,而不是列表< String>


I have a JSF datatable with a bunch of rows, with each row having a selectOneMenu inside of it like this:

    <h:form 
      <h:dataTable id="myTable"
        binding="#{myBean.dataTable}"
        value="#{myBean.dataTableRows}" var="row"
        first="0" rows="0" dir="LTR" frame="hsides" rules="all">

        <h:column>
          <f:facet name="header">
            <h:outputText value="Sample Name" />
          </f:facet>
          <h:outputText value="#{row.sampleName}" />
        </h:column>

        <h:column>
          <f:facet name="header">
            <h:outputText value="Role" />
          </f:facet>
          <h:selectOneMenu value="#{row.role}"
            id="roleInput">
            <f:selectItems value="#{myBean.allRoles}" />
          </h:selectOneMenu>
        </h:column>

      </h:dataTable>

      <h:commandButton value="Save" action="#{myBean.save}" />
    </h:form>

However, I can't seem to figure out how get the selected role out of each row in the save method. In other words, I want to save each row's value. I saw this article, which explains how to save an input text box:

http://balusc.blogspot.com/2006/06/using-datatables.html#EditableDatatable

but it doesn't seem to apply to the h:selectOneMenu case. Does anyone have any example code that does this?

Thanks!

解决方案

I see your table has binding to your bean. In your bean you can use the getDataTable() method and access it. Java doc says:

public Object getRowData()

Return the data object representing the data for the currently selected row index, if any.

So if you do your code like:

List<String> selectedRowData = (List<String>) getDataTable().getRowData()

You can then access all the fields the user has chosen. Im using this in my own project and its working. The only difference is that Im casting to my own type instead of List<String>

这篇关于如何从JSF数据表中的selectOneMenu中获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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