如何在jsf datatable中进行过滤?

时间:2010-09-01 01:23:49

标签: jsf

我需要在数据表中进行过滤。

我有两个下拉列表,第一个是静态值...类似于按名称,dob,age过滤。基于该开放的第二个下拉列表,并且过滤器显示值。

<ice:selectOneMenu size="12" onchange="this.form.submit()"
                            partialSubmit="true" id="filterByPastDue"
                            valueChangeListener="#{details.filterByListener}">

<f:selectItem itemLabel="None" itemValue="1" />
<f:selectItem itemLabel="Name" itemValue="2" />
<f:selectItem itemLabel="Age" itemValue="3" />
     <f:selectItem itemLabel="DOB" itemValue="4" />


<ice:selectOneMenu                              binding="#{person.selectFilterItems}" size="12"                             id="OpenFilterMenu" rendered="false">

在我的mangebean中有逻辑来获取第二个下拉值的值。

public void filterByListener(ValueChangeEvent valueChangeEvent) {
    int filterBy = Integer
            .parseInt((String) valueChangeEvent.getNewValue());

    switch (filterBy) {

    case 1: {
        filterItems = null;
        filterItems = new HashSet<SelectItem>();
        filterOptions = null;
        filterOptions = new HashSet<String>();
        outputTextFor = false;
        selectFilterItems.setRendered(false);

        break;
    }
    case 2: {
        // clearing arraylist
        filterItems = null;
        filterItems = new HashSet<SelectItem>();
        filterOptions = null;
        filterOptions = new HashSet<String>();

        // loading arrayList
        for (int i = 0; i < person.size(); i++) {
            filterOptions.add(person.get(i).getName()
                    .toString());
        }

        Iterator it = filterOptions.iterator();

        while (it.hasNext()) {
            String s = (String) it.next();
            filterItems.add(new SelectItem(s, s));

        }
        // rendering components
        outputTextFor = true;
        selectFilterItems.setRendered(true);

        break;
    }
    case 3: {
        // clearing arraylist
        filterItems = null;
        filterItems = new HashSet<SelectItem>();
        filterOptions = null;
        filterOptions = new HashSet<String>();
        // loading arrayList
        for (int i = 0; i < person.size(); i++) {
            filterOptions.add(person.get(i).getAge());
        }
        Iterator it = filterOptions.iterator();

        while (it.hasNext()) {
            String s = (String) it.next();
            filterItems.add(new SelectItem(s, s));

        }
        // rendering components
        outputTextFor = true;
        selectFilterItems.setRendered(true);

        break;
    }
    case 4: {
        // clearing arraylist
        filterItems = null;
        filterItems = new HashSet<SelectItem>();
        filterOptions = null;
        filterOptions = new HashSet<String>();
        // loading arrayList

        for (int i = 0; i < person.size(); i++) {
            filterOptions.add(person.get(i).getDOB());
        }
        Iterator it = filterOptions.iterator();

        while (it.hasNext()) {
            String s = (String) it.next();
            filterItems.add(new SelectItem(s, s));
        }
        // rendering components
        outputTextFor = true;
        selectFilterItems.setRendered(true);

        break;
    }
            default: {
        // clearing arrayList
        filterItems = null;
        filterItems = new HashSet<SelectItem>();
        filterOptions = null;
        filterOptions = new HashSet<String>();
        outputTextFor = false;
        selectFilterItems.setRendered(false);

        break;
    }
    }

}

现在我得到了我的数据表中的第二个下拉值。

现在,如何在datatable中显示过滤器值。我有人物对象,需要显示在第二个下拉列表中选择过的数据表。

1 个答案:

答案 0 :(得分:3)

您需要预设与value h:dataTable属性相关联的属性和所需的数据模型。您可以在提交按钮的action方法或第二个下拉列表的valueChangeListener方法中执行此操作。 E.g。

public void submit() {
    this.dataModel = getDataModelSomehow(filterBy, filterOption);
}