如何在 Datatables jquery 中的 columns.render 函数中传递多个参数?

How to pass multiple parameters in columns.render function in Datatables jquery?

我有下面的数据,我想在 Datatables 的 columns.render 函数中传递两个变量 (id, name)。

我现在只有在渲染函数中传递id。 我也想在渲染函数中传递name

谢谢。

const myData = [
  { id: 2, name: "book" },
  { id: 5, name: "song" },
];

$("#example").DataTable({
  data: myData,
  columns: [
    {
      targets: 1,
      data: "id",
      render: function (data, type, row, meta) {
        return (
          "<button class='btn btn-default' data_id='" +
          data + //id is passed to here
          "'>" +
          "name" + //the name I want to pass to here.
          "</button>"
        );
      },
    },
  ],
});

您可以使用 render 函数的 row 参数来完成。

const myData = [
  { id: 2, name: "book" },
  { id: 5, name: "song" },
];

$("#example").DataTable({
  data: myData,
  columns: [
    {
      targets: 1,
      data: "id",
      render: function (data, type, row, meta) {
        return (
          "<button class='btn btn-default' data_id='" +
          data + //id is passed to here
          "'>" +
          row.name + //get the name using row parameter
          "</button>"
        );
      },
    },
  ],
});

可以在 https://datatables.net/reference/option/columns.render

找到有关渲染函数的更多详细信息