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

时间:2022-03-06 23:07:07

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. In other words, I want to save each row's value. I saw this article, which explains how to save an input text box:

但是,我似乎无法弄清楚如何从save方法中的每一行中获取所选角色。换句话说,我想保存每一行的值。我看到这篇文章解释了如何保存输入文本框:

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?

但它似乎不适用于h:selectOneMenu案例。有没有人有任何示例代码可以做到这一点?

Thanks!

2 个解决方案

#1


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

我看到你的表绑定了你的bean。在bean中,您可以使用getDataTable()方法并访问它。 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

#2


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. It would help if you posted a sample bean that reproduces the problem and state the JSF implementation and version you are using.

表单中没有明显错误 - 如果未调用save方法,请尝试在表单中添加messages标记以帮助跟踪问题的根源。如果您发布了一个重现问题的示例bean并声明您正在使用的JSF实现和版本,那将会有所帮助。

#1


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

我看到你的表绑定了你的bean。在bean中,您可以使用getDataTable()方法并访问它。 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

#2


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. It would help if you posted a sample bean that reproduces the problem and state the JSF implementation and version you are using.

表单中没有明显错误 - 如果未调用save方法,请尝试在表单中添加messages标记以帮助跟踪问题的根源。如果您发布了一个重现问题的示例bean并声明您正在使用的JSF实现和版本,那将会有所帮助。