How to count how many occurrences of an attribute in a list are equal to a value? JSF

Asked

Viewed 122 times

0

I need to count how many status equals '1'. I used this to count how many objects I had in the list and it worked. Now I can’t count only those who have status equal to 1.

<h:outputText value="#{fn:length(cur.listaProdutosManutencao)}" />

That’s what I used and it worked.

I tried this below to get the status and it didn’t work:

<h:outputText value="#{fn:length(cur.listaProdutosManutencao.status eq '1')}" />

I tried this one too, and it didn’t work:

<h:outputText value="#{fn:join(cur.listaProdutosManutencao.status, '1')}"

Can anyone help me? I would like to follow the first one I did.

  • can you provide more details? Post your Books with this you can create a method for this.

  • Dude, listProductManutencao is a List<Product> of an object with string name and string status. Just that. How it would look?

  • Create a method that does this for you. Where you would check the status within a for would make it easier.

1 answer

0

Coso, you are using JDK 8 you can use list.foreach(action). I vary a method for this.

public Integer totalStatus ( List<ProdutoManutencao> lista, String status){
      Integer  result = 0;
      //validacao para a  lista null ou status.
     for (ProdutoManutencao object : lista) {
         result += object.stattus.equals(status) ? 1 : 0;   
      }
     return result;
  } 
}

In your . xhtml you can call your method. It can be a shorter method that you already do this directly in your bean without having to pass parameters.

<h:outputText value="#{cur.totalStatu( cur.listaProdutosManutencao() ,"1") }" />

Browser other questions tagged

You are not signed in. Login or sign up in order to post.