datatables – 如何在datatable中的每一行添加按钮

前端之家收集整理的这篇文章主要介绍了datatables – 如何在datatable中的每一行添加按钮前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是DataTables的新手。我想在每一行添加按钮进行编辑和删除(如下图)

我试过代码

Test.cfm

  1. <table id="myDataTable" class="table table-striped table-bordered">
  2. <thead>
  3. <tr>
  4. <th>UserID</th>
  5. <th>Name</th>
  6. <th>UserName</th>
  7. <th>Passowrd</th>
  8. <th>Email</th>
  9. <th>IsActive</th>
  10. <th>Command</th>
  11. </tr>
  12. </thead>
  13. <tbody>
  14. </tbody>
  1. <script>
  2.  
  3. $(document).ready(function () {
  4. var oTable = $('#myDataTable').dataTable({
  5. // "bServerSide": true,"sAjaxSource": "fetchUserData.cfm","bProcessing": true,"sPaginationType": "full_numbers","aoColumns": [
  6. { "mData": null },{ "mData": "Name","sTitle": "Name","sWidth": "20%"},{ "mData": "UserName","sTitle": "UserName","sWidth": "20%" },{ "mData": "Passowrd","sTitle": "Passowrd","sWidth": "20%" },{ "mData": "Email","sTitle": "Email",{ "mData": "IsActive","sTitle": "IsActive",{
  7. "mData": null,"bSortable": false,"mRender": function (o) { return '<a href=#/' + o.userid + '>' + 'Edit' + '</a>'; }
  8. }
  9. ]
  10. });

});

  1. </script>

fetchUserData.cfm

  1. {
  2. "aaData": [
  3. [
  4. "1","sameek","sam","sameek@test.com","1",""
  5. ],[
  6. "2","arun singh","arun","arunsingh@test.com","0",[
  7. "9","s s","sam3","ss@s.com",[
  8. "10","sameek mishra","sam56","same@s.com",[
  9. "11","narendra kumar","narendra","nav","sa@sa.com",[
  10. "12","test test","test","test2@test.com",""
  11. ]
  12. ]
  13. }

请帮帮我。

谢谢

解决方法

基本上你的代码是可以的,这是正确的方法。无论如何,有一些误会:

> fetchUserData.cfm不包含键/值对。所以在mData中解密密钥是没有意义的。只需使用mData [index]
> dataTables需要您的服务器端的一些更多信息。至少你应该告诉数据库,总共有几个项目在你的服务器端,有多少个被过滤。
我刚刚将这个信息硬编码到你的数据。您应该从服务器端脚本中的计数中获取正确的值。

  1. {
  2. "iTotalRecords":"6","iTotalDisplayRecords":"6","aaData": [
  3. [
  4. "1",""
  5. ],...

>如果您在html部分中已经设置了列名,则不需要添加sTitle。
> mRender函数有三个参数:

> data =这个单元格的数据,如mData中定义的
> type =数据类型(可以大部分忽略)
> full =此行的完整数据数组。

所以你的mRender函数应该是这样的:

  1. "mRender": function(data,type,full) {
  2. return '<a class="btn btn-info btn-sm" href=#/' + full[0] + '>' + 'Edit' + '</a>';
  3. }

找到一个工作的Plunker here

猜你在找的jQuery相关文章