【问题标题】:How to pass multiple parameters in columns.render function in Datatables jquery?如何在Datatables jquery的columns.render函数中传递多个参数?
【发布时间】:2021-05-04 06:50:34
【问题描述】:

我有下面的数据,我想在 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>"
        );
      },
    },
  ],
});

【问题讨论】:

    标签: javascript jquery datatables


    【解决方案1】:

    您可以使用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找到

    【讨论】:

    • 谢谢。实际上,我不需要使用参数data,我只能使用row 来访问我的数据对象。
    • 是的,你也可以用row.id代替data
    猜你喜欢
    • 1970-01-01
    • 2019-06-04
    • 2014-12-19
    • 2021-07-27
    • 2015-09-15
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多