0

我有一个数据表,并且我希望在使用排序时始终在顶部有一个特定的行。我能做些什么 ?

4

1 回答 1

0

您将需要按照 primefaces 4 guide pg 中的描述实现自己的排序功能。140

托管豆

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.component.datatable.DataTable;
import org.primefaces.model.SortOrder;

@ManagedBean
@ViewScoped
public class AlwaysOnTopBean implements Serializable {

    private List<Car> cars;

    @PostConstruct
    public void setup() {
        cars = new ArrayList<Car>();
        //I want b to be always on top
        cars.add(new Car("b"));
        cars.add(new Car("a"));
        cars.add(new Car("c"));
    }

    public int sort(Object o1, Object o2) {

        DataTable dataTable = (DataTable) FacesContext.getCurrentInstance().getViewRoot().findComponent("mainForm:carTable");

        SortOrder sortOrder = SortOrder.valueOf(dataTable.getSortOrder().toUpperCase(Locale.ENGLISH));
        boolean asc = sortOrder.equals(SortOrder.ASCENDING);

        String m1 = o1.toString();
        String m2 = o2.toString();

        if (m2.equals("b")) {
            return (asc) ? 1 : -1;
        }

        return m1.compareToIgnoreCase(m2);
    }

    public List<Car> getCars() {
        return cars;
    }

}

看法

<h:form id="mainForm">
    <p:dataTable id="carTable" value="#{alwaysOnTopBean.cars}" var="car">
        <p:column sortBy="model" headerText="Model" sortFunction="#{alwaysOnTopBean.sort}" >
            #{car.model}
        </p:column>
    </p:dataTable>
</h:form>
于 2013-10-17T19:54:01.103 回答