将参数传递给 jQuery 数据表中的 sAjaxSource

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17160638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:32:51  来源:igfitidea点击:

Passing arguments to sAjaxSource in jQuery datatables

ajaxcodeigniterjquerydatatables

提问by Rishi Reddy

I am working with CodeIgniter and I have the following problem.

我正在使用 CodeIgniter,但遇到以下问题。

I have a controller function in details.phpwhich takes an argument:

我有一个控制器函数,details.php它接受一个参数:

function details($id) {
    $this->datatables
         ->select('product_id, original_amount, quantity')
         ->from('orders');

    echo $this->datatables->generate();
}  

Now I need to call this from views, i.e. I want DataTables to display it like so:

现在我需要从视图中调用它,即我希望 DataTables 像这样显示它:

<script>
$(document).ready(function() 
  $('#example').dataTable({
    ...
    'sAjaxSource' : 'admin/data/details',
    ...
  });
</script>

So, how do I pass arguments to the sAjaxSourcekey, namely the $idvariable?

那么,我如何将参数传递给sAjaxSource键,即$id变量?

回答by John Moses

You should use fnServerParamsas specified in the docs.

您应该fnServerParams按照文档中的说明使用

$(document).ready(function() {
    $('#example').dataTable( {
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": "scripts/server_processing.php",
        "fnServerParams": function ( aoData ) {
            aoData.push( { "name": "more_data", "value": "my_value" } );
        }
    } );
} );

回答by Timo002

I was struggling with this issue myself. The answer of John Moses didn't work for me, also the link he provides links to the new documentation which, I think, does not count for older versions of datatables.

我自己也在努力解决这个问题。约翰摩西的回答对我不起作用,他提供的链接也指向新文档的链接,我认为这不计入旧版本的数据表。

Although, I found out that the current server side processing is working like jQuery, where older versions with "sAjaxSource" don't work that way.

虽然,我发现当前的服务器端处理像 jQuery 一样工作,其中带有“sAjaxSource”的旧版本不能那样工作。

For me, I just simpely added my parameters in the url of the sAjaxSource. So in your example code:

对我来说,我只是在sAjaxSource. 所以在你的示例代码中:

$customPHPvar = "mycustomvar";

<script>
$(document).ready(function() {
    $('#example').dataTable( {
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": "scripts/server_processing.php?customvar=<?php echo $customPHPvar; ?>",
    } );
} );
</script>

in server_processing.php

在 server_processing.php 中

$_GET['customvar'];