2009-08-16 176 views
1

我正在使用YUI數據表爲一些統計信息創建一些基本的客戶端排序,通過我創建的Web服務返回。不太想弄清楚YUI的ajax數據源的東西,所以我通過jquery和addRow()來加載數據。問題是,所有的數據都沒有正確排序。字符串字段似乎是一個隨機順序,並且數字/浮點列似乎按字符串排序(7顯示在635之上)。YUI DataTable排序

初始表定義如下:

var myColumnDefs = [ 
    {key:"appname",label:"Application Name", sortable:true}, 
    {key:"membercount",label:"Member Count",formatter:YAHOO.widget.DataTable.formatNumber,sortable:true}, 
    {key:"loggedin",label:"Logged In",formatter:YAHOO.widget.DataTable.formatNumber,sortable:true}, 
    {key:"peakoccupance",label:"Peak Occupance",formatter:YAHOO.widget.DataTable.formatNumber,sortable:true}, 
    {key:"averageoccupance",label:"Average Occupance",formatter:YAHOO.widget.DataTable.formatFloat,sortable:true} 
]; 

this.myDataSource = new YAHOO.util.DataSource([]); 
this.myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY; 
this.myDataSource.responseSchema = { 
    fields: [{key:"appname", parser:YAHOO.util.DataSourceBase.parseString}, 
     {key:"membercount", parser:YAHOO.util.DataSourceBase.parseNumber}, 
     {key:"loggedin", parser:YAHOO.util.DataSourceBase.parseNumber}, 
     {key:"peakoccupance", parser:YAHOO.util.DataSourceBase.parseNumber}, 
     {key:"averageoccupance", parser:YAHOO.util.DataSourceBase.parseNumber} 
    ] 
}; 

this.myDataTable = new YAHOO.widget.DataTable("markup", myColumnDefs, this.myDataSource, 
     {sortedBy:{key:"membercount",dir:"desc"}} 
); 

數據被加載這樣的:

function xmlCallback(xml){ 
    if($(xml).find('memberCount').text() != "0"){ 
    var appName = $(xml).find('appname').text(); 
    var appID = $(xml).find('appid').text();   
    var memberCount = $(xml).find('memberCount').text(); 
    var zoneOccupance = $(xml).find('zoneOccupance').text(); 
    var peakOccupance = $(xml).find('peakOccupance').text(); 
    var averageOccupance = $(xml).find('averageOccupance').text(); 

    console.log(appName+" loaded..."); 

    if(memberCount != "0"){ 

     var allData = { 
     row: count, 
     appname: appName, 
     membercount: memberCount, 
     loggedin: zoneOccupance, 
     peakoccupance: peakOccupance, 
     averageoccupance: averageOccupance 
     }; 

     sococo.myDataTable.addRow(allData, count); 
     count++; 
     $("appsloaded").html(count); 
    } 
    else{ 
     appCount--; 
     $("totalapps").html(appCount); 
    } 
    } 
} 

填充的一切,我只是得到一個法士特排序順序。我在這裏忘了什麼?

感謝, 喬希

回答

1

好了,在雅虎的樣本之一查找信息的隨機位後想通了這一點。顯然,如果您在更新之後未明確將sortedBy字段設置爲null,並且之前已聲明sortedBy選項,則它將保留先前找到的排序順序。我的猜測是某種緩存,所以它不會執行同樣昂貴的客戶端反覆排序。

這樣解決了這個代碼是以下幾點:

var allData = { 
    row: count, 
    appname: String(appName), 
    membercount: Number(memberCount), 
    loggedin: Number(zoneOccupance), 
    peakoccupance: Number(peakOccupance), 
    averageoccupance: Number(averageOccupance) 
}; 

sococo.myDataTable.addRow(allData, count); 
sococo.myDataTable.set("sortedBy", null); 
+0

如果您的數據不排序開始說起,簡單地離開了sortedBy領域。用sortedBy創建一個數據表格告訴YUI數據已經被排序,並且它應該保持這個排序順序以備將來排序。 – 2011-08-18 18:03:58