{hero}

columnDefs

Since: DataTables 1.10

Set column definition initialisation properties.

Description

Very similar to columns, this parameter allows you to assign specific options to columns in the table, although in this case the column options defined can be applied to one or more columns. Additionally, not every column need be specified, unlike columns.

This parameter is an array of column definition objects, where the options available exactly match those for columns (see below for list of options in the related links).

In addition to the column property options, columnDefs requires a targets property to be set in each definition object (columnDefs.targets). This targets property tells DataTables which column(s) the definition should be applied to. 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 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.

Conflict resolution

As columnDefs allows columns to be defined one or more times in different column definition objects (typically to define different aspects of the columns) conflicts can arise whereby a single property might be defined with different values for the same column. Likewise, this situation could also occur when columns is used at the same time. DataTables uses the following rules to resolve such conflicts:

  1. A property defined in columns will always take priority over any value for that property defined in columnDefs.
  2. Properties which are higher in the columnDefs array will take priority over those below.

Consider for example the following table:

var table = $('#myTable').DataTable( {
    columnDefs: [
        { targets: [0, 1], visible: true},
        { targets: '_all', visible: false }
    ]
} );

The first and second columns will be visible in the table while all others will be hidden.

Type

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

Example

Disable filtering on the first column:

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

Related

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