samedi 27 décembre 2014

jQuery Code Only Works When Alerts are Active



I posted this to Stack Exchange as well, and I was wondering if some of my fellow SharePoint developers could help me out. This is not a SharePoint direct question, but I am building a site collection feature with a ribbon button. Here is my x-post:


I have created a feature on SharePoint to export selected items from a view to Excel. The code I have works when the alerts are present. I know I have an asynchronous problem within my function and I need some help figuring it out. I do not quite understand how to use the $.Deferred() methods, and when I try, I get errors. Most likely because I am doing it wrong. Here is my code, all works and populates fine when I activate my alerts:



function get_Data() {

var ctx = SP.ClientContext.get_current();
var items = SP.ListOperation.Selection.getSelectedItems(ctx);
var i;



context = GetCurrentCtx();
list = context.ListTitle;
view = context.view;

viewFields(list, view);

//for (i in items){

for (i=0; i<items.length; i++){
myItems = items[i].id;
//alert(myItems);

$().SPServices({
operation: "GetListItems",
async: false,
listName: list,
viewName: view,
CAMLQuery: "<Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + myItems + "</Value></Eq></Where></Query>",


completefunc: function(xdata, status){

//alert(xdata.responseXML.xml);
$(xdata.responseXML).SPFilterNode("z:row").each(function() {

var xitem = $(this);

for (var z = 0; z < values.length; z++){
var variables = xitem.attr(""+ values[z] +"");
//alert(variables);
dataItems.push("<td>" + variables + "</td>");
}
tableRows.push("<tr>" + dataItems + "</tr>");
dataItems.length = 0;
//alert(tableRows);

});
}
});


td = tableRows.toString();
td = td.replace(/,/g, "");

//alert(td);
}


var th = headers.toString();
th = th.replace(/,/g, "");


//alert(th);


hidden = "<div class='my_hidden'><table class='hiddenTable' id='hiddenTable'><tbody><tr>" + th + "</tr>" + td + "</tbody></table></div>"
$("#aspnetForm").append(hidden);

success();

}


I need the success(); function to run after all to the nested loops have completed. It is firing before anything gets started it seems. The alerts keep it form skipping over the loops and .each functions. How do I get the loops to finish before I run the success(); function? This is my last piece and I am done with this code. Any help would be greatly appreciated. Thanks!








0 commentaires:

Enregistrer un commentaire