title: PrimeFaces LazyDataModel resolves rows incorrectly after live scroll?
body: <p>I'm trying to use PrimeFaces 5.3 (with JSF 2.2) to implement a data table that contains items, and each item has a list of command buttons to perform server side actions. I use primefaces LazyDataModel because I have huge datasets and cannot afford to load all the items when the view loads. But after livescrolling happens, rows do not behave as expected.</p>
  
  <p>I have setup a small project to illustrate my issue. (full sources at the end of the post)</p>
  
  <p><a href="https://i.stack.imgur.com/8CaXA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8CaXA.png" alt="Screenshot"></a></p>
  
  <h2>The issue</h2>
  
  <p>When I press A0, "Executed A0" must be printed on the console (and so on).</p>
  
  <p>Everything works fine for the first data set. Here's what happens when I press the first three buttons:</p>
  
  <pre><code>Fetching starting at 0       //expected result  
  Executed A0                  //A0
  Executed A1                  //A1
  Executed A2                  //A2
  </code></pre>
  
  <p>But when I scroll, and 3 other items are loaded, the first three rows do not work correctly. </p>
  
  <p><a href="https://i.stack.imgur.com/GvyQZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GvyQZ.png" alt="After scrolling"></a></p>
  
  <p>Here's what happens when I press all the buttons in order:</p>
  
  <pre><code>Fetching starting at 3    //expected result
  Executed A3               //A0
  Executed A4               //A1
  Executed A5               //A2
  Executed A3               //A3
  Executed A4               //A4
  Executed A5               //A5
  </code></pre>
  
  <p>The first three buttons execute the actions of the last three ones :(</p>
  
  <h2>A server-side issue</h2>
  
  <p>It is not a client-side issue, I have monitored the requests sent.</p>
  
  <p>When I press A0, the request contains the following POST parameters:</p>
  
  <pre><code>j_idt8:resultsTable:0:j_idt12:0:j_idt13=j_idt8:resultsTable:0:j_idt12:0:j_idt13
  javax.faces.source=j_idt8:resultsTable:0:j_idt12:0:j_idt13
  </code></pre>
  
  <p>And when I press A3:</p>
  
  <pre><code>j_idt8:resultsTable:3:j_idt12:0:j_idt13=j_idt8:resultsTable:3:j_idt12:0:j_idt13
  javax.faces.source=j_idt8:resultsTable:3:j_idt12:0:j_idt13
  </code></pre>
  
  <p>The correct row is referenced after resultsTable:</p>
  
  <h2>Debug mode</h2>
  
  <p>I also messed with the debug mode. When I press A1, the UIData#brodacast(FacesEvent) is called with an event that contains a field rowIndex=1. So it works fine at this point.</p>
  
  <p>Then, I don't really understand what happens. But at the end of the process, the target method is executed on the wrong instance...</p>
  
  <h2>Source code</h2>
  
  <p>Item.java</p>
  
  <pre><code>public class Item {
      private int id;
      private String name;
      private List&lt;ItemAction&gt; actions;
      public Item(int id, String name, List&lt;ItemAction&gt; actions) {
          this.id = id;
          this.name = name;
          this.actions = actions;
      }
      public int getId() {
          return id;
      }
      public String getName() {
          return name;
      }
      public List&lt;ItemAction&gt; getActions() {
          return actions;
      }
  }
  </code></pre>
  
  <p>ItemAction.java</p>
  
  <pre><code>public class ItemAction {
      private String label;
      public ItemAction(String label) {
          this.label = label;
      }
      public void execute(){
          System.out.println("Executed " + label);
      }
      public String getLabel() {
          return label;
      }
  }
  </code></pre>
  
  <p>TheService.java</p>
  
  <pre><code>public class TheService {
      private static final int ITEMS_COUNT = 8;
      public int getItemsCount() {
          return ITEMS_COUNT;
      }
      public List&lt;Item&gt; retrieveItems(int startIndex, int pageSize){
          List&lt;Item&gt; result = new ArrayList&lt;&gt;(pageSize);
          int endIndex = startIndex + pageSize;
          if(endIndex &gt; ITEMS_COUNT){
              endIndex = ITEMS_COUNT;
          }
          for(int index=startIndex; index&lt;endIndex; index++){
              result.add(generateItem(index));
          }
          return result;
      }
      private Item generateItem(int id){
          List&lt;ItemAction&gt; actions = new ArrayList&lt;&gt;(2);
          actions.add(new ItemAction("A" + id));
          actions.add(new ItemAction("B" + id));
          return new Item(id, "Item " + id, actions);
      }
  }
  </code></pre>
  
  <p>DatatableLazyModel.java</p>
  
  <pre><code>@ManagedBean
  @ViewScoped
  public class DatatableLazyModel extends LazyDataModel&lt;Item&gt;{
      private TheService service;
      private List&lt;Item&gt; allResults;
      public DatatableLazyModel() {
          this.service = new TheService();
          this.allResults = new ArrayList&lt;&gt;();
      }
      @Override
      public int getRowCount() {
          return service.getItemsCount();
      }
      @Override
      public List&lt;Item&gt; load(int first, int pageSize, String sortField, SortOrder sortOrder,
              Map&lt;String, Object&gt; filters) {
          System.out.println("Fetching starting at " + first);
          List&lt;Item&gt; pageResults = service.retrieveItems(first, pageSize);
          allResults.addAll(pageResults);
          return pageResults;
      }
  }
  </code></pre>
  
  <p>index.xhtml</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8' ?&gt;
  &lt;!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;ui:composition xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui" template="template/ui.xhtml"&gt;
      &lt;ui:define name="body"&gt;
          &lt;h3&gt;Datatablelazy load&lt;/h3&gt;
  
          &lt;h:form&gt;
              &lt;p:dataTable id="resultsTable" var="item"
                  value="#{datatableLazyModel}" liveScroll="true" scrollRows="3"
                  scrollHeight="100" scrollable="true" lazy="true"&gt;
  
                  &lt;p:column headerText="Name"&gt;
                      &lt;h:outputText value="#{item.name}" /&gt;
                  &lt;/p:column&gt;
  
                  &lt;p:column headerText="Actions"&gt;
                      &lt;ui:repeat var="action" value="#{item.actions}"&gt;
                          &lt;p:commandButton action="#{action.execute()}" value="#{action.label}"&gt;
                          &lt;/p:commandButton&gt;
                      &lt;/ui:repeat&gt;
                  &lt;/p:column&gt;
              &lt;/p:dataTable&gt;
          &lt;/h:form&gt;
      &lt;/ui:define&gt;
  &lt;/ui:composition&gt;
  </code></pre>
  
  <p>pom.xml dependencies</p>
  
  <pre><code>&lt;dependency&gt;
          &lt;groupId&gt;com.sun.faces&lt;/groupId&gt;
          &lt;artifactId&gt;jsf-api&lt;/artifactId&gt;
          &lt;version&gt;2.2.4&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.sun.faces&lt;/groupId&gt;
          &lt;artifactId&gt;jsf-impl&lt;/artifactId&gt;
          &lt;version&gt;2.2.4&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
          &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;
          &lt;version&gt;3.0.1&lt;/version&gt;
      &lt;/dependency&gt;
  
          &lt;dependency&gt;  
          &lt;groupId&gt;org.primefaces&lt;/groupId&gt;  
          &lt;artifactId&gt;primefaces&lt;/artifactId&gt;  
          &lt;version&gt;5.3&lt;/version&gt;  
      &lt;/dependency&gt;  
  </code></pre>
  
  <h2>Questions</h2>
  
  <p>I have tried this with more items loaded. Each time, only methods on the last loaded set are executed. Even from old components.</p>
  
  <p>So basically, the question is : <strong>"How can I make all the buttons work as expected?"</strong></p>
  
  <p>Why do methods do not get executed on the correct instance? How is the target instance resolved?</p>
  
  <p>Thank you for your help :)</p>