如何将附加参数传递给 jQuery DataTable ajax 调用? [英] How do I pass additional parameters to the jQuery DataTable ajax call?

查看:26
本文介绍了如何将附加参数传递给 jQuery DataTable ajax 调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

加载 jQuery 数据表时,我有如下所示的代码.如何将附加参数传递给 AJAX 调用?以下问题和答案中建议的 fnServerParams 回调不起作用.也就是说,天真地使用 aodata.push() 会导致推送未定义"(因为实际上,aodata 不是数组).那么,这样做的正确方法是什么?

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?

相关问题:

代码:

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%"

                }
            ],
        });

推荐答案

我忽略了 RTFM.fnServerParams 回调现在是 1.9 及更早版本的legacy.在最新版本的 DataTables 中,您可以利用 ajax 数据参数 如 DataTables 文档中所述.在下面的示例中,将 mykey 附加到 d 对象即可:

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:

$(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
            }
        }
    } );
} );

这篇关于如何将附加参数传递给 jQuery DataTable ajax 调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