Why does footerCallback function only allow this.api()?
Why does footerCallback function only allow this.api()?
 hzhong            
            
                Posts: 26Questions: 9Answers: 0
hzhong            
            
                Posts: 26Questions: 9Answers: 0            
            
            
                            
                                  in DataTables             
        Hello,
Here is a short example using the API to sum a specific column and output in the footer. I have couple questions. Would someone help me understand?
var table =  $('#example').DataTable( {
  "footerCallback": function( tfoot, data, start, end, display ) {
    var api = this.api();
    $( api.column( 5 ).footer() ).html(
        api.column( 5 ).data().reduce( function ( a, b ) {
            return a + b;
        }, 0 )
    );
  }
} );
- Why does footerCallback function only allow this.api() as the api instance? I tried using tablevariable but failed, which is also an api instance right?
- I assume this.api() is an implementation of $( selector ).dataTable().api(). But why does thiskeyword refer to the dataTable object, not just the object passed in the constructor?
Thank you.
This question has accepted answers - jump to:
This discussion has been closed.
            
Answers
That's really just standard JavaScript questions.
tableis assigned once the initialisation has completed, so at the point it's called infooterCallbackit would still be initialised.thisis the context within the callback function - sothis.api()is there to assist with the issue in your first question,Colin
Thanks Colin. I understand the first point now. I'm still trying to understand the second one.
I thought
thisin the callback referred to the below object which was passed to the table initialization. I still don't get it why it's resolved to same as$(#example).dataTable().Thank you for your time. You can also point me to some useful resource I can refer.
In
footerCallbackthe scope ofthisis thetableelement. We add anapi()method to that to allow access to the DataTables API in the callback.Ideally the scope would just be the DataTables API, but it isn't for legacy reasons. The API (as it currently is) was added in 1.10, while
footerCallbackwas added long before then, so I followed standard jQuery practice and made the scope of the function the element.It might be changes at some point, but I try very hard to not break backwards compatibility, so more likely this will stay as is, and a new callback or event will be used with the API scope.
Allan
Hello Allan,
Thank you for the answer. Maybe I missed something in standard JavaScript as I am still confused why
thisis resolved to the jQuery object, not the options object passed in the initialization as I thought.If I have a simplified object
aas belowThen I call the method
a.footerCallback(). The log result is the objecta.But why is it after I passed this
aas the options object into the datatable initialization astable = $('#example').DataTable( a ),thisresolved to datatable object?Thanks.
Thanks Allan. I'm clear now. Please disregard the last comment.