简体   繁体   English

如何从JSF数据表中的selectOneMenu中获取值?

[英]How can I get values out of a selectOneMenu inside a JSF datatable?

I have a JSF datatable with a bunch of rows, with each row having a selectOneMenu inside of it like this: 我有一个带有一堆行的JSF数据表,每行都有一个selectOneMenu,如下所示:

    <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. 但是,我似乎无法弄清楚如何从save方法中的每一行中获取所选角色。 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 http://balusc.blogspot.com/2006/06/using-datatables.html#EditableDatatable

but it doesn't seem to apply to the h:selectOneMenu case. 但它似乎不适用于h:selectOneMenu案例。 Does anyone have any example code that does this? 有没有人有任何示例代码可以做到这一点?

Thanks! 谢谢!

I see your table has binding to your bean. 我看到你的表绑定了你的bean。 In your bean you can use the getDataTable() method and access it. 在bean中,您可以使用getDataTable()方法并访问它。 Java doc says: Java doc说:

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> 唯一的区别是Im转换为我自己的类型而不是List<String>

There are no obvious errors in the form - if your save method is not being invoked, try adding a messages tag to your form to help track down the source of the problem. 表单中没有明显错误 - 如果未调用save方法,请尝试在表单中添加messages标记以帮助跟踪问题的根源。 It would help if you posted a sample bean that reproduces the problem and state the JSF implementation and version you are using. 如果您发布了一个重现问题的示例bean并说明您正在使用的JSF实现和版本,那将会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM