maximus
maximus

Reputation: 11524

How to get the selected item from an p:orderList?

I want to get the selected item from an orderList. It seems to me that this functionality is missing. Is there a functionality in PF? Or is there a possible workaround?

I really appreciate your answer!

My technology stack:

Upvotes: 1

Views: 3115

Answers (3)

Vasil Lukach
Vasil Lukach

Reputation: 3728

It is possible in PrimeFaces 5 and higher. There is no out of box feature, but it can be easily achieved with additional list of selected items. See example

Java code

private List<Record> allItems; // with getter and setter
private List<Record> selectedItems; // with getter and setter

public void onSelect(SelectEvent event) {
    if (null == event || null == event.getObject()) {
        return;
    }
    if (!event.isCtrlKey()) {
        setSelectedItems(new ArrayList<Record>());
    }
    Record item = (Record)event.getObject();
    if (!getSelectedItems().contains(item)) {
        getSelectedItems().add(item);
    }
}

public void onUnselect(UnselectEvent event) {
    if (null == event || null == event.getObject()) {
        return;
    }
    Record item = (Record)event.getObject();
    if (getSelectedItems().contains(item)) {
        getSelectedItems().remove(item);
    }
}

public void deleteSelected() {
    if (getAllItems().isEmpty()) {
        addErrorMessage("listEmpty");
        return;
    }
    if (getSelectedItems().isEmpty()) {
        addErrorMessage("noItemSelected");
        return;
    }
    for (Record item : getSelectedItems()) {
        if (getAllItems().contains(item)) {
            getAllItems().remove(item);
        }
    }
}

XHTML

    <h:panelGroup id="listGroup">
        <p:orderList id="list" value="#{bean.allItems}"
            var="item" itemLabel="#{item.code}" itemValue="#{item}"
            converter="#{itemConverter}" controlsLocation="none">
            <f:facet name="caption">#{msg.listName}</f:facet>
            <p:ajax event="select" listener="#{bean.onSelect}" />
            <p:ajax event="unselect" listener="#{bean.onUnselect}" />
        </p:orderList>
        <p:contextMenu id="listMenu" for="list">
            <p:menuitem value="#{msg.delete}"
                actionListener="#{bean.deleteSelected}"
                update="listGroup, messages"
                rendered="#{not empty bean.allItems}" />
        </p:contextMenu>
    </h:panelGroup>

Upvotes: 1

Jasper de Vries
Jasper de Vries

Reputation: 20198

I ran into the same issue trying to create an edit action for each item in my orderList. I tried passing my var to the action, creating an action listener in combination with an attribute, and a select listener all without success.

I ended up using a dataTable ... draggableRows="true" (see data table reorder in showcase). In the data table I could simply pass my var as a parameter to an action... So I thought. I ran into the issue that after reordering, clicking the edit button gave me the wrong item. I created a workaround being that on reorder I update the model and redraw the table. It works, but doesn't feel right.

<p:dataTable id="myTable"
             value="#{myBean.myItems}"
             var="item"
             sortBy="#{item.orderNumber}" sortOrder="ascending"
             draggableRows="true">
    ...
    <p:column headerText="Actions">
        <p:commandButton title="Edit"
                         action="#{myBean.edit(item)}"
                         process="@this">
    </p:column>
    <p:ajax event="rowReorder"
            listener="#{myBean.onSlotContentReorder}"
            update="myTable"/>
</p:dataTable>

Upvotes: 1

Mansoor
Mansoor

Reputation: 11

I've resolved this by using a button for delete. The code snippet is below:

<p:orderList id="layersList" value="#{mappingLayerController.layers}" 
             converter="layerConverter" var="layer" itemValue="#{layer}" 
             controlsLocation="left">
 <p:column>
  <h:outputText value="#{layer.layerName}"/>
 </p:column>
 <p:column style="width: 4%">
  <p:commandButton icon="ui-icon-close" actionListener="#{controller.deleteLayer()}">
   <f:param name="layerName" value="#{layer.layerName}" />
  </p:commandButton>
 </p:column>
</p:orderList>

And in the backing bean you can get the supplied parameter via the f:param tag as:

String layerName = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("layerName");

I hope this helps.

Upvotes: 1

Related Questions