Hello All,
I have this function to search person info. When I click a search button, it retrieves
some info. and adds to a table as rows. I have the function to click "trs" and then
do something with it which calls "SetDevMember" function. I need to pass the id attached to that
particular row to this function. But right now it is always retrieving the last row's id.
function GetPersonInfo()
{
$.ajax({
type: 'GET',
url: "@Url.Action("GetPersonDetails", "Emp")" + "?first=" + $("#FirstName").val() + "&last=" + $("#LastName").val() ,
success: function(responseobj, status) {
if (responseobj.length > 10) {
$("#SearchResults").find("tr:gt(0)").remove();
$('#SearchResults').show();
var nextRow=$("<tr><td>"+ "Search retrieved more than 10 results. Please narrow your search" + "</td></tr>");
$('#SearchResults tr:last').after(nextRow);
}
else
{
$("#SearchResults").find("tr:gt(0)").remove();
for(var i =0;i < responseobj.length;++i) {
var item = responseobj[i];
$('#SearchResults').show();
var nextRow=$("<tr><td>"+ "Name: " + item.FirstName + " " + item.LastName + "</br>" + "Email: " + item.EmailAddress + "</td></tr>");
$('#SearchResults tr:last').after(nextRow);
$('#SearchResults tr').addClass('SearchResultItem ui-corner-all');
$('#SearchResults tr').hover(function () {
$(this).addClass('ui-state-hover');
}, function () {
$(this).removeClass('ui-state-hover');
}).click(function () {
var name = item.FirstName + " " + item.LastName;
SetDevMember(item.PersonId, name);
});
};
}
},
error: function()
{
console.log("Bad");
}
});
}
The value of item at the time you click is always the last one, which is what you are seeing. This is the sequence of events:
The for-loop executes, doing it's thing. After the for-loop finishes executing, item is now pointing at the last item.
Then you click, and pass the value of item.
This should get you your current item:
.click(function () {
var ndx = $(this).prevAll().length; //count the number of TRs before us
var myitem = responseobj[ndx];
var name = myitem.FirstName + " " + myitem.LastName;
SetDevMember(myitem.PersonId, name);
})
It might be off by one index, so you might have to subtract one from ndx. I didn't look that closely, but that will put you on the right track.
nissan
Participant
1065 Points
618 Posts
Not retrieving the correct id
Apr 24, 2012 03:42 PM|LINK
Hello All, I have this function to search person info. When I click a search button, it retrieves some info. and adds to a table as rows. I have the function to click "trs" and then do something with it which calls "SetDevMember" function. I need to pass the id attached to that particular row to this function. But right now it is always retrieving the last row's id. function GetPersonInfo() { $.ajax({ type: 'GET', url: "@Url.Action("GetPersonDetails", "Emp")" + "?first=" + $("#FirstName").val() + "&last=" + $("#LastName").val() , success: function(responseobj, status) { if (responseobj.length > 10) { $("#SearchResults").find("tr:gt(0)").remove(); $('#SearchResults').show(); var nextRow=$("<tr><td>"+ "Search retrieved more than 10 results. Please narrow your search" + "</td></tr>"); $('#SearchResults tr:last').after(nextRow); } else { $("#SearchResults").find("tr:gt(0)").remove(); for(var i =0;i < responseobj.length;++i) { var item = responseobj[i]; $('#SearchResults').show(); var nextRow=$("<tr><td>"+ "Name: " + item.FirstName + " " + item.LastName + "</br>" + "Email: " + item.EmailAddress + "</td></tr>"); $('#SearchResults tr:last').after(nextRow); $('#SearchResults tr').addClass('SearchResultItem ui-corner-all'); $('#SearchResults tr').hover(function () { $(this).addClass('ui-state-hover'); }, function () { $(this).removeClass('ui-state-hover'); }).click(function () { var name = item.FirstName + " " + item.LastName; SetDevMember(item.PersonId, name); }); }; } }, error: function() { console.log("Bad"); } }); }Motley
Star
13789 Points
2449 Posts
MVP
Re: Not retrieving the correct id
Apr 24, 2012 04:07 PM|LINK
The value of item at the time you click is always the last one, which is what you are seeing. This is the sequence of events:
The for-loop executes, doing it's thing. After the for-loop finishes executing, item is now pointing at the last item.
Then you click, and pass the value of item.
This should get you your current item:
.click(function () { var ndx = $(this).prevAll().length; //count the number of TRs before us var myitem = responseobj[ndx]; var name = myitem.FirstName + " " + myitem.LastName; SetDevMember(myitem.PersonId, name); })It might be off by one index, so you might have to subtract one from ndx. I didn't look that closely, but that will put you on the right track.
nissan
Participant
1065 Points
618 Posts
Re: Not retrieving the correct id
Apr 24, 2012 05:02 PM|LINK
Thank you for explaining me. That did work. I had to substract one though...