如何将其他参数传递给jQuery DataTable ajax调用?

时间:2022-02-15 09:19:45

When loading a jQuery DataTable, I have the code shown below. How do I pass additional parameters to the AJAX call? The fnServerParams callback suggested in the questions and answers below does not work. That is, naively using aodata.push() results in "push is undefined" (because, indeed, aodata is not an array). So what's the correct way to do this?

加载jQuery DataTable时,我有如下所示的代码。如何将其他参数传递给AJAX调用?下面的问题和答案中建议的fnServerParams回调不起作用。也就是说,天真地使用aodata.push()会导致“push is undefined”(因为,实际上,aodata不是数组)。那么这样做的正确方法是什么?

Related questions:

相关问题:

Code:

码:

self.dataTable = self.dataTableContainer.DataTable({
            "autoWidth": false,
            "bSort": false,
            "displayStart": 0,
            "paging": false,
            "lengthChange": false,
            "processing": true,
            "serverSide": true,
            "dom": "<'dataTables_header dashboard_alert_history__alertHeader'i>",
            "ajax": {
                url: getDataUri,
                error: onError,
                cache: false,
                "fnDrawCallback": onTableDrawn,
            },
            "fnDrawCallback": onTableDrawn,
            "language": {
                "info": resources.alarmHistory,
                "infoEmpty": resources.alarmHistory,
                "infoFiltered": ''
            },
            "columns": [
                {
                    "data": "timestamp",
                    "mRender": function (data) {
                        return IoTApp.Helpers.Dates.localizeDate(data, 'L LTS');
                    },
                    "name": "timestamp"
                },
                {
                    "data": "deviceId",
                    "mRender": function (data) {
                        return htmlEncode(data);
                    },
                    "name": "deviceId"
                },
                {
                    "data": "ruleOutput",
                    "mRender": function (data) {
                        return htmlEncode(data);
                    },
                    "name": "ruleOutput"
                },
                {
                    "data": "value",
                    "mRender": function (data) {
                        return htmlEncode(IoTApp.Helpers.Numbers.localizeFromInvariant(data));
                    },
                    "name": "value"
                },
            ],
            "columnDefs": [
                {
                    "targets": [0, 1, 2, 3],
                    "className": 'table_alertHistory_issueType',
                    "width": "20%"

                }
            ],
        });

1 个解决方案

#1


17  

I neglected to RTFM. The fnServerParams callback is now legacy for versions 1.9 and earlier. In the latest version of DataTables, you leverage the ajax data parameter as described in the DataTables documentation. In the example below, appending mykey to the d object will do the trick:

我忽略了RTFM。对于1.9及更早版本,fnServerParams回调现在是遗留的。在最新版本的DataTables中,您可以使用DataTables文档中描述的ajax数据参数。在下面的示例中,将mykey附加到d对象将起到作用:

$(document).ready(function() {
    $('#example').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": {
            "url": "scripts/server_processing.php",
            "data": function ( d ) {
                d.myKey = "myValue";
                // d.custom = $('#myInput').val();
                // etc
            }
        }
    } );
} );

#1


17  

I neglected to RTFM. The fnServerParams callback is now legacy for versions 1.9 and earlier. In the latest version of DataTables, you leverage the ajax data parameter as described in the DataTables documentation. In the example below, appending mykey to the d object will do the trick:

我忽略了RTFM。对于1.9及更早版本,fnServerParams回调现在是遗留的。在最新版本的DataTables中,您可以使用DataTables文档中描述的ajax数据参数。在下面的示例中,将mykey附加到d对象将起到作用:

$(document).ready(function() {
    $('#example').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": {
            "url": "scripts/server_processing.php",
            "data": function ( d ) {
                d.myKey = "myValue";
                // d.custom = $('#myInput').val();
                // etc
            }
        }
    } );
} );