简体   繁体   中英

Primefaces Picklist with large data set

I created PickList is my website and it works but is very slow. I have about 1000 items and every time I click submit it goes into Converter and does 1000 times it. Even if nothing selected.

Maybe I can skip converter or take only target items (not found example on internet) or give back only some text (for this can be only Code) or other solusion?

I am using primefaces 3.5

My Converter:

public Object getAsObject(FacesContext context, UIComponent component, String value) {

    if(value != null){
        int iii = Integer.parseInt(value);
        Jbtit s = new Jbtit();

        s = SR.findById(iii);

        return s;
    }

    return null;
}


public String getAsString(FacesContext context, UIComponent component, Object value) {

    if(value == null){
        return "";
    }

    if (!(value instanceof Jbtit)) return null;

    Integer i = ((Jbtit) value).getId();

    if(i != null){
        String s = Integer.toString(i);
        return s;
    }
    return "";
}

XHTML:

<p:pickList id="jbtit" styleClass="jbtitPickList" 
    value="#{panelCountMB.jobTitles}" var="jb" effect="none" 
    itemValue="#{jb}" itemLabel="#{jb.code} - #{jb.description_en}"
    converter="#{jbtitConverter}"  showCheckbox="true" showSourceFilter="true"
    showTargetFilter="true" filterMatchMode="contains" >  
    <f:facet name="sourceCaption">Available</f:facet>  
    <f:facet name="targetCaption">Selected</f:facet>  
        <p:column >  
            <h:outputLabel styleClass="testingas" style="width:100%"  value="#{jb.code} - #{jb.description_en}" />
        </p:column>  
</p:pickList>

It seems to be so slow because of the 'findById' executed for each of the 1000 items. Maybe use database caching or implement a custom cache solution for this case.

It seems it was my mistake to use always database.

I found another solution.

Converter can be rewritten like this:

@Component(value="primefacesPicklistConverter")
public class PrimefacesPicklistConverter implements Converter {

        public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
            Object ret = null;
            if (arg1 instanceof PickList) {
                Object dualList = ((PickList) arg1).getValue();
                DualListModel<?> dl = (DualListModel<?>) dualList;
                for (Object o : dl.getSource()) {
                    String id = "";
                    if (o instanceof Jbtit) {
                        id += ((Jbtit) o).getId();
                    }
                    if (arg2.equals(id)) {
                        ret = o;
                        break;
                    }
                }
                if (ret == null)
                    for (Object o : dl.getTarget()) {
                            String id = "";
                            if (o instanceof Jbtit) {
                                id += ((Jbtit) o).getId();
                            }
                        if (arg2.equals(id)) {
                            ret = o;
                            break;
                        }
                    }
            }
            return ret;
        }

        public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
            String str = "";
            if (arg2 instanceof Jbtit) {
                str = "" + ((Jbtit) arg2).getId();
            }
            return str;
        }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM