默认情况下,此函数与$_GET一起使用.基于
on this discussion我修改了
this function并得到了类似下面的内容.现在的问题是,Firebug给出了错误消息
json.aaData is undefined @ line 99
这是行(位于代码末尾):
json.aaData.splice( 0, iRequestStart-oCache.iCacheLower );
PHP端响应,此表100%无需流水线工作.但是当我启用管道传输时出现此错误:
http://screencast.com/t/GOJzPHq3kg
function fnDataTablesPipeline ( sSource, aoData, fnCallback ) {
var iPipe = 5; /* Ajust the pipe size */
var bNeedServer = false;
var sEcho = fnGetKey(aoData, "sEcho");
var iRequestStart = fnGetKey(aoData, "iDisplayStart");
var iRequestLength = fnGetKey(aoData, "iDisplayLength");
var iRequestEnd = iRequestStart + iRequestLength;
oCache.iDisplayStart = iRequestStart;
/* outside pipeline? */
if ( oCache.iCacheLower < 0 || iRequestStart < oCache.iCacheLower || iRequestEnd > oCache.iCacheUpper )
{
bNeedServer = true;
}
/* sorting etc changed? */
if ( oCache.lastRequest && !bNeedServer )
{
for( var i=0, iLen=aoData.length ; i<iLen ; i++ )
{
if ( aoData[i].name != "iDisplayStart" && aoData[i].name != "iDisplayLength" && aoData[i].name != "sEcho" )
{
if ( aoData[i].value != oCache.lastRequest[i].value )
{
bNeedServer = true;
break;
}
}
}
}
/* Store the request for checking next time around */
oCache.lastRequest = aoData.slice();
if ( bNeedServer )
{
if ( iRequestStart < oCache.iCacheLower )
{
iRequestStart = iRequestStart - (iRequestLength*(iPipe-1));
if ( iRequestStart < 0 )
{
iRequestStart = 0;
}
}
oCache.iCacheLower = iRequestStart;
oCache.iCacheUpper = iRequestStart + (iRequestLength * iPipe);
oCache.iDisplayLength = fnGetKey( aoData, "iDisplayLength" );
fnSetKey( aoData, "iDisplayStart", iRequestStart );
fnSetKey( aoData, "iDisplayLength", iRequestLength*iPipe );
jQuery.post( sSource, aoData, function (data) {
/* Callback processing */
oCache.lastJson = jQuery.extend(true, {}, data);
if ( oCache.iCacheLower != oCache.iDisplayStart )
{
data.aaData.splice( 0, oCache.iDisplayStart-oCache.iCacheLower );
}
data.aaData.splice( oCache.iDisplayLength, data.aaData.length );
fnCallback(data)
},"json" );
}
else
{
json = jQuery.extend(true, {}, oCache.lastJson);
json.sEcho = sEcho; /* Update the echo for each response */
json.aaData.splice( 0, iRequestStart-oCache.iCacheLower ); // <- this line
json.aaData.splice( iRequestLength, json.aaData.length );
fnCallback(json);
return;
}
}
我错过了什么?有什么建议吗?
最佳答案 json从oCache.lastJson获取fnDataTablesPipeline()内部的属性,但仅当bNeedServer为true时才获取.
如果fnDataTablesPipeline()从未被调用或已被调用但bNeedServer仍为false,那么行oCache.lastJson = jQuery.extend(true,{},data);将不会被执行,json.aaData.splice()将失败.
您需要处理oCache.lastJson未填充(或仅部分填充)的情况:
>通过使用默认属性初始化oCache.lastJson
>通过在尝试使用它们并相应地执行之前测试属性的存在,
例如:
else{
if(oCache.lastJson.aaData) {
json = jQuery.extend(true, {}, oCache.lastJson);
json.sEcho = sEcho; /* Update the echo for each response */
json.aaData.splice( 0, iRequestStart-oCache.iCacheLower );
json.aaData.splice( iRequestLength, json.aaData.length );
fnCallback(json);
}
}
我不知道这是否100%正确.你可能需要在if子句中加入较少的内容,例如,仅抑制从json.aaData.splice开始的两行可能是合适的….