【问题标题】:How to set the href in a DataTables button?如何在 DataTables 按钮中设置 href?
【发布时间】:2017-01-11 13:48:37
【问题描述】:

我使用 DataTables 来格式化我的表格。我也使用他们的按钮插件。我正在尝试创建一个自定义按钮以重定向到另一个页面,我将在其中创建一个 Excel 文件以供下载。我只是不确定如何设置href。我试过这个:

$.fn.dataTable.ext.buttons.export =
{
    className: 'buttons-alert',
    text: "Export All Test III",
    action: function (e, dt, node, config)
    {
        var SearchData = dt.rows({ filter: 'applied' }).data();
        var OrderData = dt.order();
        alert("Test Data for Searching: " + SearchData);
        alert("Test Data for Ordering: " + OrderData);
    },
    href: './AjaxHandler.php'
};

href 被忽略且未设置。我需要设置href

我该怎么做?

我可以在 Firefox 的开发工具中看到它具有该属性,但它被设置为 # 如下:

编辑

我已经尝试在初始化后设置href,如下所示:

$('.dt-button.buttons-alert').attr('href', './AjaxHandler.php');


document.querySelector('.buttons-alert').setAttribute('href', './AjaxHandler.php');

不过,这些都不起作用,href 仍然只显示 #。

【问题讨论】:

    标签: javascript jquery datatables-1.10


    【解决方案1】:

    我已经让它工作了,有点。我仍然无法在按钮中设置href。我能做的是:

    $.fn.dataTable.ext.buttons.export =
    {
        className: 'buttons-alert',
        id: 'ExportButton',
        text: "Export All Test III",
        action: function (e, dt, node, config)
        {
            //This will send the page to the location specified
            window.location.href = './AjaxHandler.php';
        }
    };
    

    这完成了同样的事情,即使它以不同的方式完成。

    【讨论】:

    • 这对我来说是一个救命的技巧。谢谢哥们。如果您使用这种方法遇到任何错误,也请告诉我。
    • 我刚刚更新了我在我的网站上工作的内容。到目前为止,它没有任何问题。
    【解决方案2】:

    这是我为解决此问题所做的。这会将我的“添加记录”按钮放入 DataTable DOM

    $('#myTable').DataTable({
        ...,
        buttons: [
            {
                text: '<i class="fa fa-plus"></i>',
                className: 'btn btn-default btnAddJob',
                titleAttr: 'Add a new record',
                init: function (dt, node, config) {
                    $(node).attr('href', 'put/your/href/here')
                }
            }
        ]
    })
    

    【讨论】:

      【解决方案3】:

      dataTables 的选项中没有“href”。 按钮只有以下可以使用的选项: https://datatables.net/reference/option/#buttons

      【讨论】:

      • 是的,我看过那个页面。当然,由于页面上有href,所以有一种方法可以访问它并将其设置为我想要的?
      • 在初始化按钮后,您始终可以使用 jquery 动态添加 href 链接.. $(".dt-button.buttons-alert").attr("href", "./AjaxHandler.php" )
      • 我试过这个$('dt-button.buttons-alert').attr('href', './AjaxHandler.php'); 并没有任何区别,href 仍然是#
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-28
      相关资源
      最近更新 更多