[英]Dynamically update value of a table row using ajax
I am trying to update the value (views) of a table row using Ajax when an onclick event is called. 我正在尝试在调用onclick事件时使用Ajax更新表行的值(视图)。
The views are updated but the problem is that I can't get the new value of the views to show on that particular table row. 视图已更新,但问题是我无法获取要在该特定表行上显示的视图的新值。
I have to refresh the page to see the updated views value and that's not what I want. 我必须刷新页面才能看到更新的视图值,这不是我想要的。
I want it updated dynamically as the button is clicked. 我希望它在单击按钮时动态更新。 This is what I am doing. 这就是我在做的事情。
<table>
@foreach($data as $datum)
<tr>
<td><span class="view2">{{$datum->views}}</span></td>
<td><button class="btn btn-default views" value="{{$datum->game_id}}">click</button></td>
</tr>
@endforeach
</table>
<script>
$('.views').click(function (event) {
var game_id = this.value;
//alert(game_id);
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
type:'POST',
dataType:'JSON',
url:'{{url('home/update-views')}}',
data:'game_id='+game_id,
success:function(data)
{
var data=eval(data);
message=data.message;
var countSpan = $(this).parent().parent().find(".view2");
countSpan.text(message);
}
});
})
</script>
2 change required. 2需要更改。
1.captur the object into a variable so that it can be available for success
callback 1.将对象转换为变量,以便它可用于success
回调
2.use the variable along with .closest()
to paste new message in corresponding td
. 2.使用变量和.closest()
将新消息粘贴到相应的td
。
So code need to be:- 所以代码必须是: -
<script>
$('.views').click(function (event) {
var obj = $(this); // captur the object into a variable so that it can be available for success callback
var game_id = obj.val();// use variable to get value
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
type:'POST',
dataType:'JSON',
url:'{{url('home/update-views')}}',
data:'game_id='+game_id,
success:function(data)
{
var data=eval(data);
message=data.message;
obj.closest('tr').find(".view2").text(message); // use the variable along with closest to paste new message
}
});
})
</script>
If you use DataTable
, you can do this as below: 如果您使用DataTable
,则可以执行以下操作:
var oTable = $('#datatables').dataTable();
oTable.fnUpdate([
'<td><span class="view2">{{$datum->views}}</span></td>',
'<td><button class="btn btn-default views" value="{{$datum->game_id}}">click</button></td>'
], 0);
The problem with your code is you are getting "this" property of success function whereas you have to use "this" property of ".views click" function. 您的代码的问题是您获得成功函数的“this”属性,而您必须使用“.views”功能的“this”属性。
<table>
@foreach($data as $datum)
<tr>
<td><span class="view2">{{$datum->views}}</span></td>
<td><button class="btn btn-default views" value="{{$datum->game_id}}">click</button></td>
</tr>
@endforeach
</table>
<script>
$('.views').click(function (event) {
var game_id = this.value;
var that = this;
//alert(game_id);
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
type:'POST',
dataType:'JSON',
url:"{{url('home/update-views')}}",
data:'game_id='+game_id,
success:function(data)
{
var data=eval(data);
$(that).parent().parent()
.find(".view2").text(data.message);
}
});
})
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.