我正在使用YUI数据表为某些统计信息创建一些基本的客户端排序,通过我创建的Web服务返回。不太想弄清楚YUI的ajax数据源的东西,所以我在通过jquery和addRow()加载数据。问题是,所有数据都没有正确排序。字符串字段似乎是一个随机顺序,数字/浮点列似乎按字符串排序(7出现在635以上)。
初始表定义如下:
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 );
}
}
}
所有东西都填充,我只是得到一个shite排序顺序。我忘记了什么?
谢谢, 约什
答案 0 :(得分: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 );