0

I use the JQuery DataTables plugin and I initialize it with ajax parameter.

I'll need to calcolate new parameter when I reload data but not run if I pass parameter in the function. I call the ajax.reload from a "Reload" button. Why I can not use function with parameter?

// Run correctly
function loadData(){alert("called");}
tab=$("#tabID").DataTable({ajax:{url:"a.php",data:loadData}}); //  The "loadData" is called
...
$("#tabID").DataTable().ajax.reload(); //  The "loadData" is called again

// Problem
function loadData(A){alert("called with "+A);}
tab=$("#tabID").DataTable({ajax:{url:"a.php",data:loadData("text")}}); //  The "loadData" is called
...
$("#tabID").DataTable().ajax.reload(); // The "loadData" is NOT called... why?

Thank's

2 Answers 2

1

Use the following code:

$("#tabID").DataTable({
   ajax: {
      url: "a.php",
      data: function(data, settings){
         return loadData("text");
      }
   }
}); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I have modify the script with "return" before loadData("text");
1
// Problem
function loadData(A){alert("called with "+A);}
tab=$("#tabID").DataTable({ajax:{url:"a.php",data:loadData("text")}}); //  The "loadData" is called
...
$("#tabID").DataTable().ajax.reload(); // The "loadData" is NOT called... why?

It is just because in second case you pass to data: property result of loadData() not the function, you can bind your parameter like

tab=$("#tabID").DataTable({ajax:{url:"a.php",data:loadData.bind(null, "text")}});

read more about bind

if your browser not implemented bind, you can use handmade bind, or shim,

this code explains what bind make for you in this case

function bindArgs() { // this shim will not use 1st parameter, so should called as (bindArgs(callback, arguments);
    var args = Array.prototype.slice.call(arguments);
    var callback = args.shift();
    return function() { //return new function
           callback.apply(null, args); // run real callback
    }
}
function run() {console.log(arguments)}
setTimeout(bindArgs(run, 1,2,3), 1000)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.