我正在使用jQuery请求并返回它的xhr对象。完成后,我传递这个xhr对象,以便我可以构建一个图表。
见下面的代码:
function getPieData() {
getData('@Url.Action("AjaxPieDataOverdue", "KPIs")').done(createPie, 'overdue');
getData('@Url.Action("AjaxPieDataApproaching", "KPIs")').done(createPie, 'approaching');
getData('@Url.Action("AjaxPieDataWithin", "KPIs")').done(createPie, 'within');
};
function getData(url) {
return $.ajax({
type: "GET",
url: url,
contentType: "application/json; charset=utf-8",
success: function (data) {
},
error: function (data) {
}
});
};
function createPie(data, type) {
alert(data);
$('#jqxwindow-pie').jqxWindow('open');
var source =
{
datatype: "json",
datafields: [
{ name: 'SectionCount' },
{ name: 'SectionStatus' }
],
localdata: data
};
var dataAdapter = new $.jqx.dataAdapter(source, { async: false, autoBind: true, loadError: function (xhr, status, error) { alert('Error loading "' + source.url + '" : ' + error); } });
// prepare jqxChart settings
var settings = {
title: "Desktop browsers share in Dec 2011",
description: "(source: wikipedia.org)",
enableAnimations: true,
showLegend: false,
legendPosition: { left: 520, top: 140, width: 100, height: 100 },
padding: { left: 5, top: 5, right: 5, bottom: 5 },
titlePadding: { left: 0, top: 0, right: 0, bottom: 10 },
source: dataAdapter,
colorScheme: 'scheme02',
seriesGroups:
[
{
type: 'pie',
showLabels: true,
series:
[
{
dataField: 'SectionCount',
displayText: 'SectionStatus',
labelRadius: 100,
initialAngle: 15,
radius: 130,
innerRadius: 50,
centerOffset: 0,
formatSettings: { sufix: '%', decimalPlaces: 1 }
}
]
}
]
};
if (type == 'overdue') {
// setup the chart
alert("overdue");
$('#chartContainer-pie-overdue').jqxChart(settings);
}
else if (type == 'approaching') {
alert("approaching");
$('#chartContainer-pie-approaching').jqxChart(settings);
} else {
alert("approaching");
$('#chartContainer-pie-within').jqxChart(settings);
}
}
在这个例子中:
getData('@Url.Action("AjaxPieDataOverdue", "KPIs")').done(createPie, 'overdue');
逾期没有传递给我的函数。我想将此变量用于if / else块进一步向下。如何传递此变量?
答案 0 :(得分:2)
例如,您可以在done
回调中使用匿名函数来传递这样的额外参数:
getData('...').done(function(data){createPie(data,'overdue');});