jquery - How to pass custom arguments to dataTables footerCallback -
i want add custom argument footercallback
in jquery datatables.
this part of code :
otableclient = $('#tbl_client').datatable({ "footercallback": function(){ datatablefootercallback('client'); } }); function datatablefootercallback(type, row, data, start, end, display) { var api = this.api(), data; if (type == 'client') { data = api.column(6, {page: 'current'}).data(); } else { data = api.column(5, {page: 'current'}).data(); } }
but getting error,
typeerror: this.api not function
var api = this.api(), data;
where wrong?
you trying access datatable api on this
not exists. every datatable method or callback returns either api or api accessible through this
context, have pass if want operate on this.api()
in own function. can calling datatablefootercallback()
apply()
:
footercallback: function() { datatablefootercallback.apply(this, arguments) }
now have passed this
datatablefootercallback
, have access api :
function datatablefootercallback(type, row, data, start, end, display) { //get first row trough api console.log(this.api().row(0).data()) //rest of passed arguments same footercallback() console.log(arguments) }
demo -> http://jsfiddle.net/95h3a8nw/
adding variables arguments
little bit tricky since arguments
not real array, not have prototype methods such push()
. workaround work :
footercallback: function() { var newarguments = array.prototype.slice.call(arguments) newarguments.push('client') datatablefootercallback.apply(this, newarguments) }
in datatablefootercallback()
'client'
added last param in function call. see updated fiddle -> http://jsfiddle.net/95h3a8nw/1/
Comments
Post a Comment