{hero}

DataTable.util.get()

Since: DataTables 1.11.0

Create a read function from a descriptor.

Description

It can often be useful in Javascript to write data location descriptors as a string, as we often do with JSON notation - e.g. staff.name, or having the flexibility of using a function to get arbitrary data. The DataTables columns.data and columns.render properties make use of this to be able to easily describe where data should be fetched from to display in a column. This method exposes that ability as part of the DataTables API for use in plug-ins and other libraries.

The key point with this method is to remember that it will itself return a function, which you must then execute to be able to read the nested data - e.g.:

let fn = DataTable.util.get('name.first');
let name = fn({
    name: {
        first: 'Fiona',
        last: 'Grayling'
    }
}); // Returns `Fiona`

The descriptor can be given as:

  • null: The returned function will simply be return the full data object given to the accessor function.
  • A function: Returns a function that will call the given function with the same parameters given to it (effectively just making it a proxy function).
  • An object: Returns a function that will access data from an object based on the second parameter passed into the accessor. In DataTables this is used for the columns.render property's ability to use orthogonal data in an object.
  • A string: Returns a function that will access data based on a JSON notation string, with the additional ability of being able to use () notation to execute a function, and [] to get array details. If [] is used without characters inside it for accessing an array, an array will be returned; if characters are used inside the brackets, they will be used to join the array - e.g. [, ] would give a comma space separated string.

Type

function get( descriptor )

Description:

Build an accessor function to get data based on the given descriptor.

Returns:

An accessor function that will be used to read data from a given object