Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing arguments to sAjaxSource in jQuery datatables

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?

like image 417
Rishi Reddy Avatar asked Nov 28 '22 15:11

Rishi Reddy


1 Answers

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" } );
        }
    } );
} );
like image 144
John Moses Avatar answered Dec 04 '22 06:12

John Moses