【问题标题】:Passing arguments to sAjaxSource in jQuery datatables在 jQuery 数据表中将参数传递给 sAjaxSource
【发布时间】:2013-06-14 04:21:02
【问题描述】:

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

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

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

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

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

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

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

【问题讨论】:

  • 为什么要在 details 方法中传递 id
  • 使用 post 或 get 方法而不是作为参数传递
  • 先生,我想在细节方法中做 where('product_id',$id) 所以我想传递值

标签: ajax codeigniter jquery datatables


【解决方案1】:

您应该使用fnServerParams 中指定的docs

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

【讨论】:

  • 我收到错误 Uncaught TypeError: Cannot read property 'match' of undefined。为什么?
  • 我也收到同样的错误“无法读取未定义的属性‘匹配’。”您能否提供一些提示,为什么使用您的解决方案会出现此错误
  • 你必须将数据作为数组输入:aoData.push( { "name": "name1", "value": "val1" } ,{ "name": "name2", "值": "val2" },{ "name": "name3", "value": "val3" });
【解决方案2】:

我自己也在努力解决这个问题。 John Moses 的回答对我不起作用,他还提供了指向新文档的链接,我认为这不适用于旧版本的数据表。

虽然,我发现当前服务器端处理的工作方式与 jQuery 类似,而带有“sAjaxSource”的旧版本不能那样工作。

对我来说,我只是在sAjaxSource 的网址中添加了我的参数。所以在你的示例代码中:

$customPHPvar = "mycustomvar";

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

在 server_processing.php 中

$_GET['customvar'];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-16
    • 1970-01-01
    • 2013-01-27
    • 2010-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    相关资源
    最近更新 更多