我第一次使用jquery .promise而且我做错了。我收到语法错误。请帮忙。我有一个SharePoint站点,我使用REST API来查询列表。我的目标是计算故障单类型并创建jqplot图表。我尝试使用jquery .promise时出现语法错误,但看起来很好。任何帮助将不胜感激。
var ticketArray = new Array();
var myData = new Array();
var i = 0;
$.getJSON("../practice/_vti_bin/ListData.svc/TicketType", function(data) {
//iterate through all returned Ticket list items and store in an object
$.each(data.d.results, function(i, result) {
//get item property
var TicketObj = {};
TicketObj.TicketType = result.Title;
TicketObj.Count = 0;
ticketArray[i] = TicketObj;
i++;
});
promise = $.ajax({
type: "GET",
//dataType: "json",
url: "../practice/_vti_bin/ListData.svc/TicketingSystems?$select=TypeOfTicket",
cache: false
});
promise.done(function(data) {
//success : function(data) {
//iterate through all returned Ticket list items and get count
$.each(data.d.results, function(i, result) {
for (var x = 0; x < ticketArray.length; x++) {
//console.log("Ticket Type: "+result.TypeOfTicket);
if (ticketArray[x].TicketType == result.TypeOfTicket) {
ticketArray[x].Count += 1;
console.log("Ticket Count: " + ticketArray[x].Count);
}
}
}); //each()
)
};
promise.done(function(data) {
$.each(ticketArray, function(index, value) {
myData.push([ticketArray[index].TicketType, ticketArray[index].Count]);
});
)
};
//}
var plot1 = jQuery.jqplot('chart1', [myData], {
seriesDefaults: {
renderer: jQuery.jqplot.PieRenderer,
rendererOptions: {
showDataLabels: true
}
},
legend: {
show: true,
location: 'e'
}
}); //end plot1
答案 0 :(得分:0)
按原样使用以下脚本,如果有问题请告诉我:)
添加对这三个脚本的引用:
1. /_layouts/15/SP.Runtime.js
2./_layouts/15/SP.js
3. //ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js
<script type="text/javascript">
$(document).ready(function () {
var ticketArray = new Array();
var myData = new Array();
getListData('TicketType').then(function (items) {
var ListEnumeratorTT = items.getEnumerator();
var i = 0;
while (ListEnumeratorTT.moveNext()) {
var currentItem = ListEnumeratorTT.get_current();
var TicketObj = {};
TicketObj.TicketType = currentItem.get_item('Title');
TicketObj.Count = 0;
ticketArray[i] = TicketObj;
i += 1;
}
getListData('TicketingSystems').then(function (items) {
var ListEnumeratorTS = items.getEnumerator();
var i = 0;
while (ListEnumeratorTS.moveNext()) {
var currentItem = ListEnumeratorTS.get_current();
for (var x = 0; x < ticketArray.length; x++) {
if (ticketArray[x].TicketType == currentItem.get_item('TypeOfTicket')) {
ticketArray[x].Count += 1;
}
}
}
$.each(ticketArray, function (index, value) {
myData.push([ticketArray[index].TicketType, ticketArray[index].Count]);
});
var plot1 = jQuery.jqplot('chart1', [myData], {
seriesDefaults: {
renderer: jQuery.jqplot.PieRenderer,
rendererOptions: {
showDataLabels: true
}
},
legend: { show: true, location: 'e' }
});//end plot1
});
});
});
function getListData(listTitle, success, error) {
var dfd = $.Deferred(function () {
var context = SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle(listTitle);
var items = list.getItems(SP.CamlQuery.createAllItemsQuery());
context.load(items);
context.executeQueryAsync(
function () {
dfd.resolve(items);
},
function (sender, args) {
dfd.reject(args);
}
);
});
return dfd.promise();
}
</script>
答案 1 :(得分:0)
以下对我有用。
<script type="text/javascript">
$(document).ready(function() {
var ticketArray = new Array();
var myData = new Array();
var i=0;
$.getJSON("../practice/_vti_bin/ListData.svc/TicketType",function(data) {
//iterate through all returned Ticket list items and store in an object
$.each(data.d.results, function(i,result) {
//get item property
var TicketObj = {};
TicketObj.TicketType = result.Title;
TicketObj.Count = 0;
ticketArray[i] = TicketObj;
i++;
});
promise = $.getJSON("../practice/_vti_bin/ListData.svc/TicketingSystems?$select=TypeOfTicket",function(data) {
console.log("#1");
//iterate through all returned Ticket list items and get count
$.each(data.d.results, function(i,result) {
for (var x=0; x<ticketArray.length; x++)
{
if (ticketArray[x].TicketType == result.TypeOfTicket)
{
ticketArray[x].Count +=1;
}
}
});//each()
});//getJSON
promise.done(function(data) {
console.log("#2");
$.each(ticketArray, function (index, value) {
myData.push([ticketArray[index].TicketType, ticketArray[index].Count]);
});
var plot1 = jQuery.jqplot('chart1',[myData], {
seriesDefaults: {
renderer: jQuery.jqplot.PieRenderer,
rendererOptions: {
showDataLabels: true
}
},
legend: { show:true, location: 'e' }
});//end plot1
});
});