0
votes

I'm trying to add an inputText field to my primefaces pickList, but I've got some problems with it.

At first: I can't write in the textField with a normal left-click. I have to right click to be able to type in the textField.

Second: It doesn't seem to save the value from the textField to the corresponding entity.

My case:

<h:form id="pickListForm">
        <p:pickList converter="primeFacesPickListConverter" id="pickList" value="#{locodeBackingBean.locodes}"
                    var="locode" showCheckbox="true"
                    itemValue="#{locode.id}">
            <f:facet name="sourceCaption">#{msgs['locode.not.infected']}</f:facet>
            <f:facet name="targetCaption">#{msgs['locode.infected']}</f:facet>
            <p:column style="width:70%;">
                <h:outputText value="#{locode.description}"/>
            </p:column>
            <p:column style="width:30%">
                <p:inputText value="#{locode.incubationPeriod}"/>
            </p:column>
        </p:pickList>
        <p:commandButton value="#{msgs.save}" action="#{locodeBackingBean.save()}" update="growl"/>
    </h:form>

My backing bean:

@ManagedBean
@ViewScoped
public class LocodeBackingBean implements Serializable {
   private static final long serialVersionUID = 1L;

   @Inject
   private transient LocodeRepositoryBean locodeRepository;

   private DualListModel<Locode> locodes = new DualListModel<>();

   public void save() {
    for (Locode locode : locodes.getTarget()) {
        locode.setInfectedArea(true);
        locodeRepository.save(locode);
    }

    for (Locode locode : locodes.getSource()) {
        locode.setInfectedArea(false);
        locodeRepository.save(locode);
    }
    addFacesMessage("locode.action.saved");
   }

   @PostConstruct
   public void search() {
    List<Locode> source = locodeRepository.findAllNotInfected(locodeSearchCriteria);;
    List<Locode> target locodeRepository.findAllInfected(locodeSearchCriteria);
    locodes.setSource(source);
    locodes.setTarget(target);
   }


   public DualListModel<Locode> getLocodes() {
    return locodes;
   }

   public void setLocodes(DualListModel<Locode> locodes) {
    this.locodes = locodes;
   }

And to be complete, my converter:

@FacesConverter(value = "primeFacesPickListConverter")
public class PrimeFacesPickListConverter<T extends Domain> implements Converter {

   @Override
   public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
    if (arg1 instanceof PickList) {
        Object dualList = ((PickList) arg1).getValue();
        DualListModel dl = (DualListModel) dualList;
        for (Object o : dl.getSource()) {
            if (equalsById(arg2, o)) return o;
        }

        for (Object o : dl.getTarget()) {
            if (equalsById(arg2, o)) return o;
        }
    }
    throw new PrimeFacesPickListConverterException("Could not match object's id (" + arg2 + ") to any id's in the list.");
   }

   private boolean equalsById(String arg2, Object o) {
    String id = String.valueOf((((T) o)).getId());
    return arg2.equals(id);
   }

   @Override
   public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
    return String.valueOf(arg2);
   }

using PrimeFaces 5.2

1
It was never intended to be used this way. Embedding controls in a picklist in general is not very common (at least I've never seen it). See also stackoverflow.com/questions/32489834/… - Kukeltje

1 Answers

0
votes

Picklists in general are not intended to contain 'controls'. So it was never designed to work this way and if it does not work, that is just unfortunate. Redesigning your ui is the only thing to do.