数据源无法使用分页映射到转换后的 json
datasource failing to map to the transformed json using pagination
我正在使用 pagination.js 通过以下代码片段加载异步数据。
$('#demo').pagination({
dataSource: 'https://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?',
locator: 'items',
totalNumber: 120,
pageSize: 20,
ajax: {
beforeSend: function() {
dataContainer.html('Loading data from flickr.com ...');
}
},
callback: function(data, pagination) {
// template method of yourself
var html = template(data);
dataContainer.html(html);
}
})
由于我的json结构不同,我需要在映射到数据源之前转换我的json,所以我将上面的代码更改为如下所示:
$('#demo').pagination({
dataSource: getTransformedJson(),
locator: 'items',
totalNumber: 120,
pageSize: 25,
ajax: {
beforeSend: function() {
container.prev().html('Loading products data ...');
}
},
callback: function(response, pagination) {
var dataHtml = '<ul>';
console.log('response', response)
$.each(response, function (index, item) {
dataHtml += '<li>' + item.productName + '</li>';
});
dataHtml += '</ul>';
container.prev().html(dataHtml);
}
})
});
但是在回调中,console.log('response', response)
始终在浏览器控制台中显示空数组 response []
。下面是我的 getTransformedJSON()
函数:
function getTransformedJson() {
$.getJSON('./products.json', function(data) {
transformedData = data;
totalCount = data['response']['totalCount'];
transformedData['items'] = data['response']['results'];
delete transformedData['response']["results"];
console.log(transformedData);
});
return transformedData;
}
但在我的 getTransformedJson()
函数中,console.log(transformedData);
显示正确转换的 json 数据。请对此进行任何修复。
像这样:
dataSource: function(done) {
$.getJSON('./products.json', function(data) {
transformedData = data;
totalCount = data['response']['totalCount'];
transformedData['items'] = data['response']['results'];
delete transformedData['response']["results"];
done(transformedData);
});
}
我正在使用 pagination.js 通过以下代码片段加载异步数据。
$('#demo').pagination({
dataSource: 'https://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?',
locator: 'items',
totalNumber: 120,
pageSize: 20,
ajax: {
beforeSend: function() {
dataContainer.html('Loading data from flickr.com ...');
}
},
callback: function(data, pagination) {
// template method of yourself
var html = template(data);
dataContainer.html(html);
}
})
由于我的json结构不同,我需要在映射到数据源之前转换我的json,所以我将上面的代码更改为如下所示:
$('#demo').pagination({
dataSource: getTransformedJson(),
locator: 'items',
totalNumber: 120,
pageSize: 25,
ajax: {
beforeSend: function() {
container.prev().html('Loading products data ...');
}
},
callback: function(response, pagination) {
var dataHtml = '<ul>';
console.log('response', response)
$.each(response, function (index, item) {
dataHtml += '<li>' + item.productName + '</li>';
});
dataHtml += '</ul>';
container.prev().html(dataHtml);
}
})
});
但是在回调中,console.log('response', response)
始终在浏览器控制台中显示空数组 response []
。下面是我的 getTransformedJSON()
函数:
function getTransformedJson() {
$.getJSON('./products.json', function(data) {
transformedData = data;
totalCount = data['response']['totalCount'];
transformedData['items'] = data['response']['results'];
delete transformedData['response']["results"];
console.log(transformedData);
});
return transformedData;
}
但在我的 getTransformedJson()
函数中,console.log(transformedData);
显示正确转换的 json 数据。请对此进行任何修复。
像这样:
dataSource: function(done) {
$.getJSON('./products.json', function(data) {
transformedData = data;
totalCount = data['response']['totalCount'];
transformedData['items'] = data['response']['results'];
delete transformedData['response']["results"];
done(transformedData);
});
}