![](/img/trans.png)
[英]Jquery and Bootstrap 4 dataTable delete row on page 2 not working
[英]Laravel and jquery datatable delete row
我正在使用laravel 5.2,Jquery 1.10和MYSQL 5.7。
我在laravel中将RESTful服务用于消息模型。
我试图在Jquery数据表中呈现消息列表,并在每行上提供一个删除按钮,当按下该按钮时应触发MessageController.php上的destroy方法,删除该行然后重新加载消息索引页以显示更新的数据表。
最初已正确生成数据表,但删除按钮不会触发控制器上的删除方法。 我假设用于生成删除按钮的ajax代码一定是错误的。
在此先感谢您的帮助。
我的数据表ajax脚本是-
$(document).ready( function () {
$('#message_table').DataTable({
"searchable": false,
"ajax": {
"url": "/api/message",
"type": "POST",
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
},
"columns": [
{ "data": "id"},
{ "data": "subject",
"render": function(data,type,row,meta) {
return '<a href="/message/'+row.id+'">'+data+'</a>';
}
},
{ "data": "created_at",
"render": function ( data, type, full, meta ) {
// instantiate a moment object and hand it the string date
var d = moment(data);
var month = d.month() +1 < 10 ? "0" + (d.month() +1) : d.month() +1;
var day = d.date() < 10 ? "0" + (d.date()): d.date();
return month + "/" + day + "/" + d.year();
}
},
{"defaultContent": "null", "render": function(data,type,row,meta) {
return '<button action="' + $(location).attr('protocol') + $(location).attr('host') + '/message/'+row.id+ '"' + 'name="_method"' + 'method="post"' +'type="submit"' + 'value="Delete"'+'>'+ 'Delete</button>';
}
}
]
});
} );
从上一个函数为删除按钮生成的HTML是-
<button action="http:localhost:8000/message/6" name="_method" method="post" type="submit" value="Delete">Delete</button>
消息控制器删除方法-
public function destroy($id)
{
Message::destroy($id);
return Redirect::route('message.index');
}
如果您使用的是laravel的restful控制器,则destroy方法是通过delete
http方法。 因此,最好的选择是创建一个小的表单,例如:
<form action="http:localhost:8000/message/6" method="post">
<input type="hidden" name="_method" value="delete">
<button type="submit">Delete</button>
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.