Commenting code changes behavior of prior code
Guys - I have no idea what is causing my code to break here and I'd love your help to find out why! The function you see below, gets a row from a web service and builds a table row based on the data that is returned.
function GetData(id, thisRow) {
if (id == 0) {
var tier = 0;
}
else {
var currenttier = parseInt(thisRow.find("td.Tier").text());
var tier = currenttier + 1;
}
//*** this console.log is affected by the for loop that appears later..
console.log(tier);
$.ajax({
type: "POST",
url: "F2.svc/GetChildTable",
data: id,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data, textStatus, xhr) {
var tempstring = "";
var temp = "";
if (data.Rows.length > 0) {
for (var i = 0; i < data.Rows.length; i++) {
tempstring += "<tr class = 'Show Row ItemID-" + data.Rows[i].ItemID + " ParentID-" + data.Rows[i].ItemParentID + " Tier-" + tier + "'>";
tempstring += "<td class='Tier'>";
//*** the loop below seems to break the tier variable declared up above..
for (var p = 0; p < tier; p++) {
tempstring += "+";
}
tempstring += " " + tier;
tempstring += "</td>";
tempstring += "<td class='ItemID'>";
tempstring += data.Rows[i].ItemID;
tempstring += "</td>";
tempstring += "<td class='ItemParentID'>";
tempstring += data.Rows[i].ItemParentID;
tempstring += "</td>";
tempstring += "<td class='ItemDateEntered'>";
tempstring += data.Rows[i].ItemDateEntered;
tempstring += "</td>";
tempstring += "<td class='ItemLastUpdated'>";
tempstring += data.Rows[i].ItemLastUpdated;
tempstring += "</td>";
tempstring += "<td class='ItemName'>";
tempstring += data.Rows[i].ItemName;
tempstring += "</td>";
tempstring += "<td class='ItemStatusID'>";
tempstring += data.Rows[i].ItemStatusID;
tempstring += "</td>";
tempstring += "<td class='ItemTasks'>";
tempstring += data.Rows[i].ItemTasks;
tempstring += "</td>";
tempstring += "<td class='ItemBlocks'>";
tempstring += data.Rows[i].ItemBlocks;
tempstring += "</td>";
tempstring += "<td class='ItemComments'>";
tempstring += data.Rows[i].ItemComments;
tempstring += "</td>";
tempstring += "<td class='ItemFilterID'>";
tempstring += data.Rows[i].ItemFilterID;
tempstring += "</td>";
tempstring += "<td class='ItemManHoursEst'>";
tempstring += data.Rows[i].ItemManHoursEst;
tempstring += "</td>";
tempstring += "<td class='ItemManHoursAct'>";
tempstring += data.Rows[i].ItemManHoursAct;
tempstring += "</td><td></td>"
}
tempstring += "</tr>";
var newRow = $(tempstring);
if (id == 0) {
$("table#Main > tbody").html(newRow);
}
else {
thisRow.after(newRow);
thisRow.addClass("ChildrenLoaded");
}
}
},
error: function (xhr, textStatus, ex) {
var response = xhr.responseText;
if (response.length > 11 && response.substr(0, 11) === '{"Message":' &&
response.charAt(response.length - 1) === '}') {
var exInfo = JSON.parse(xhr.responseText);
var text = "Message: " + exInfo.Message + "\r\n" +
"Exception: " + exInfo.ExceptionType; // + exInfo.StackTrace;
alert(text);
} else {
alert("error");
}
}
});
}
So as you can see, the console.log(tier) displays NaN when it should display 2. What's really weird is that if I comment out the for loop that contains tempstring += "+"; The console.log works as you'd expect. The only relationship I can see, is that the for loop uses p < tier but that shouldn't have any affect on the tier variable declaration earlier on, right?
Oh I guess it also goes without saying that tempstring += " " + tier; is broken too ^^
Thank开发者_如何学Go you!
Lots of code, no HTML dump, but I have a guess.
It seems that it runs fine on the first try, but:
var currenttier = parseInt(thisRow.find("td.Tier").text());
Kills you afterwards since you add one +
per tier:
console.log(parseInt('++ 1')); // NaN
You have to strip the pluses, otherwise your number will not parse.
Your issue is actually not in the loop, but in this code:
var currenttier = parseInt(thisRow.find("td.Tier").text());
var tier = currenttier + 1;
What's happening here is that you're parsing "0" the first time, and it works fine, because "0" is an int.
But the second time, you're parsing "+ 1" which does not give a valid int, but NaN. This can be solved by changing the first line to:
var currenttier = parseInt(thisRow.find("td.Tier").text().replace(/\D/g, ''));
精彩评论