1

I need to be able to display a dialog referencing the current item, when clicking a SelectBooleanCheckbox inside a DataList. I don't seem to be able to pass the current item/index on valueChange or keep a record of the current item when paging through the list. Either would work for me. Can anyone please help?

Thanks :) Neil

Neil Richards
  • 131
  • 1
  • 4
  • 16

1 Answers1

0

It's easy if you use a view scoped bean. Basically, you just need to set the current item of the data list as a property of the bean and update the dialog's content which should present that item and then open the dialog. Assuming that the dialog represents an edit form which you'd like to close on successful save, then you'd like to update the data list as well with the new value. You could use <p:remoteCommand> for this. You can of course also add the client ID of the data list to the update attribute of the command button in the dialog, but that's thus unnecessary when there's a validation error.

Here's a concrete kickoff example:

<h:form>
    <p:dataList id="list" value="#{bean.items}" var="item">
        #{item.id} #{item.value}
        <p:commandButton value="edit" 
            action="#{bean.setItem(item)}" 
            update=":editForm" 
            oncomplete="editDialog.show()" />
    </p:dataList>
    <p:remoteCommand name="updateList" update="list" />
</h:form>
<p:dialog widgetVar="editDialog">
    <h:form id="editForm">
        <p:messages autoUpdate="true" />
        <h:inputText value="#{bean.item.value}" required="true" />
        <p:commandButton value="save" 
            action="#{bean.save}" 
            oncomplete="if (!args.validationFailed) { editDialog.hide(); updateList(); }" />
    </h:form>
</p:dialog>

with this bean:

@ManagedBean
@ViewScoped
public class Bean implements Serializable {

    private List<Item> items;
    private Item item;

    @EJB
    private ItemService service;

    @PostConstruct
    public void loadItems() {
        items = service.list();
    }

    public void save() {
        service.save(item);
        loadItems();
    }

    public List<Item> getItems() {
        return items;
    }

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }

}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for your reply :). That works because you are able to pass a parameter through the action, but I need to pop up a dialog when a SelectBooleanCheckbox is checked (but not unchecked), and so I only have a listener to work on. Is there any way of doing something similar in that case? – Neil Richards Dec 07 '12 at 10:07
  • You could nest a `` in there. It also offers an `oncomplete` attribute (with standard JSF `` you'd have to fiddle with `onevent` attribute). – BalusC Dec 07 '12 at 11:38
  • I have one of those already with a listener which needs the current item. Where/when would the bean.setItem() get called though? oncomplete is too late. – Neil Richards Dec 07 '12 at 13:31
  • Just in the listener method. Note that oncomplete doesn't execute a bean method, but JavaScript code, so your "too late" argument makes no sense. – BalusC Dec 07 '12 at 13:35
  • Sorry - I misunderstood. I already use the oncomplete to pop up the dialog conditionally (like your suggestion) but the listener already needs 'item' set to determine whether the dialog should pop up or not. When you say that setItem() gets called 'in the listener method' how am I getting the 'item' to set. Sorry - feeling stupid now :) – Neil Richards Dec 07 '12 at 13:56
  • Just the same way. ``. – BalusC Dec 07 '12 at 14:01
  • Been away from the office until now - tried as you suggested `` and get `Error Parsing: #{bean.setCurrentItem(item)}. Encountered "(" at line 1, column 36. Was expecting one of: "}" ... "." ... "[" ... ">" ... "gt" ... "<" ... "lt" ... ">=" ... "ge" ... "<=" ... "le" ... "==" ... "eq" ... "!=" ... "ne" ... "&&" ... "and" ... "||" ... "or" ... "*" ... "+" ... "-" ... "?" ... "/" ... "div" ... "%" ... "mod" ... ` – Neil Richards Dec 10 '12 at 16:20
  • Oh, you're not on EL 2.2 yet? Use approach #5 of this answer then http://stackoverflow.com/a/4994833/157882 – BalusC Dec 10 '12 at 16:21
  • Amazing. I'd hardly finished typing my comment and you had answered :). Not on 2.2 yet - hoping to upgrade JBoss next year which will allow us to do so. Used approach #5 and it worked a treat. Thank you again! – Neil Richards Dec 10 '12 at 17:07
  • You're welcome. You can alternatively also install JBoss EL to get the same EL 2.2 feature in a Servlet 2.5 / EL 2.1 targeted webapp. Head to this related answer for the How: http://stackoverflow.com/questions/8325298/invoking-methods-with-parameters-by-el-in-jsf-1-2/8326551#8326551 – BalusC Dec 10 '12 at 17:09
  • Actually that might be a better solution. No code change when upgrading then :). Will try it. – Neil Richards Dec 10 '12 at 17:13
  • Got the same error back again with that solution. and jar file are as suggested. Never mind - I do have a solution which I'm stoked about :) – Neil Richards Dec 10 '12 at 17:28