如何将数据发布到<a> element with jquery

时间:2016-12-22 15:01:20

标签: php jquery datatables-1.10

I use DataTables to format my tables on a reporting website that I run. I am using the initialization to add a hyperlink to the first column of my tables. Is there a way to make it act like a button instead of a hyperlink?

I need this because I have the hyperlink sending the whole row as part of the link (so through GET), but it's failing because of the limit on characters in GET. I need to use a POST so that I can send this larger amount of data.

The definition of the hyperlink is:

data = '<a href="FormToEdit.php?everything=\'' + encodeURIComponent(row) + '\'">' + data + '</a>';

Here's the full datatables initialization to show the hyperlink in context:

$.fn.dataTable.ext.buttons.export =
{
    className: 'buttons-alert',
    "text": "Export All Test",
    action: function (e, dt, node, config)
    {
        alert('Export All Test');
    }
};

$(document).ready(function ()
{
    // Setup - add a text input to each footer cell
    $('#DataTableEdit tfoot th').each(function ()
    {
        var title = $(this).text();
        $(this).html('<input type="text" placeholder="Search ' + title + '" />');
    });

    var table = $('#DataTableEdit').DataTable({
        "lengthMenu": [[25, 50, 75, 100, 150, -1], [25, 50, 75, 100, 150, 'All']],
        "dom": '<"top"Bifpl<"clear">>rt<"bottom"ip<"clear">>',
        "buttons": [{
            extend: 'collection',
            text: 'Selection',
            buttons: ['selectAll', 'selectNone']
        }, {
            extend: 'collection',
            text: 'Export',
            buttons: ['export', 'excel', 'csv', 'pdf', { extend: 'excel',
                text: 'Export Current Page',
                exportOptions: {
                    modifier: {
                        page: 'current'
                    }
                },
                customize: function (xlsx)
                {
                    var sheet = xlsx.xl.worksheets['sheet1.xml'];
                    $('row:first c', sheet).attr('s', '7');
                }
            },

            {
                text: 'Export All to Excel',
                action: function (e, dt, button, config)
                {
                    dt.one('preXhr', function (e, s, data)
                    {
                        data.length = -1;
                    }).one('draw', function (e, settings, json, xhr)
                    {
                        var excelButtonConfig = $.fn.DataTable.ext.buttons.excelHtml5;
                        var addOptions = { exportOptions: { 'columns': ':all'} };

                        $.extend(true, excelButtonConfig, addOptions);
                        excelButtonConfig.action(e, dt, button, excelButtonConfig);
                    }).draw();
                }
            }]
        }
        ],
        "fixedHeader": {
            header: true,
            footer: true
        },
        "select": true,
        "processing": true,
        "serverSide": true,
        "ajax": {
            "url": "./ServerSide.php",
            "type": "POST"
        },
//This is the part that adds the hyperlink
        columnDefs: [
        {
            targets: 0,
            render: function (data, type, row, meta)
            {
                if (type === 'display')
                {
                    data = '<a href="FormToEdit.php?everything=\'' + encodeURIComponent(row) + '\'">' + data + '</a>';
                }
                return data;
            }
        }],
//End of hyperlink creation
        initComplete: function ()
        {
            var api = this.api();

            // Apply the search
            api.columns().every(function ()
            {
                var that = this;

                $('input', this.footer()).on('keyup change', function ()
                {
                    if (that.search() !== this.value)
                    {
                        that
                          .search(this.value)
                          .draw();
                    }
                });
            });
        }
    });
});

If I don't have enough information let me know and I'll add more.

1 个答案:

答案 0 :(得分:1)

您可以尝试将其封装在<form>标记内,以便将其作为帖子提交

if (type == 'display'){

    data = '<form action="FormToEdit.php" method="post"><button type="submit" 
           class="btn-as-link" value = \'' +
           encodeURIComponent(row) + '\'>' + data + '</button></form>' ;  
}

你可以把它看作链接:

.btn-as-link{
  border: none;
  outline: none;
  background: none;
  cursor: pointer;
  padding: 0;
  text-decoration: underline;
}