Bug in dataTable?

UI Components for JSF
Post Reply
dandao
Posts: 2
Joined: 16 Mar 2012, 15:24

16 Mar 2012, 15:43

I have 6 products in my dataTable. Number lines for pagination is 5.
If I click in page number 2, it has only 1 record.
If I delete this record, my dataTable not renderize backing to the page 1 with others 5 records. It shows like if no have records and not renderize.
So, when I click in page link 1, my dataTable shows the 5 records correctly.
Is it a bug or is it something I'm wrong?
I'll show my codes:

Code: Select all

<h:form>  
  
    <h:panelGrid id="panelGridDataTable">  
  
        <p:dataTable id="produtoDataTable" var="p"  
            value="#{bean.produtosList}" paginator="true" rows="5"  
            paginatorPosition="bottom"  
            emptyMessage="Nenhum produto encontrado.">  
  
            <p:column sortBy="#{p.id}">  
                <f:facet name="header">  
                    <h:outputText value="ID" />  
                </f:facet>  
                <h:outputText value="#{p.id}" />  
            </p:column>  
            <p:column sortBy="#{p.nome}">  
                <f:facet name="header">  
                    <h:outputText value="Nome" />  
                </f:facet>  
                <h:outputText value="#{p.nome}" />  
            </p:column>  
            <p:column>  
                <f:facet name="header">  
                    <h:outputText value="Ações" />  
                </f:facet>  
                <p:commandLink update=":form:confirmacaoExclusao"  
                    oncomplete="confirmacaoExclusao.show()">  
                    <h:graphicImage id="imagemExcluir"  
                        value="/resources/imagens/excluir.png" />  
                    <f:setPropertyActionListener value="#{p}"  
                        target="#{bean.produto}" />  
                </p:commandLink>  
                <p:tooltip for="imagemExcluir" value="Excluir" />  
            </p:column>  
  
        </p:dataTable>  
  
    </h:panelGrid>  
  
    <p:dialog id="confirmacaoExclusao" widgetVar="confirmacaoExclusao"  
        header="Exclusão" showEffect="fade"  
        hideEffect="fade" modal="true" resizable="false">  
  
        <center>  
  
            <br></br>  
  
            <h:graphicImage value="/resources/imagens/aviso.png" />  
            <p:spacer width="20" />  
            <h:outputText value="Confirma exclusão?" />  
  
            <br></br>  
  
            <h:panelGrid id="confirmaExclusaoProdutoPanelGrid" columns="2"  
                columnClasses="textoDireita" cellpadding="3">  
  
                <h:outputText value="ID:" styleClass="negrito" />  
                <h:outputText value="#{bean.produto.id}" />  
  
                <h:outputText value="Nome:" styleClass="negrito" />  
                <h:outputText value="#{bean.produto.nome}" />  
  
            </h:panelGrid>  
  
            <br></br>  
  
            <p:commandButton action="#{bean.excluir}"  
                oncomplete="confirmacaoExclusao.hide()"  
                value="Sim" update="panelGridDataTable" />  
            <p:spacer width="10" />  
            <p:commandButton oncomplete="confirmacaoExclusao.hide()"  
                value="Não" />  
  
            <br></br> <br></br>  
  
        </center>  
  
    </p:dialog>  
                  
</h:form> 
Bean

Code: Select all

@ManagedBean(name = "bean")  
@SessionScoped  
public class Bean implements Serializable {  
  
    private static final long serialVersionUID = 1L;  
    private Produto produto;  
    private List<Produto> produtosList;  
    @EJB  
    private ProdutoDAO produtoDAO;  
  
    public void excluir() {  
  
        if (produtoDAO.excluir(getProduto())) {  
  
            getProdutoList().remove(getProduto());  
  
        }  
    }  
  
    public Produto getProduto() {  
        if (produto == null) {  
            produto = new Produto();  
        }  
        return produto;  
    }  
  
    public void setProduto(Produto produto) {  
        this.produto = produto;  
    }  
  
    public List<Produto> getProdutosList() {  
        if(produtosList == null){  
            produtosList = produtoDAO.listar();  
        }  
        return produtosList;  
    }  
  
