4

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

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

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:

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

So, how do I pass arguments to the sAjaxSource key, namely the $id variable?

oxfist
  • 749
  • 6
  • 22
Rishi Reddy
  • 123
  • 1
  • 2
  • 10

2 Answers2

20

You should use fnServerParams as specified in the 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" } );
        }
    } );
} );
oxfist
  • 749
  • 6
  • 22
John Moses
  • 1,283
  • 1
  • 12
  • 18
  • 1
    i'm getting the error Uncaught TypeError: Cannot read property 'match' of undefined. Why ? – Mvram Jun 26 '17 at 18:42
  • 1
    i am also getting same error " Cannot read property 'match' of undefined." can you provide some hint why this error getting using your solution – coderwill Jul 25 '17 at 07:16
  • you have to enter the data as an arraylike: aoData.push( { "name": "name1", "value": "val1" } ,{ "name": "name2", "value": "val2" },{ "name": "name3", "value": "val3" }); – Rajat Masih Oct 23 '17 at 07:21
3

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.

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

$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

$_GET['customvar'];
Timo002
  • 3,138
  • 4
  • 40
  • 65