Primefaces:DataTable再现排序数据

时间:2012-09-27 10:06:45

标签: database sorting primefaces datatable

我遇到了Primefaces DataTable的问题。我想构建一个带有过滤和排序的复杂数据表。我是Primefaces的新手,所以我研究了Primefaces Showcase上的例子。

如果您第一次点击排序按钮,则会复制整个列表并进行排序。在第二次点击时,它会正常排序。

输出如下:

初始:列表有3个项目。 第一次点击:列表有6个项目,并按ASC排序 第二次点击:列表有6个项目,并按DES排序

我很困惑,找不到错误。请帮我解决一下这个。

注意:我在Apache Tomcat 7.x上使用Primefaces 3.4和JSF 2.1

Dashboard.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition 
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">

<h:form id="frm_dashboard">

    <p:growl autoUpdate="true" showDetail="true" />

    <p:dataTable var="AntragsDatenObj" value="#{dashboardController.obj_offAntraege}" rowKey="#{AntragsDatenObj.antr_id}" paginator="false" rows="20"  
                 selection="#{dashboardController.selektierterAntrag}" selectionMode="single" id="dt_AntragsDatenObj" style="font-size: 12px; width: 96%">  

        <p: ajax event="rowSelect" update=":frm_dashboard:display" oncomplete="Dialog.show()" />  

        <f:facet name="header">  
            Liste von Anträgen  
        </f:facet>  

        <p:column headerText="ID" sortBy="#{AntragsDatenObj.antr_id}" id="dt_antr_id" style="font-size: 12px;">  
            #{AntragsDatenObj.antr_id}  
        </p:column>  

        <p:column headerText="Start" sortBy="#{AntragsDatenObj.urlb_startdatum}" id="dt_urlb_startdatum" style="font-size: 12px;">  
            #{AntragsDatenObj.urlb_startdatum}  
        </p:column>  

        <p:column headerText="Ende" sortBy="#{AntragsDatenObj.urlb_enddatum}" id="dt_urlb_enddatum" style="font-size: 12px;">  
            #{AntragsDatenObj.urlb_enddatum}  
        </p:column>  

        <p:column headerText="Länge" sortBy="#{AntragsDatenObj.urlb_laenge}" id="dt_urlb_laenge" style="font-size: 12px;">  
            #{AntragsDatenObj.urlb_laenge}  
        </p:column>

        <p:column headerText="Benutzer" sortBy="#{AntragsDatenObj.user_name}" id="dt_user_name" style="font-size: 12px;">  
            #{AntragsDatenObj.user_name}  
        </p:column>

    </p:dataTable>

<p:dialog header="Details" widgetVar="Dialog" resizable="false"  
           width="200" showEffect="explode" hideEffect="explode">  

     <h:panelGrid id="display" columns="2" cellpadding="4">  

         <f:facet name="header">  
             Details
         </f:facet>  

         <h:outputText value="ID:" />  
         <h:outputText value="#{dashboard.selektierterAntrag.antr_id}" id="antr_id"/>  

         <h:outputText value="Startdatum:" />  
         <h:outputText value="#{dashboard.selektierterAntrag.urlb_startdatum}" id="urlb_startdatum"/>  

         <h:outputText value="Enddatum:" />  
         <h:outputText value="#{dashboard.selektierterAntrag.urlb_enddatum}" id="urlb_enddatum"/>  

         <h:outputText value="Länge:" />  
         <h:outputText value="#{dashboard.selektierterAntrag.urlb_laenge}" id="urlb_laenge"/>  
     </h:panelGrid>  
 </p:dialog>  

</h:form>  

MangedBean名为DashboardController.java     package lean.controller;

import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import lean.dataobjects.AntragsDatenObj;


@ViewScoped
@ManagedBean
public class DashboardController {

    private List<AntragsDatenObj> obj_offAntraege;

    private AntragsDatenObj selektierterAntrag;

    public List<AntragsDatenObj> getObj_offAntraege() {
        return obj_offAntraege;
    }

    public AntragsDatenObj getSelektierterAntrag() {
        return selektierterAntrag;
    }

    public void setSelektierterAntrag(AntragsDatenObj selektierterAntrag) {
        this.selektierterAntrag = selektierterAntrag;
    }

    public DashboardController() {

        try{

            this.obj_offAntraege = lean.sql.QueryLibrary.getOffneAntraegeMitDaten();

        } catch (Exception e){

            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"#DashBoard001 KOMPONENTE: getoffneAntraege ist abgestürzt: ",e.getMessage()));

        }

    }

}

DataObject AntragsDatenObj.java     package lean.dataobjects;

import java.util.Date;


public class AntragsDatenObj {

    private int antr_id;
    private int antr_status;
    private Date urlb_startdatum;
    private Date urlb_enddatum;
    private int urlb_laenge;
    private String user_name;

    public AntragsDatenObj(int antr_id, int antr_status, Date urlb_startdatum, Date urlb_enddatum, int urlb_laenge, String user_name) {
        this.antr_id = antr_id;
        this.antr_status = antr_status;
        this.urlb_startdatum = urlb_startdatum;
        this.urlb_enddatum = urlb_enddatum;
        this.urlb_laenge = urlb_laenge;
        this.user_name = user_name;
    }

    public int getAntr_id() {
        return antr_id;
    }

    public int getAntr_status() {
        return antr_status;
    }

    public Date getUrlb_startdatum() {
        return urlb_startdatum;
    }

    public Date getUrlb_enddatum() {
        return urlb_enddatum;
    }

    public int getUrlb_laenge() {
        return urlb_laenge;
    }

    public String getUser_name() {
        return user_name;
    }

}

1 个答案:

答案 0 :(得分:0)

我为我的英语道歉。

datatable.js第108行存在问题

sortorder = columnHeader.data('sortorder');

if(sortorder) {
    if(sortorder === 'DESCENDING')
        _self.sort(columnHeader, 'ASCENDING');
    else if(sortorder === 'ASCENDING')
        _self.sort(columnHeader, 'DESCENDING');
}
else {
    _self.sort(columnHeader, 'ASCENDING');
}

即使存在选定的已排序列,最初columnHeader.data('sortorder')也为空。

关于ajax成功的

修复:,例如

$('body').ajaxSuccess(...);

function() {
    $('.ui-datatable .ui-sortable-column.ui-state-active').each(function() {
        column = $(this);

        if (!column.data('sortorder')) {
            if (column.find('.ui-icon-triangle-1-n')) {
                column.data('sortorder', 'ASCENDING');
            } else if (column.find('.ui-icon-triangle-1-s')) {
                column.data('sortorder', 'DESCENDING');
            }
        }
    });
}