DataTable.use()
Get / set the libraries or global object upon which DataTables uses.
Description
When loading DataTables in an environment that doesn't using a global window
as its base (e.g. ES Modules) you may need to tell it about the external libraries that it can make use of. For example DataTables and use Moment.js or Luxon to handle formatted dates and times. In a browser those libraries automatically attach to the global window
, but that isn't possible in a module loader. For such cases, use this method to make them available to DataTables.
It is also possible to pass in a window
object, which can be useful for constructing a DataTable in a headless environment.
Important: If you are settings Moment.js or Luxon as a library to use for date / time manipulation, DataTables expects only a single one of the two libraries to be used. Do not set both.
Note that this method is primarily useful when using ES modules or Common JS loading. You would not typically need to use this method if you are using a script
tag to load DataTables and any dependencies as they will be automatically detected.
Types
function use(type)
- Description:
Get the library that has been set for use.
- Parameters:
Name Type Optional 1 type
No The value given for this parameter tells DataTables what library it is you are setting. It can take one of the following parameters:
jq
: jQuery - used for DOM manipulation and eventswin
: Globalwindow
object (which will also include thedocument
as a property of the object)luxon
: Luxon - optionally used for date time formattersmoment
: Moment - optionally used for date time formattersdatetime
: DateTime picker
- Returns:
Any
The library requested that is currently being used by DataTables will be returned.
function use(library)
- Description:
Set the libraries, or the global object (i.e. the
window
) that is used by DataTables for operations such as date and time manipulation with automatic detection of what the library is.- Parameters:
Name Type Optional 1 library
Any
No This should be the library, or object that is being set.
- Returns:
No value is returned.
function use(type, library)
- Description:
Set the libraries, or the global object (i.e. the
window
) that is used by DataTables for operations such as date and time manipulation, specifying what the library is.- Parameters:
Name Type Optional 1 library
Any
No This should be the library, or object that is being set.
2 type
No The value given for this parameter tells DataTables what library it is you are setting. See the parameters for the getter overload above for the values this can take.
- Returns:
No value is returned.
Examples
Detect formatted dates with Moment.js:
import moment from 'moment';
import DataTable from 'datatables.net';
// Tell DataTables that it can use Moment for date formatting
DataTable.use(moment, 'moment');
// Tell DataTables what date format you want to look for in the HTML data
DataTable.datetime('D MMM YYYY');
// Initialise DataTables
new DataTable('#example');
Use Luxon to render data into a specific format:
import luxon from 'luxon';
import DataTable from 'datatables.net';
// Tell DataTables that it can use Luxon for date formatting
DataTable.use(luxon, 'luxon');
// Initialise DataTables with a specific column formatting date information
new DataTable('#example', {
columnDefs: [
{
targets: 4,
render: DataTable.render.datetime('d MMM yyyy')
}
]
});