有没有人知道是否有可用的插件允许表缓存ajax请求的行。
目前,datatables能够通过ajax源实现分页。我需要缓存已经请求的行。 1.表现 2.我需要从请求的行访问输入字段。
任何人都知道怎么做?
感谢。
答案 0 :(得分:0)
适合任何需要此功能的人。我创建了一个缓存所有渲染行的文件
适用于jquery 1.10.2和dataTables 1.10.0
$.fn.dataTable.ajaxCaching = function () {
var jsonData = null;
var reqStart = null;
var reqEnd = null;
var displayStart = null;
var displayLength = null;
var req = {};
function _assembleRequest(request) {
$(request).each(function () {
req[this.name] = this;
});
}
function _checkRows() {
var ret = false;
if (jsonData == null) return true;
// check for cached rows; update req start at the start of unrequested row
for (var i = reqStart; i < (reqStart + reqLength); i++) {
if (jsonData.aaData[i] == undefined) {
reqStart = i;
ret = true;
break;
};
}
// if not all rows are requested, find last row for the range to be requested
if (ret) {
for (var i = (reqStart + reqLength) - 1; i > reqStart; i--) {
if (jsonData.aaData[i] == undefined) {
reqEnd = i;
break;
}
}
}
return ret;
}
return function (sSource, aoData, fnCallback, oSettings) {
_assembleRequest(aoData);
reqStart = displayStart = req.iDisplayStart.value;
reqLength = displayLength = req.iDisplayLength.value;
oSettings._iDisplayStart = displayStart;
oSettings._iDisplayLength = displayLength;
// Request Rows
if (_checkRows()) {
req.iDisplayStart.value = reqStart;
req.iDisplayLength.value = reqLength;
$.ajax({
'dataType': 'json',
'type': 'POST',
'url': sSource,
'data': aoData,
'success': function (json) {
// update cached data
if (jsonData == null) {
jsonData = json;
} else {
for (var i = 0; i < json.aaData.length; i++) {
jsonData.aaData[i + reqStart] = json.aaData[i];
}
json.aaData = jsonData.aaData.slice(displayStart, (displayStart + displayLength));
}
oSettings.cachedData = jsonData.aaData;
fnCallback(json);
}
});
} else {
jsonData.sEcho = req.sEcho.value;
// render cached data
var json = $.extend(true, {}, jsonData);
json.iDisplayStart = displayStart;
json.iDisplayLength = displayLength;
json.aaData = jsonData.aaData.slice(displayStart, (displayStart + displayLength));
oSettings.cachedData = jsonData.aaData;
fnCallback(json);
}
};
};
用法:
$('#Table').dataTable({
'bServerSide': true,
'sAjaxSource': 'url/to/web/api',
'fnServerData': $.fn.dataTable.ajaxCaching(),
....
});