如何在与按钮相同的数据表行中获取输入组件的值? [英] How to get the value of an input component in the same datatable row as the button?
问题描述
我有一个数据表,其中的行是动态的,每行都包含一个selectOneMenu
.如果我在每行上都有一个按钮,并且想要在selectOneMenu
上获取选定的项目,那么最好的方法是什么?
I have a datatable where the rows are dynamic and each row contain a selectOneMenu
. If I have a button on each row and I want to get the selected item on the selectOneMenu
, what's the best way to do it?
推荐答案
在 (本例中的Item
只是代表每一行的javabean类,例如Person
,Product
等)
(the Item
in this example is just the javabean class representing every row, e.g. Person
, Product
, etc)
将其绑定到数据表的value
.
<h:dataTable value="#{bean.model}" var="item">
如果下拉列表绑定到Item
的属性,并且按钮绑定到同一bean的方法...
If the dropdown is bound to a property of Item
and the button to a method of the same bean ...
<h:column>
<h:selectOneMenu value="#{item.value}">
<f:selectItems value="#{bean.values}" />
</h:selectOneMenu>
</h:column>
<h:column>
<h:commandButton value="submit" action="#{bean.submit}" />
</h:column>
... then you can grab the current item by DataModel#getRowData()
and corrspondingly also the selected value in action method as follows:
public void submit() {
Item item = model.getRowData();
String value = item.getValue();
// ...
}
这篇关于如何在与按钮相同的数据表行中获取输入组件的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!