Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>While Richfaces extended data tables support a <a href="http://showcase.richfaces.org/richfaces/component-sample.jsf?demo=extendedDataTable&amp;sample=exTableSelection&amp;skin=blueSky" rel="nofollow">selection management</a>, Richfaces datatables don't.</p> <p>What I found the easiest way to do to retrieve a somehow selected item from a list, is to add an icon to every row. For this, put the command-Button into the data table itself:</p> <pre><code>&lt;rich:dataTable id="itemTable" value="#{backingBean.itemsList}" var="i" &gt; &lt;rich:column&gt; &lt;h:inputText id="myId" value="#{i.value}" /&gt; &lt;h:commandButton id="saveB" action="#{backingBean.doSave}" /&gt; &lt;/rich:column&gt; &lt;/rich:dataTable&gt; </code></pre> <p>In the bean code, provide the method <code>doSave</code> but with an additional parameter ´ActionEvent´</p> <pre><code>public String doSave(ActionEvent ev) { Item selectedItem = null; UIDataTable objHtmlDataTable = retrieveDataTable((UIComponent)ev.getSource()); if (objHtmlDataTable != null) { selectedItem = (Item) objHtmlDataTable.getRowData(); } } private static UIDataTable retrieveDataTable(UIComponent component) { if (component instanceof UIDataTable) {return (UIDataTable) component;} if (component.getParent() == null) {return null;} return retrieveDataTable(component.getParent()); } </code></pre> <p>You see, that the <code>ActionEvent ev</code> provides you with the source element <code>(UIComponent)ev.getSource()</code>. Traverse through it until you hit the <code>UIDataTable</code> element and use it's row data.</p> <p>Possible way two is to justgive the element as parameter with the function call:</p> <pre><code> &lt;rich:dataTable id="itemTable" value="#{backingBean.itemsList}" var="i" &gt; &lt;rich:column&gt; &lt;h:inputText id="myId" value="#{i.value}" /&gt; &lt;h:commandButton id="saveB" action="#{backingBean.doSave(i)}" /&gt; &lt;/rich:column&gt; &lt;/rich:dataTable&gt; </code></pre> <p>and in the bean </p> <pre><code>public String doSave(Item item) { // do stuff } </code></pre> <p>That's not as clean, but should work with the EL, too. Hope, it helps...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload