响应式jQuery数据表无法获取行的详细信息

时间:2019-02-01 10:16:04

标签: javascript jquery html datatables

我正在将插件jQuery DataTables与响应式插件一起使用,通过这种方式,我可以根据浏览器的大小动态显示和隐藏列。

我有一个名为Actions的列,用户可以使用它来编辑记录,当按下该列中包含的铅笔时,我将搜索所单击元素的id

仅当表未处于responsive模式时,该机制才起作用,实际上,当表处于响应状态并且我展开其他列时:

enter image description here

如果我点击铅笔,我会得到:

  

无法读取未定义的属性“ id”

因为该插件无法找到被单击元素的行。

我创建了一个代码段和一个JSFIDDLE

$(document).ready(function() {
  var table = $('#example').DataTable({
    "columns": [{
        data: 'name'
      },
      {
        data: 'position'
      },
      {
        data: 'office'
      },
      {
        data: 'age'
      },
      {
        data: 'salary'
      },
      {
        data: null,
        render: function(data, type, row) {
          return '<a href="javascript:void(0)" data="' + data.id + '" class="btn btn-link btn-warning btn-icon btn-sm edit"><i class="fas fa-pencil-alt"></i></a>';
        }
      }
    ],
    responsive: true
  });

  var users = [{
      id: 1,
      name: "Penny",
      position: "waitress",
      office: "none",
      age: "25",
      salary: "1550$"
    },
    {
      id: 2,
      name: "Sheldon",
      position: "physical",
      office: "university",
      age: "39",
      salary: "8435$"
    },
    {
      id: 3,
      name: "Leonard",
      position: "physical",
      office: "university",
      age: "35",
      salary: "7842$"
    },
  ];

  $('#example').DataTable().clear().draw();
  $('#example').DataTable().rows.add(users);
  $('#example').DataTable().draw();

  $('#example').on('click', '.edit', function(element) {

    var tr = $(element.target).closest('tr');
    var data = table.row(tr).data();
    console.log(data.id);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/v/dt/dt-1.10.18/r-2.2.2/datatables.min.js"></script>
<link href="https://cdn.datatables.net/v/dt/dt-1.10.18/r-2.2.2/datatables.min.css" rel="stylesheet"/>
<link href="https://use.fontawesome.com/releases/v5.7.0/css/all.css" rel="stylesheet"/>

<table id="example" class="display" cellspacing="0" width="100%">
  <thead>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Age</th>
      <th>Salary</th>
      <th class="sorting_desc_disabled sorting_asc_disabled">Action</th>
    </tr>
  </thead>

  <tfoot>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Age</th>
      <th>Salary</th>
      <th class="sorting_desc_disabled sorting_asc_disabled">Action</th>
    </tr>
  </tfoot>

  <tbody>
  </tbody>
</table>

我做错了什么吗?还是那一个错误? 有办法解决吗?

谢谢。

3 个答案:

答案 0 :(得分:1)

您也可以尝试以下操作:

  $(document).ready(function() {
      var table = $('#example').DataTable({
        "columns": [{
            data: 'name'
          },
          {
            data: 'position'
          },
          {
            data: 'office'
          },
          {
            data: 'age'
          },
          {
            data: 'salary'
          },
          {
            data: null,
            render: function(data, type, row) {
              return '<a href="javascript:void(0)" data="' + data.id + '" class="btn btn-link btn-warning btn-icon btn-sm edit"><i class="fas fa-pencil-alt"></i></a>';
            }
          }
        ],
        responsive: true
      });

      var users = [{
          id: 1,
          name: "Penny",
          position: "waitress",
          office: "none",
          age: "25",
          salary: "1550$"
        },
        {
          id: 2,
          name: "Sheldon",
          position: "physical",
          office: "university",
          age: "39",
          salary: "8435$"
        },
        {
          id: 3,
          name: "Leonard",
          position: "physical",
          office: "university",
          age: "35",
          salary: "7842$"
        },
      ];

      $('#example').DataTable().clear().draw();
      $('#example').DataTable().rows.add(users);
      $('#example').DataTable().draw();

      $('#example').on('click', '.edit', function(element) {
    debugger
        var tr = $(element.target).closest('tr');
        **if(tr.hasClass('child'))
        {
            tr=$(tr).prev();
        }**
        var data = table.row(tr).data();
        console.log(data.id);
      });
    });

答案 1 :(得分:1)

根据评论讨论,我将其发布

$(document).ready(function() {
  var table = $('#example').DataTable({
  "columns": [{
    data: 'name'
  },
  {
    data: 'position'
  },
  {
    data: 'office'
  },
  {
    data: 'age'
  },
  {
    data: 'salary'
  },
  {
    data: null,
    render: function(data, type, row) {
      return '<a href="javascript:void(0)" class="btn btn-link btn-warning btn-icon btn-sm "><i class="fas fa-pencil-alt edit" data-id="' + data.id + '" ></i></a>'; // same class in i element removed it from a element
    }
  }
],
responsive: true
});

var users = [{
  id: 1,
  name: "Penny",
  position: "waitress",
  office: "none",
  age: "25",
  salary: "1550$"
},
{
  id: 2,
  name: "Sheldon",
  position: "physical",
  office: "university",
  age: "39",
  salary: "8435$"
},
{
  id: 3,
  name: "Leonard",
  position: "physical",
  office: "university",
  age: "35",
  salary: "7842$"
},
];

$('#example').DataTable().clear().draw();
$('#example').DataTable().rows.add(users);
$('#example').DataTable().draw();

$(document).on('click', '.edit', function(element) {

console.log($(this).data('id')); //direct get the value
});
 });

答案 2 :(得分:0)

使用data-id

$(document).ready(function() {
  var table = $('#example').DataTable({
    "columns": [{
        data: 'name'
      },
      {
        data: 'position'
      },
      {
        data: 'office'
      },
      {
        data: 'age'
      },
      {
        data: 'salary'
      },
      {
        data: null,
        render: function(data, type, row) {
          return '<a href="javascript:void(0)" data-id="' + data.id + '" class="btn btn-link btn-warning btn-icon btn-sm edit"><i class="fas fa-pencil-alt"></i></a>';
        }
      }
    ],
    responsive: true
  });

  var users = [{
      id: 1,
      name: "Penny",
      position: "waitress",
      office: "none",
      age: "25",
      salary: "1550$"
    },
    {
      id: 2,
      name: "Sheldon",
      position: "physical",
      office: "university",
      age: "39",
      salary: "8435$"
    },
    {
      id: 3,
      name: "Leonard",
      position: "physical",
      office: "university",
      age: "35",
      salary: "7842$"
    },
  ];

  $('#example').DataTable().clear().draw();
  $('#example').DataTable().rows.add(users);
  $('#example').DataTable().draw();

  $('#example').on('click', '.edit', function(element) {

    /*var tr = $(element.target).closest('tr');
    var data = table.row(tr).data();
    console.log(data.id);*/
    var id = $(this).data('id')
    console.log(id);

  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/v/dt/dt-1.10.18/r-2.2.2/datatables.min.js"></script>
<link href="https://cdn.datatables.net/v/dt/dt-1.10.18/r-2.2.2/datatables.min.css" rel="stylesheet"/>
<link href="https://use.fontawesome.com/releases/v5.7.0/css/all.css" rel="stylesheet"/>

<table id="example" class="display" cellspacing="0" width="100%">
  <thead>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Age</th>
      <th>Salary</th>
      <th class="sorting_desc_disabled sorting_asc_disabled">Action</th>
    </tr>
  </thead>

  <tfoot>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Age</th>
      <th>Salary</th>
      <th class="sorting_desc_disabled sorting_asc_disabled">Action</th>
    </tr>
  </tfoot>

  <tbody>
  </tbody>
</table>