    public void setProdutosList(List<Produto> produtosList) {  
        this.produtosList = produtosList;  
    }  
  
} 

User avatar
T.dot
Expert Member
Posts: 620
Joined: 01 Feb 2012, 15:39
Location: Vienna/Austria

16 Mar 2012, 16:10

Do you update the table after deleting the row?

dandao
Posts: 2
Joined: 16 Mar 2012, 15:24

16 Mar 2012, 17:34

Yes.

Code: Select all

<p:commandButton action="#{bean.excluir}" 
                oncomplete="confirmacaoExclusao.hide()" 
                value="Sim" update="panelGridDataTable, produtoDataTable" /> 
            <p:spacer width="10" />  
I updated the panelGrid and the dataTable, but it doesn't fix.

angusan
Posts: 4
Joined: 16 Apr 2012, 13:00

16 Apr 2012, 13:56

We also met same issue and had a work around solution.
But it still has defect since the page number actually didn't move forward. (still at pg 2 not move to 1 based on dandao's case)

After deleting last record in last page(assume it is 5), Paginator's first/pre/next/last link are all enable; (next, last link should be disabled)
JumpToPageDropdown display 1 (should be 4);
widgetVar.getPaginator.getCurrentPage = 4 (should be 3)

Until we click any of these links or select PageDropDown, the paginator get back to normal .
Anyone has further solution? or Know how to update current page number in java code?

Code: Select all

public List<T> load(...) {
   int lastRecord = Math.min(first + pageSize, list.size());
   if (first >= lastRecord) {
     first = first - pageSize;
   }
   try {
      return list.subList(first, lastRecord);
   } catch (Exception ex) {
      return list;
   }
}

User avatar
andyba
Expert Member
Posts: 2473
Joined: 31 Mar 2011, 16:27
Location: Steinfeld, near Bremen/Osnabrück, DE
Contact:

16 Apr 2012, 15:39

Are you refreshing your data model you feed the DataTable with after deleting the record from the database?

One of the most common causes of errors with things like this is forgetting you need to reread your datatable model back out of the database.
Another one is not making sure that the page you were on when you delete something from it still exists after the operation. Here it seems like you are expecting to read the second page when you can't.
PF 4.x (Elite versions), PF 5, Pf 5.1, PF 6.0
Glassfish 4.1, Mojarra 2.x, Java 8, Payara 4.1.1.
If you haven't read the forum rules read them now

angusan
Posts: 4
Joined: 16 Apr 2012, 13:00

18 Apr 2012, 11:02

hi andyba, thanks for reminding, i tried several ways to update lazy model right after deletion.

at last, i found datatable.loadLazyModel() does work. (since i saw the record number didn't update until we force it to reload)

but the page number display on UI is still incorrect(i do update UI by update attribute).

it didn't locate on any page, just like the case i mentioned before.

any other suggestion in advance? thanks for your kindly support

User avatar
andyba
Expert Member
Posts: 2473
Joined: 31 Mar 2011, 16:27
Location: Steinfeld, near Bremen/Osnabrück, DE
Contact:

18 Apr 2012, 13:34

This is a typical case of "what happens when you delete a record from a page when only 1 record is visible on that page".
Scenario: you have at least 2 pages of records the last page of which only displays 1 record.
You are viewing the last page and you delete that one record. When the view is refreshed and you don't account for it you suddenly have no data page to display.
This is what you are seeing.

Solution: when you calculate which page you currently wish to display you need to make sure that it does not exceed the maximum number of available data pages and set it to the max page if it does.
PF 4.x (Elite versions), PF 5, Pf 5.1, PF 6.0
Glassfish 4.1, Mojarra 2.x, Java 8, Payara 4.1.1.
If you haven't read the forum rules read them now

User avatar
T.dot
Expert Member
Posts: 620
Joined: 01 Feb 2012, 15:39
Location: Vienna/Austria

18 Apr 2012, 14:11

If you use a lazy dataModel the following works for me:
- set your dataModel to null
- call reset() on the binding of your dataTable.
- update the dataTable so a new DataModel is generated

angusan
Posts: 4
Joined: 16 Apr 2012, 13:00

22 Apr 2012, 11:43

Hi, T.dot

Many thanks for your solution.
It works!!!

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 58 guests