Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

jquery - Datatables - Send Parameter dynamically

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.

question from:https://stackoverflow.com/questions/66064209/datatables-send-parameter-dynamically

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...