Search code examples
jsfjsf-2primefacesspring-webflowjsf-2.2

how to limit the records to display under <h:selectManyCheckbox>


I would like to limit the no of record to be displayed under .

Here is the code snippet.

//Filter Class used to display checkboxes

class Filter {
 public String filterValueName;
}

// Fields declared under BEAN

public List<String> filterValuesChecked;
public List<Filter> filterValues;

// XHTML to iterate over list

<h:selectManyCheckbox value="#{fltrResult.filterValuesChecked}" layout="pageDirection" >
    <f:selectItems value="#{fltrResult.filterValues}" var="fltrVal"
        itemLabel="#{fltrVal.filterValueName}"
        itemValue="#{fltrVal.filterValueName}" />
</h:selectManyCheckbox>

Above code is working very well, but here issue is List filterValues contains 1000 records, and I want to display only 5-10 checkboxes and after that Link to visualize whole list.

I had wasted so much time on google to find out the solution but didn't get it. Please provide the way how can I achieve the same

Thanks in Advance, Chirag


Solution

  • Try this:

    In the Bean:

    private final int MAXNUMBER = 10; // e.g.
    public List<String> filterValuesChecked;
    public List<Filter> filterValues;
    
    // With a fixed limit:
    public List<Filter> getRestrictedFilterValues(){
        return this.filterValues.subList(0, MAXNUMBER);
    }
    
    // With a dynamic limit:
    public List<Filter> getRestrictedFilterValues(int maximum){
        return this.filterValues.subList(0, maximum);
    }
    

    In the xhtml:

    <h:selectManyCheckbox value="#{fltrResult.filterValuesChecked}" layout="pageDirection">
    <f:selectItems value="#{fltrResult.getRestrictedFilterValues()}" var="fltrVal"
        itemLabel="#{fltrVal.filterValueName}"
        itemValue="#{fltrVal.filterValueName}" />
    </h:selectManyCheckbox>
    

    Of cause, you can use any select-algorithm in the getRestrictedFilterValues-Method.

    Addition: Consider the usage of a (custom) converter in this cases, so you don't have to handle with Strings but can directly get Filter-Objects as result (=> public List<Filter> filterValuesChecked;). See http://www.tutorialspoint.com/jsf/jsf_customconvertor_tag.htm