There are times when you may wish to call API functions inside the DataTables callback functions (for
example initComplete
, rowCallback
etc). The complicating issue with this is that
the object hasn't fully initialised, so you can't assign the result to a variable and then use that variable in
the callback. However, all of DataTables' callback functions are executed in the scope of the DataTable
instance, so you can use the Javascript special variable this
to access the API
(this.api()
will give an API instance).
In this example you will be able to see that the $()
method is used to get all cell nodes in the table's body
and then act on them (in this case adding a click event). Note also the value of this
stored in
the variable that
so it can be used inside the jQuery click function, where the execution scope
has been changed to the td element!). The action here is to apply the filter with the value of what is in each
cell.
It is also worth noting that this same effect could be achieved by attaching a delegated click event after the table has been initialised - but the example shows the use of the API inside the callbacks!
Name | Age | Position | Office | Start date | Salary |
---|---|---|---|---|---|
Name | Age | Position | Office | Start date | Salary |
Tiger Nixon | 61 | System Architect | Edinburgh | 2011/04/25 | $3,120 |
Garrett Winters | 63 | Director | Edinburgh | 2011/07/25 | $5,300 |
Ashton Cox | 66 | Technical Author | San Francisco | 2009/01/12 | $4,800 |
Cedric Kelly | 22 | Javascript Developer | Edinburgh | 2012/03/29 | $3,600 |
Jenna Elliott | 33 | Financial Controller | Edinburgh | 2008/11/28 | $5,300 |
Brielle Williamson | 61 | Integration Specialist | New York | 2012/12/02 | $4,525 |
Herrod Chandler | 59 | Sales Assistant | San Francisco | 2012/08/06 | $4,080 |
Rhona Davidson | 55 | Integration Specialist | Edinburgh | 2010/10/14 | $6,730 |
Colleen Hurst | 39 | Javascript Developer | San Francisco | 2009/09/15 | $5,000 |
Sonya Frost | 23 | Software Engineer | Edinburgh | 2008/12/13 | $3,600 |
Jena Gaines | 30 | System Architect | London | 2008/12/19 | $5,000 |
Quinn Flynn | 22 | Financial Controller | Edinburgh | 2013/03/03 | $4,200 |
Charde Marshall | 36 | Regional Director | San Francisco | 2008/10/16 | $5,300 |
Haley Kennedy | 43 | Senior Marketing Designer | London | 2012/12/18 | $4,800 |
Tatyana Fitzpatrick | 19 | Regional Director | London | 2010/03/17 | $2,875 |
Michael Silva | 66 | Senior Marketing Designer | London | 2012/11/27 | $3,750 |
Paul Byrd | 64 | Javascript Developer | New York | 2010/06/09 | $5,000 |
Gloria Little | 59 | Systems Administrator | New York | 2009/04/10 | $3,120 |
Bradley Greer | 41 | Software Engineer | London | 2012/10/13 | $3,120 |
Dai Rios | 35 | System Architect | Edinburgh | 2012/09/26 | $4,200 |
Jenette Caldwell | 30 | Financial Controller | New York | 2011/09/03 | $4,965 |
Yuri Berry | 40 | System Architect | New York | 2009/06/25 | $3,600 |
Caesar Vance | 21 | Technical Author | New York | 2011/12/12 | $4,965 |
Doris Wilder | 23 | Sales Assistant | Edinburgh | 2010/09/20 | $4,965 |
Angelica Ramos | 36 | System Architect | London | 2009/10/09 | $2,875 |
Gavin Joyce | 42 | Developer | Edinburgh | 2010/12/22 | $4,525 |
Jennifer Chang | 28 | Regional Director | London | 2010/11/14 | $4,080 |
Brenden Wagner | 18 | Software Engineer | San Francisco | 2011/06/07 | $3,750 |
Ebony Grimes | 48 | Software Engineer | San Francisco | 2010/03/11 | $2,875 |
Russell Chavez | 20 | Director | Edinburgh | 2011/08/14 | $3,600 |
Michelle House | 37 | Integration Specialist | Edinburgh | 2011/06/02 | $3,750 |
Suki Burks | 53 | Developer | London | 2009/10/22 | $2,875 |
Prescott Bartlett | 27 | Technical Author | London | 2011/05/07 | $6,730 |
Gavin Cortez | 22 | Technical Author | San Francisco | 2008/10/26 | $6,730 |
Martena Mccray | 46 | Integration Specialist | Edinburgh | 2011/03/09 | $4,080 |
Unity Butler | 47 | Senior Marketing Designer | San Francisco | 2009/12/09 | $3,750 |
Howard Hatfield | 51 | Financial Controller | San Francisco | 2008/12/16 | $4,080 |
Hope Fuentes | 41 | Financial Controller | San Francisco | 2010/02/12 | $4,200 |
Vivian Harrell | 62 | System Architect | San Francisco | 2009/02/14 | $4,965 |
Timothy Mooney | 37 | Financial Controller | London | 2008/12/11 | $4,200 |
Jackson Bradshaw | 65 | Director | New York | 2008/09/26 | $5,000 |
Miriam Weiss | 64 | Support Engineer | Edinburgh | 2011/02/03 | $4,965 |
Bruno Nash | 38 | Software Engineer | London | 2011/05/03 | $4,200 |
Odessa Jackson | 37 | Support Engineer | Edinburgh | 2009/08/19 | $3,600 |
Thor Walton | 61 | Developer | New York | 2013/08/11 | $3,600 |
Finn Camacho | 47 | Support Engineer | San Francisco | 2009/07/07 | $4,800 |
Elton Baldwin | 64 | Data Coordinator | Edinburgh | 2012/04/09 | $6,730 |
Zenaida Frank | 63 | Software Engineer | New York | 2010/01/04 | $4,800 |
Zorita Serrano | 56 | Software Engineer | San Francisco | 2012/06/01 | $5,300 |
Jennifer Acosta | 43 | Javascript Developer | Edinburgh | 2013/02/01 | $2,875 |
Cara Stevens | 46 | Sales Assistant | New York | 2011/12/06 | $4,800 |
Hermione Butler | 47 | Director | London | 2011/03/21 | $4,080 |
Lael Greer | 21 | Systems Administrator | London | 2009/02/27 | $3,120 |
Jonas Alexander | 30 | Developer | San Francisco | 2010/07/14 | $5,300 |
Shad Decker | 51 | Regional Director | Edinburgh | 2008/11/13 | $5,300 |
Michael Bruce | 29 | Javascript Developer | Edinburgh | 2011/06/27 | $4,080 |
Donna Snider | 27 | System Architect | New York | 2011/01/25 | $3,120 |