从数据表错误中的按钮获取数据

Get data from button inside datatables error

我有如下的数据表脚本。 Datatable 工作正常,但是当我单击 edit 按钮时,出现如下错误:

Uncaught TypeError: datatables.row is not a function.

如何修复代码并获取编辑按钮的 ID?

let data_table = $('#categories_table');

let datatables = data_table.dataTable({
  "processing": true,
  "serverSide": true,
  "order": [
    [5, "asc"]
  ],
  ajax: '/admin/categories/datatables',
  columnDefs: [{
    'targets': 0,
    'searchable': false,
    'orderable': false,
    'render': function(data, type, full, meta) {
      return '<input type="checkbox" class="form-check-input-styled-primary" name="ids[]" value="' +
        $('<div/>').text(data).html() + '">';
    }
  }, {
    targets: -1,
    title: 'Actions',
    orderable: false,
    render: function(data, type, full, meta) {
      return `<a  href="#"  title="Edit User" class="list-icons-item text-primary-600 edit">
          <i class="icon-pencil7"></i>
        </a>
        <a href="#" title="Delete User" class="list-icons-item text-danger-600 delete">
          <i class="icon-trash"> </i>
        </a>`;
    },
  }]
});

datatables.on('click', '.edit', function(event) {
  event.preventDefault();
  var data = datatables.row($(this).closest('tr')).data();
  let id = data[1];
  let $edit_url = "/admin/categories/" + id + "/edit";

  $.post($edit_url, {
    id: id,
    csrf_token: $csrf
  }).done(function(data) {
    update_modal.modal("show");
    update_validator.resetForm();
    $(".modal-title").text("Edit User");
    update_modal.find("form")[0].reset();
    $("#id").val(data.id);
    $("#update_csrf").val($csrf);
    $("#update_name").val(data.name);
    $("#update_email").val(data.email);
    $("#update_role").val(data.role);
    $("#update_status").val(data.is_active);
  });
});

您可以这样修改您的编辑:

 {
                    targets: -1,
                    title: 'Actions',
                    orderable: false,
                    render: function( data, type, row, meta ) {
                        return '\
                        <button class="btn btn-outline bg-primary border-primary text-primary-800 btn-icon btn-sm edit" data-category='+ row[1] + '>\
                          <i class="icon-pencil7"></i>\
                        </button>\
                        <button title="Delete User" class="btn btn-outline bg-danger border-danger text-danger-800 btn-icon btn-sm e delete">\
                          <i class="icon-trash"> </i>\
                        </button>';
                    },
                }

然后在您的点击编辑按钮中,您甚至可以获得这样的类别 ID:

    let id = $(this).data("category");