Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

jquery - How to set dynamically the Ajax URL of a dataTable?

I'm using jQuery DataTables, my JavaScript code is shown below:

$(document).ready(function() {
   var tbl = $('#table_tabl').DataTable({
      responsive: true,
      "oLanguage": {
         "sUrl": "<?php  echo RP_LANG ?>fr_FR.txt",
      },
      "processing": true,
      "serverSide": true,
      ajax: "<?php  echo RP_SSP ?>server_processing_reservTables.php", // I want to add a parmeter to it dynamically when a select element is selected 
      "aoColumnDefs": [{
         "aTargets": [3],
         "mData": 3,
         "mRender": function(data, type, full) {
            return '<div style="text-align:center;"><a href="RestaurantReservation/reserverTable/' + data + '" title="R&eacute;server"><span class="mif-lock icon"></span></a></div>';
         }
      }],
      "aLengthMenu": [
         [10, 25, 50, 100, -1],
         [10, 25, 50, 100, "Tout"]
      ]
   });
});

I want to filter this dataTable according to the selected value of a select element :

$("#select_id").on("change", function(){
    // set the ajax option value of the dataTable here according to the select's value
});

How to set the ajax option's value of the dataTable in the on_change event of the select element based on the select's selected item ?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

SOLUTION 1

Use ajax.url() API method to set the URL that DataTables uses to Ajax fetch data.

$("#select_id").on("change", function(){
    // set the ajax option value of the dataTable here according to the select's value
    $('#table_tabl').DataTable()
       .ajax.url(
          "<?php  echo RP_SSP ?>server_processing_reservTables.php?param=" 
          + encodeURIComponent(this.value)
       )
       .load();
});

SOLUTION 2

Use ajax.data option to add or modify data submitted to the server upon an Ajax request.

var tbl = $('#table_tabl').DataTable({
   // ... skipped other parameters ...
   ajax: {
      url: "<?php  echo RP_SSP ?>server_processing_reservTables.php",
      data: function(d){
         d.param = $('#select_id').val();
      }
   }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...