Customizing DataTables options in GravityView
A DataTables View draws its table with the DataTables JavaScript library. Most of what the table does is set on the DataTables tab of the View’s Settings box, which Getting started with the DataTables Layout lists. For anything the tab doesn’t offer, you can change the library’s options with the PHP filters on this page.
The main filter #
The gravityview_datatables_js_options filter receives the DataTables settings of each DataTables View, just before the table is drawn. Every example on this page uses this shape:
/**
* Changes the DataTables settings of every DataTables View.
*
* @param array $dt_config The DataTables settings for the View.
* @param int $view_id The ID of the View.
*
* @return array The changed settings.
*/
add_filter( 'gravityview_datatables_js_options', function ( $dt_config, $view_id ) {
// Change $dt_config here.
return $dt_config;
}, 20, 2 );Note: The PHP examples on this page go in your site’s code. Adding custom PHP code snippets to your website shows where.
Keep the priority at 20. GravityView’s own DataTables settings change the same options at the default priority, 10, after your filter has run, so at 10 some changes are undone. A search box hidden at priority 10 comes back, and export buttons set at 10 show up next to the ones chosen in the View’s settings.
The filter also passes the post the View is shown on and the DataTables class instance, which the examples don’t use. The developer reference for this filter describes all four.
Note: A View with Save Table State turned on, the default, reopens the table the way each visitor last left it in that browser tab. A new default sort or page length then shows only in a new tab. The Save Table State setting in DataTables explains the setting.
Changing only some Views #
An example with no View ID check changes every DataTables View on your site. To change only some of them, check the View’s ID first.
One View
/**
* Hides the DataTables search box on one View.
*
* @param array $dt_config The DataTables settings for the View.
* @param int $view_id The ID of the View.
*
* @return array The changed settings.
*/
add_filter( 'gravityview_datatables_js_options', function ( $dt_config, $view_id ) {
// Replace 123 with the ID of your View.
if ( 123 !== $view_id ) {
return $dt_config;
}
$dt_config['searching'] = false;
return $dt_config;
}, 20, 2 );Several Views
/**
* Shows every entry on one page in three Views.
*
* @param array $dt_config The DataTables settings for the View.
* @param int $view_id The ID of the View.
*
* @return array The changed settings.
*/
add_filter( 'gravityview_datatables_js_options', function ( $dt_config, $view_id ) {
// Replace these with the IDs of your Views.
$target_views = [ 123, 456, 789 ];
if ( in_array( $view_id, $target_views, true ) ) {
$dt_config['paging'] = false;
}
return $dt_config;
}, 20, 2 );Page length options #
The Show entries menu above the table offers 10, 25, 50, All, and the View’s Number of entries per page, which is also the number the table opens with. Both can be changed in code.
Using the menu’s own filter
The gravityview_datatables_lengthmenu filter changes the menu’s choices. Each key is a number of entries and each value is what the menu shows for it. The key -1 shows every entry:
/**
* Changes the choices in the DataTables "Show entries" menu.
*
* @param array $values Number of entries => menu label.
* @param array $view_data The View's data.
*
* @return array The menu choices.
*/
add_filter( 'gravityview_datatables_lengthmenu', function ( $values, $view_data ) {
return [
10 => 10,
25 => 25,
50 => 50,
100 => 100,
250 => 250,
-1 => 'All',
];
}, 10, 2 );Keep your View’s Number of entries per page among the keys, since the table still opens with that number.
Using the main filter
The main filter can set the menu and the first page length together. The menu then needs two lists, the numbers first and the labels second:
/**
* Sets the "Show entries" menu and opens the table with 25 entries.
*
* @param array $dt_config The DataTables settings for the View.
*
* @return array The changed settings.
*/
add_filter( 'gravityview_datatables_js_options', function ( $dt_config ) {
$dt_config['lengthMenu'] = [
[ 10, 25, 50, 100, -1 ], // Numbers of entries.
[ 10, 25, 50, 100, 'All' ], // What the menu shows.
];
$dt_config['pageLength'] = 25;
return $dt_config;
}, 20 );With one list, such as [ 10, 25, 50, -1 ], the menu shows -1 where “All” should be.
The DataTables search box #
DataTables adds its own Search: box above the table of a View that has no Search Bar widget. A View with a Search Bar widget never shows it. To hide the box on every DataTables View:
/**
* Hides the DataTables search box on every DataTables View.
*
* @param array $dt_config The DataTables settings for the View.
*
* @return array The changed settings.
*/
add_filter( 'gravityview_datatables_js_options', function ( $dt_config ) {
$dt_config['searching'] = false;
return $dt_config;
}, 20 );The box goes, and a Search Bar widget and Field Filters keep working. Configuring the Search Bar widget covers the Search Bar.
Pagination controls #
Turning off pagination
With paging off, the table shows every entry in one list and the Show entries menu goes away:
/**
* Shows every entry on one page, with no page links.
*
* @param array $dt_config The DataTables settings for the View.
*
* @return array The changed settings.
*/
add_filter( 'gravityview_datatables_js_options', function ( $dt_config ) {
$dt_config['paging'] = false;
return $dt_config;
}, 20 );Hiding the Show entries menu
/**
* Hides the "Show entries" menu. The page links stay.
*
* @param array $dt_config The DataTables settings for the View.
*
* @return array The changed settings.
*/
add_filter( 'gravityview_datatables_js_options', function ( $dt_config ) {
$dt_config['lengthChange'] = false;
return $dt_config;
}, 20 );Hiding the entry count
/**
* Hides the "Showing 1 to 10 of 64 entries" line.
*
* @param array $dt_config The DataTables settings for the View.
*
* @return array The changed settings.
*/
add_filter( 'gravityview_datatables_js_options', function ( $dt_config ) {
$dt_config['info'] = false;
return $dt_config;
}, 20 );Column sorting #
Without code, the View’s Sort by field and Sort direction settings set the order the table opens in. How a DataTables View sorts its entries explains how the table sorts.
Turning off sorting
Visitors can no longer sort by clicking a column heading. The entries keep the View’s own sort order:
/**
* Turns off sorting by column headings.
*
* @param array $dt_config The DataTables settings for the View.
*
* @return array The changed settings.
*/
add_filter( 'gravityview_datatables_js_options', function ( $dt_config ) {
$dt_config['ordering'] = false;
return $dt_config;
}, 20 );Turning off sorting on one column
Columns are counted from 0, left to right, in the order the table shows them:
/**
* Turns off sorting on the first column.
*
* @param array $dt_config The DataTables settings for the View.
*
* @return array The changed settings.
*/
add_filter( 'gravityview_datatables_js_options', function ( $dt_config ) {
if ( ! isset( $dt_config['columnDefs'] ) ) {
$dt_config['columnDefs'] = [];
}
// Add to columnDefs. Never replace it.
$dt_config['columnDefs'][] = [
'targets' => 0, // The first column.
'orderable' => false,
];
return $dt_config;
}, 20 );Setting the default sort order
This opens the table sorted by the third column, in descending order, in place of the View’s own sort settings:
/**
* Opens the table sorted by the third column, descending.
*
* @param array $dt_config The DataTables settings for the View.
*
* @return array The changed settings.
*/
add_filter( 'gravityview_datatables_js_options', function ( $dt_config ) {
$dt_config['order'] = [
[ 2, 'desc' ], // Column 2 is the third column.
];
return $dt_config;
}, 20 );Opening with no column sorted
With an empty order, no column heading is marked as sorted when the table opens. The entries still arrive in the View’s own sort order:
/**
* Opens the table with no column marked as sorted.
*
* @param array $dt_config The DataTables settings for the View.
*
* @return array The changed settings.
*/
add_filter( 'gravityview_datatables_js_options', function ( $dt_config ) {
$dt_config['order'] = [];
return $dt_config;
}, 20 );Column settings #
The columnDefs option holds settings for single columns. Always add to it, as the examples below do.
When a View sorts by a field that isn’t one of its columns, GravityView adds a hidden column for that field at the end of the table, through columnDefs. The hidden column has no heading and holds none of the field’s values.
A snippet that replaces columnDefs makes it show up as an extra column at the right, with an empty heading. Its cells are empty in Ajax (Server-side) mode and hold numbers such as 0000000001 in Preloaded (Client-side) mode.
Adding a CSS class to a column
The class goes on the column’s heading and on each of its cells:
/**
* Adds a CSS class to the first column.
*
* @param array $dt_config The DataTables settings for the View.
*
* @return array The changed settings.
*/
add_filter( 'gravityview_datatables_js_options', function ( $dt_config ) {
if ( ! isset( $dt_config['columnDefs'] ) ) {
$dt_config['columnDefs'] = [];
}
$dt_config['columnDefs'][] = [
'targets' => 0, // The first column.
'className' => 'my-custom-class', // The CSS class.
];
return $dt_config;
}, 20 );Adding custom CSS to your Views shows where to put the CSS for that class.
Hiding columns
A hidden column still counts for the DataTables search box, so a search can match text in it. Export buttons leave hidden columns out of the file:
/**
* Hides the second and fourth columns of one View.
*
* @param array $dt_config The DataTables settings for the View.
* @param int $view_id The ID of the View.
*
* @return array The changed settings.
*/
add_filter( 'gravityview_datatables_js_options', function ( $dt_config, $view_id ) {
// Replace 123 with the ID of your View.
if ( 123 !== $view_id ) {
return $dt_config;
}
if ( ! isset( $dt_config['columnDefs'] ) ) {
$dt_config['columnDefs'] = [];
}
$dt_config['columnDefs'][] = [
'targets' => [ 1, 3 ], // The second and fourth columns.
'visible' => false,
];
return $dt_config;
}, 20, 2 );Column widths
Set widths in the View editor: each field in Visible Table Columns has a Percent Width setting, and Controlling column widths and pinning columns in DataTables explains it. A width added through columnDefs changes nothing, because GravityView sets every column’s width itself.
The Loading data message #
While the table fetches entries, it shows a Loading data… message. In the Ajax (Server-side) processing mode, this turns the message off:
/**
* Turns off the "Loading data…" message (Ajax mode only).
*
* @param array $dt_config The DataTables settings for the View.
*
* @return array The changed settings.
*/
add_filter( 'gravityview_datatables_js_options', function ( $dt_config ) {
$dt_config['processing'] = false;
return $dt_config;
}, 20 );A View in the Preloaded (Client-side) mode still shows the message. How to disable the “Loading data” message hides it with CSS in both modes.
To change the message’s words instead:
/**
* Changes the words of the "Loading data…" message.
*
* @param string $text The message. Default: "Loading data…".
* @param \GV\View $view The View.
*
* @return string The new message.
*/
add_filter( 'gravityview_datatables_loading_text', function ( $text, $view ) {
return 'Loading the attendee list…';
}, 10, 2 );Export buttons #
The Buttons setting on the DataTables tab chooses which export buttons show above the table, and Buttons (DataTables Setting) explains each one. The examples below need that setting on.
CSV files and UTF-8
The CSV button already saves the file as UTF-8, with a byte order mark and a heading row, so the encoding needs no code. To change the separator, see How to change the separator in DataTables CSV exports.
PDF title and file name
To change the PDF file’s title and name, use the PDF button’s own filter. It works when PDF is ticked under Buttons:
/**
* Changes the title and file name of one View's PDF export.
*
* @param array $button The PDF button's settings.
* @param int $view_id The ID of the View.
*
* @return array The changed settings.
*/
add_filter( 'gravityview/datatables/button_pdf', function ( $button, $view_id ) {
// Replace 123 with the ID of your View.
if ( 123 !== $view_id ) {
return $button;
}
$button['title'] = 'Conference attendees';
$button['filename'] = 'attendees';
return $button;
}, 10, 2 );The title is printed above the table, and the file is saved as attendees.pdf.
Choosing the buttons in code
Set the buttons option to choose the buttons in code. Your list replaces the buttons ticked in the View’s settings:
/**
* Shows CSV (no heading row), PDF (with a title), and Print buttons.
*
* @param array $dt_config The DataTables settings for the View.
*
* @return array The changed settings.
*/
add_filter( 'gravityview_datatables_js_options', function ( $dt_config ) {
$dt_config['buttons'] = [
[
'extend' => 'csv',
'header' => false, // No heading row in the file.
],
[
'extend' => 'pdfHtml5',
'title' => 'Conference attendees',
],
[
'extend' => 'print',
],
];
return $dt_config;
}, 20 );Changing the table’s text #
The View’s No Entries Message and “No Search Results” Text settings change the message in an empty table without code, as Modifying the text displayed when there are no entries shows. This filter changes those messages and the table’s other words:
/**
* Changes the words DataTables shows around the table.
*
* @param array $language The DataTables text, by key.
*
* @return array The changed text.
*/
add_filter( 'gravityview/datatables/config/language', function ( $language ) {
// The View has no entries.
$language['zeroRecords'] = 'No attendees have registered yet.';
// A search found nothing.
$language['emptyTable'] = 'No attendees match your search.';
$language['info'] = 'Showing _START_ to _END_ of _TOTAL_ attendees';
$language['infoEmpty'] = 'No attendees to show';
$language['search'] = 'Find an attendee:';
return $language;
} );In GravityView, zeroRecords is the message for a View with no entries and emptyTable the message for a search that finds nothing. The filter’s text replaces both View settings.
In DataTables 3.15.2 or newer, zeroRecords and emptyTable are shown as HTML, so they can hold a link. Plain text that contains < or & needs esc_html() to show as typed. Without it, the browser reads <name> as a tag and drops it:
/**
* Links the no-entries message and escapes the no-results message.
*
* @param array $language The DataTables text, by key.
*
* @return array The changed text.
*/
add_filter( 'gravityview/datatables/config/language', function ( $language ) {
// HTML: the link works.
$language['zeroRecords'] = 'No attendees have registered yet. '
. '<a href="https://example.com/register/">Register for the conference</a>.';
// Plain text with < and >: esc_html() shows them as typed.
$language['emptyTable'] = esc_html(
'No attendee matches that search. Search by <name> or <company>.'
);
return $language;
} );Field Filters hint text #
When DataTables Field Filters are on, this changes the hint in each text and drop-down filter. A date filter has none. A text filter shows the hint inside the box, and a drop-down filter shows it as its first choice:
/**
* Changes the hint text of DataTables Field Filters.
*
* @param string $placeholder The hint. Default: "Filter by" and the column's label.
* @param string $field_label The column's label.
*
* @return string The new hint.
*/
add_filter(
'gravityview/datatables/field_filters/placeholder',
function ( $placeholder, $field_label ) {
return 'Search by ' . $field_label . '...';
},
10,
2
);Complete examples #
A table with no controls
This table has no search box, Show entries menu, entry count, page links, or sorting. Export buttons still show while the Buttons setting is on, so turn it off on the DataTables tab for a table with no controls at all.
/**
* Shows one View as a plain table: every entry, no search, sorting or paging.
*
* @param array $dt_config The DataTables settings for the View.
* @param int $view_id The ID of the View.
*
* @return array The changed settings.
*/
add_filter( 'gravityview_datatables_js_options', function ( $dt_config, $view_id ) {
// Replace 123 with the ID of your View.
if ( 123 !== $view_id ) {
return $dt_config;
}
$dt_config['searching'] = false; // No search box.
$dt_config['lengthChange'] = false; // No "Show entries" menu.
$dt_config['ordering'] = false; // No sorting.
$dt_config['info'] = false; // No "Showing 1 to 10 of 64 entries" line.
$dt_config['paging'] = false; // Every entry on one page.
return $dt_config;
}, 20, 2 );
Larger page lengths
/**
* Offers larger page lengths and opens the table with 100 entries.
*
* @param array $dt_config The DataTables settings for the View.
*
* @return array The changed settings.
*/
add_filter( 'gravityview_datatables_js_options', function ( $dt_config ) {
$dt_config['lengthMenu'] = [
[ 25, 50, 100, 250, 500, -1 ],
[ 25, 50, 100, 250, 500, 'All' ],
];
$dt_config['pageLength'] = 100;
return $dt_config;
}, 20 );An export-ready table
/**
* Gives one View CSV, PDF, and Print buttons and 50 entries a page.
*
* @param array $dt_config The DataTables settings for the View.
* @param int $view_id The ID of the View.
*
* @return array The changed settings.
*/
add_filter( 'gravityview_datatables_js_options', function ( $dt_config, $view_id ) {
// Replace 123 with the ID of your View.
if ( 123 !== $view_id ) {
return $dt_config;
}
$dt_config['buttons'] = [
[ 'extend' => 'csv' ],
[ 'extend' => 'pdfHtml5', 'title' => 'Conference attendees' ],
[ 'extend' => 'print' ],
];
$dt_config['order'] = [];
$dt_config['lengthMenu'] = [
[ 25, 50, 100, -1 ],
[ 25, 50, 100, 'All' ],
];
$dt_config['pageLength'] = 50;
return $dt_config;
}, 20, 2 );
Options reference #
| Option | Type | What it changes |
|---|---|---|
lengthMenu | array | The choices in the Show entries menu. Use two lists to show “All”. |
pageLength | int | How many entries the table opens with. |
lengthChange | bool | false hides the Show entries menu. |
paging | bool | false shows every entry on one page. |
searching | bool | false hides the DataTables search box. |
ordering | bool | false turns off sorting by column headings. |
info | bool | false hides the “Showing 1 to 10 of 64 entries” line. |
processing | bool | false turns off the Loading data… message in Ajax (Server-side) mode. |
order | array | The column and direction the table opens sorted by. |
columnDefs | array | Settings for single columns. Add to it, never replace it. |
buttons | array | The export buttons. Replaces the buttons ticked in the View’s settings. |
The DataTables options reference lists every option the library has.