title: Dynamic reload of items in select many component
body: <p>I want to achieve sth similar to <a href="http://www.primefaces.org/showcase/ui/pprSelect.jsf" rel="nofollow">http://www.primefaces.org/showcase/ui/pprSelect.jsf</a> but i need a collection of double-combos, so i wrapped it in <strong>ui:repeat</strong></p>
  
  <p>I need on the backend check which element from collection of double-combos was changed and what I need to reload. For communication is used p:ajax as in the example, but AjaxBehaviorEvent not bring me any idea of index of element ( i mean index of double-combos element generated by ui:repeat)</p>
  
  <p>My client code, the idea is to update <strong>bean:selectedIndex</strong> everytime when a ajax event will be raised ( on change value of <strong>selectOneMenu</strong> ), and value of <strong>bean:selectedIndex</strong> will be set as index of changed <strong>selectOneMenu</strong> </p>
  
  <pre><code>private List&lt;State&gt; productStates
  private int selectedIndex;
  private List&lt;Group&gt; groups;
  private Map&lt;Integer, Collection&lt;Device&gt;&gt; availableDevicesMap;
  </code></pre>
  
  <pre class="lang-html prettyprint-override"><code>&lt;ui:repeat var="state" value="#{bean.productStates}" varStatus="iter"&gt;
  
      &lt;p:selectOneMenu id="devGroup" value="#{state.group}"&gt;  
          &lt;f:selectItems value="#{bean.groups}"  /&gt;  
              &lt;p:ajax  update="refreshable"  process="devGroup, @this"  listener="#{bean.refreshDevicesForState}" &gt;
                      &lt;f:setPropertyActionListener target="#{bean.selectedIndex}" value="#{iter.index}"/&gt;
              &lt;/p:ajax&gt;                                    
      &lt;/p:selectOneMenu&gt;
  
      &lt;!-- THIS WILL BE UPDATED --&gt;
      &lt;h:panelGroup id="refreshable"&gt;
              &lt;p:selectManyButton  id="devices" value="#{state.devices}" &gt;  
                  &lt;f:selectItems value="#{bean.availableDevicesMap[status.index]}"   /&gt;  
              &lt;/p:selectManyButton&gt; 
      &lt;/h:panelGroup&gt;
  &lt;/ui:repeat&gt;
  </code></pre>
  
  <p>Backend which doesn't work as expected. <strong>setPropertyActionListener</strong> is not invoked and <strong>selectOneMenu</strong> component hasn't got selected group as value</p>
  
  <pre><code>public refreshDevicesForState(AjaxBehaviorEvent e) {
          SelectOneMenu menu = (SelectOneMenu)e.getComponent();
      // this value is not as selected on frontend
          Group group = (Group)menu.getValue();
  
      // selectedIndex will not be set, so I assume that setPropertyActionListener didn't invoked
      availableDevicesMap.put(selectedIndex, group.getDevices());
  }
  </code></pre>
  
  <p>I tried also with code below which works but in my opinion it is ugly</p>
  
  <pre><code>// id will be grandpaId:parentId:index:myId 
  String selectedIndex = IdHelper.getIdPart(e.getComponent().getClientId(), -2);
  State state = productStates.get(Integer.parseInt(selectedIndex));
  </code></pre>
  
  <p>I am using latest primefaces on glassfish and Mojarra as jsf reference implementation</p>
  
  <p>Thank you for any help</p>
  
  <h2>In more general sense:</h2>
  
  <p>I have list of objects on backed bean, lets say Cars</p>
  
  <pre><code>List&lt;Car&gt; cars
  </code></pre>
  
  <p>on frontent I iterate over them and create <em>select brand</em> and <em>select model</em> combos for every car. When user select brand for i.e 4th car i want to get to know on backend that 4th car will be changed and i will reload list of available model for this one car
  </p>
  
  <pre><code> &lt;ui:repeat var="state" value="#{bean.cars}" &gt;
      &lt;p:selectOneMenu id="brands"/&gt;// select brand
  
      &lt;p:selectOneMenu "models"/&gt;// show available models depends on selected brand
   &lt;/ui:repeat&gt;
  </code></pre>
  
  <p>How to handle it correct in the JSF world ?</p>