{hero}

columnDefs.targets

Since: DataTables 1.10

Assign a column definition to one or more columns.

Description

The columnDefs option allows a column definition object to be defined and then assigned to one or more columns in a DataTable, regardless of the order of the column definitions array, or the order of the columns in the table.

This columnDefs.targets option provides the information required by DataTables for which columns in the table the column definition object should be applied.

It can be:

  • 0 or a positive integer - column index counting from the left
  • A negative integer - column index counting from the right
  • A CSS selector - columns that match the selector will be used (since 2.0)
  • The string _all - all columns (i.e. assign a default)
  • A string ending in :name - this will match columns that have a name defined using columns.name (since 2.0).
  • A string with just letters, numbers underscore and dash (/^[a-z][\w-]*$/i as regex) - a class name match. Please note that this option is provided for backwards compatibility with DataTables 1.x. It does mean you can't just use element names as a selector, but that would be limited to th and td in the header anyway.

Additionally, targets can be either a single option from the list above, or an array of options (the different types can be mixed in the array if required). For example targets: [ -1, -2 ] would target the last and second last columns in the table.

Please note that unlike all other column configuration options which can be applied to be columns and columnDefs, the columnDefs.targets option can only be used in columnDefs.

Type

This option can be given in the following type(s):

Examples

Disable filtering on the first column:

new DataTable('#myTable', {
	columnDefs: [
		{
			targets: 0,
			searchable: false
		}
	]
});

Disable sorting on the first and third columns:

new DataTable('#myTable', {
	columnDefs: [
		{
			targets: [0, 2],
			orderable: false
		}
	]
});

Disable ordering on columns which have a class of 'nosort':

new DataTable('#myTable', {
	columnDefs: [
		{
			targets: 'nosort',
			orderable: false
		}
	]
});

Use of a name to match a column:

new DataTable('#myTable', {
	columns: [
		null,
		null,
		null,
		{ name: 'action' }
	],
	columnDefs: [
		{
			targets: 'action:name',
			defaultContent: '<button>Run</button>'
		}
	]
});

Related

The following options are directly related and may also be useful in your application development.