0

I created datatable like below;

var costtable = $('#productcategories').DataTable({
    "responsive": true,
    "processing": true,
    "serverSide": true,
    "deferRender": true,
    "ajax": "{{ route('product-json') }}?{!! \Request::getQueryString() !!}",
.....

I need to add/change some parameters with dynamic actions. Like;

$('body').on('change', '.blabla', function(){
var val = $(this).val();
costtable.addparameter('blabla', val);
});

How can I make it possible? I will use that parameter on backend.

2
  • We're going to need more detail to be able to help you. Exactly what parameter are you trying to change? Check the datatables documentation, it's very thorough: datatables.net/reference/option/ajax.data Commented Feb 5, 2021 at 13:35
  • actually i want to add "user_id" parameters to ajax data. Commented Feb 5, 2021 at 13:41

1 Answer 1

1

Instead of using the string form of the DataTables ajax option like this...

"ajax": "your_url_here"

...you can use the object form of the syntax:

"ajax": {
  "url": "your_url_here",
  "data": extraRequestParams
}

Based on the question, the object you need to construct will be:

extraRequestParams = { user_id: "some_value" }

...where the value is what you populate from your var val = $(this).val();.

This data will be added to the request data which DataTables automatically generates to support server-side processing:

draw=1&...&start=0&length=25&user_id=some_value

In order for your onchange event to trigger the ajax call, you will need to refer to the DataTable - so, something like this:

var extraRequestParams = {};

$('body').on('change', '.blabla', function(){
  var val = $(this).val();
  extraRequestParams = { user_id: val };
  costtable.ajax.reload();
});

This assumes costtable is defined as your DataTable:

var costtable = $('#example').DataTable( { ... } );

See ajax.reload() for reference.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.