如何使用jQuery DataTables在ajax调用中传递额外的参数?

时间:2015-07-12 12:09:32

标签: php jquery ajax datatables

如何使用jQuery DataTables在ajax调用中将额外参数传递给php?

这是我的代码

 $(document).ready(function() {
            var dataTable =  $('#student-grid').DataTable( {
                responsive: {
                    details: {
                        renderer: function ( api, rowIdx ) {
                            var data = api.cells( rowIdx, ':hidden' ).eq(0).map( function ( cell ) {
                                var header = $( api.column( cell.column ).header() );
                                return  '<p style="color:#00A">'+header.text()+' : '+api.cell( cell ).data()+'</p>';
                            } ).toArray().join('');

                            return data ?    $('<table/>').append( data ) :    false;
                        }
                    }
                },
                processing: true,
                serverSide: true,
                ajax: "borrowedBookNew.php" // json datasource
            } );

        } );

我想将一个新参数传递给我的php文件并获得一个新结果。

1 个答案:

答案 0 :(得分:5)

您可以通过将ajax参数设置为对象来传递其他数据:

$('#student-grid').dataTable({
    // ...
    ajax: {
        url: 'borrowedBookNew.php',
        data: {
            customField: 'customValue'
        }
    }
});

您还可以将data一个接收当前数据的函数作为可以操作的对象传递。这对于添加页面加载时无法使用的动态数据特别有用。

来源:http://datatables.net/examples/server_side/custom_vars.html