Reputation: 4174
Here When i click on the link get the appropriate id but change the status to all posts. Not in selected item
here html code
<div th:each="p : ${posts}">
<div id="para">
<a style="float: right;" href="#" class="decision"
th:text="${p.approved}">Approve</a>
<input type="hidden" class="pid" th:value="*{p.id}" />
</div>
</div>
when i click approve link get the id value is correct. but change status effect all other posts
Here is script
<script>
$(document).ready(function() {
$('.decision').click(function(e) {
$.ajax({
type : 'POST',
dataType : 'json',
url : '/RealEstate/ChangeStatus.html',
data : ({
pid :$(this).siblings('.pid').val()
}),
success : function(response) {
alert(response);
$('.decision').html(response);
/* $('.decision').text(response); */
},
error : function() {
alert('Error while request..');
}
});
});
});
</script>
and the controller
@RequestMapping("/ChangeStatus.html")
@ResponseBody
public String approvDisapprove(@RequestParam("pid") String pid) {
Gson gson = new Gson();
String data = null;
Property property = propertyDAO.findById(Integer.parseInt(pid));
if (property.getApproved() == true) {
property.setApproved(false);
data = gson.toJson("False");
} else {
property.setApproved(true);
data = gson.toJson("True");
}
propertyDAO.update(property);
return data;
}
if you know about this problem please share answer
Upvotes: 1
Views: 80
Reputation: 785
<script>
$(document).ready(function() {
$('.decision').click(function(e) {
$.ajax({
type : 'POST',
dataType : 'json',
url : '/RealEstate/ChangeStatus.html',
data : ({ pid :$(this).siblings('.pid').val() }),
})
.done(function(response) {
alert(response);
$(this).text(response);
})
.fail(function() {
alert('Error while request..');
});
});
});
</script>
Upvotes: 1
Reputation: 28513
You need to use this
reference like below :
<script>
$(document).ready(function() {
$('.decision').click(function(e) {
$.ajax({
type : 'POST',
dataType : 'json',
url : '/RealEstate/ChangeStatus.html',
data : ({
pid :$(this).siblings('.pid').val()
}),
success : function(response) {
alert(response);
$(this).html(response);
^------^ // here use this instead of .decision selector
/* $('.decision').text(response); */
},
error : function() {
alert('Error while request..');
}
});
});
});
</script>
Upvotes: 3