tags:

views:

49

answers:

1

Hello All! I have one command button for task add to cart, it means when items quantity instock is geater than 0 command button will display but if items quantity instock is 0 it doesn't display. How can i write one method render with command button for my upon requirement?

Edited

=================================================================================

i post my code attach with question following: i write one method in Session Bean select list of Quantity on hand in stock are zero following:

public List<Items> checkItemsInstock(){
        Query query = em.createNamedQuery("Items.findByInstockgtZero");
        query.setParameter("instockgtzero", 0);

        return query.getResultList();

    }

and Namequery is:

@NamedQuery(name = "Items.findByInstockgtZero", query = "SELECT i FROM Items i WHERE i.instock = :instockgtzero")

in JSF Managed Bean i wrote a method return list of items , and Quantity on hand are zero

public List<Items> getQuantityOnHand(){
    return itemDAO.checkItemsInstock();
  }

and in JSF Page i was rendered h:commandButton is:

<h:commandButton value="add to cart" style="font-size: x-small" action="#{catItemsListController.addtoCart(item)}"  id="addcart" rendered="#{not empty catItemsListController.quantityOnHand}"/>

but it not work, all items have QOH is zero or not zero are display CommandButton

i need help ! Thank you

+1  A: 

The empty keyword of Expression Language will check if the given property is null or empty. So in your case, use not empty:

<h:commandButton ... rendered="#{not empty myBean.myList}"/>

So this button will only be displayed when the list exists and is not empty.

Another idea is to provide a specific method:

public boolean isCommandButtonRendered() {
    return myList != null && !myList.isEmpty();
}

and use it in the XHTML code:

<h:commandButton ... rendered="#{myBean.commandButtonRendered}"/>

Edit

I think there is a misunderstanding on what you expect regarding the solution I provided. My solution is working if you want to test the list itself (i.e. the list is null or does not contains any element).

In your case, it seems that your list is not empty, but contains elements (objects Items) that has a value that can be equals to 0 or not.

I don't know the code of your Items class, so I will consider that this class holds a instock int property.

So as far as I know, you iterate on your List within a datatable:

<h:datatable var="item" value="#{catItemsListController.quantityOnHand}" ...>
    ...
    <h:column>
        <h:commandButton ... rendered="#{item.instock gt 0}"/>
    </h:column>
</h:datatable>

As shown in my code, you have a commandButton that will be displayed only if the value of instock of the item element is greater than (gt) 0.

romaintaz
@romaintaz: Thank you for your support but it not work, i was edit my question, please check it ! Thank you
MYE
@MYE In `getQuantityOnHand()` method, maybe you can add a log in order to check the number of elements in the list.
romaintaz
@romaintaz can you give me code sample ? Thank you
MYE
@MYE Code sample for what exactly?
romaintaz
` you can add a log in order to check the number of elements in the list` i not yet understand you said! it mean user log in?
MYE
No, just a `System.out.println("List size: " + myList.size());` (or a `log` using *log4j* for example) in order to check if the getter returns an empty list or not...
romaintaz
it print 5 line and result are 2 ( 5 items and 2 items are zero) but when i rendered h:commandbutton `rendered="#{myBean.commandButtonRendered}"/>` 5 items not display h:commandButton, while 2 items quantity on hands are zero and 3 is not, i dont know why it not display?
MYE
in method `public boolean isCommandButtonRendered() { return myList != null }` client return true it means my list not null and not equals zero but when i print it, it loop through items , if i have 5 items and 2 items are zero it print 5 lines and result list().size() are 2, i need helps please
MYE
@MYE I don't get you. You say that the `list.size()` returns `2`. So your list is **not** empty, and thus the command button is rendered. No?
romaintaz
yes romaintaz my sql statement is select items from DB where quantity on hands is zero (0) if it zero it means mylist.size is not null it does not show the items button, which its quantity on hand is zero. I dont know why it not display all or display all if rendered is !mybean.list or rendered is mybean.list
MYE
@romaintaz i use dataTable to display data
MYE
@MYE I think I finally get your problem. I've edited my answer. Let me know if it solves your issue now.
romaintaz
Thank you! Best Regards
MYE