DataTables Column Highlighter (Standalone)
A reusable JavaScript extension for DataTables that highlights specific columns in visible rows and responsive child rows based on declarative conditions.
- Declarative config via DataTables options:
columnHighlighter.rules - Supports HTML-backed tables and JavaScript-backed tables (auto-detected)
- Target columns by header name or index; per-target styles
- Condition groups with
AND/OR/NONElogic and robust operators - Applies to visible cells on init/draw and to responsive child rows on expand
- Optional helper to toggle Responsive/ScrollX at runtime while preserving state (
datatables.toggleView.js)
- Include scripts (jQuery, DataTables, Responsive optional, this plugin):
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.5/css/jquery.dataTables.min.css" />
<link rel="stylesheet" href="https://cdn.datatables.net/responsive/2.5.0/css/responsive.dataTables.min.css" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.13.5/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/responsive/2.5.0/js/dataTables.responsive.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@evotecit/[email protected]/dist/datatables.columnHighlighter.js"></script>
<!-- Or auto-minified: -->
<!-- <script src="https://cdn.jsdelivr.net/npm/@evotecit/[email protected]/dist/datatables.columnHighlighter.min.js"></script> -->- Initialize your DataTable with a
columnHighlighterblock:
$('#myTable').DataTable({
responsive: { details: { type: 'inline' } },
columnHighlighter: {
rules: [
{
conditionsContainer: [
{
logic: 'AND',
conditions: [
{ columnName: 'Name', operator: 'eq', type: 'string', value: '1Password' },
{ columnName: 'NPM', operator: 'eq', type: 'number', value: 17984 }
]
}
],
targets: [
{ column: 'Name', css: { 'background-color': '#fa8072', color: '#000' } },
{ column: 'NPM', css: { 'background-color': '#fa8072', color: '#000' } }
]
}
]
}
});Each condition supports:
columnNameorcolumnId: pick either; the other is auto-filledoperator:eq,ne,gt,lt,le,ge,in,notin,contains,notcontains,like,notlike,between,betweenInclusivetype:string,number,bool,datevalue: scalar or array; fordateusevalueDatevalueDate:{ year, month, day, hours, minutes, seconds }or array of thosecaseSensitive:true/false(forstring)dateTimeFormat: string used bymoment(if included) for date parsingreverseCondition: swap left/right for comparisons
Group multiple conditions using conditionsContainer blocks with logic set to AND, OR, or NONE.
Each rule has targets: an array of columns to style. Each target:
column: header name (string) or index (number)backgroundColor/textColoror a fullcssobjecthighlightParent: also colors the parent element in child rows
If you prefer to attach rules in code instead of options:
setupColumnHighlighting(tableId, rules, table)setupChildRowConditionalFormatting(tableId, conditionsContainer, highlightColumn, css, failCss, table)
final-html-simple.html— HTML table, simple rulefinal-html-and.html— HTML table, AND groupfinal-html-advanced.html— HTML table, responsive demo with multiple rules, fail targets, stylesfinal-js-simple.html— JavaScript data, simple rulefinal-js-and.html— JavaScript data, AND groupfinal-js-advanced.html— JavaScript data, responsive demo with multiple rules, fail targets, stylesfinal-toggle-view-preserve.html— Toggle Responsive/ScrollX without losing highlighting or filtersfinal-toggle-view-preserve.html— Toggle Responsive/ScrollX (default Buttons theme; no styling changes)final-toggle-view-preserve-bootstrap5.html— Same with Bootstrap 5 Buttons styling
Include the helper and call hfxToggleView(api) to switch modes without losing highlighting, searches, or column visibility:
<script src="https://cdn.jsdelivr.net/npm/@evotecit/[email protected]/dist/datatables.toggleView.js"></script>
<script>
var api = $('#table').DataTable({ responsive: true, columnHighlighter: { rules: [...] } });
// later
api = window.hfxToggleView(api) || api; // switches mode & preserves stateexamples/datatables-bootstrap-striped-responsive.html— Bootstraptable-striped+ Responsive; shows consistent per-cell highlights
Bootstrap 5.3 renders zebra striping and hover states using a full-cell box-shadow overlay driven by CSS variables (for example, --bs-table-accent-bg). If you only set background-color on a cell, that overlay can tint your highlight on odd rows, making it look uneven.
When a backgroundColor is provided, the Column Highlighter now:
- sets
--bs-table-accent-bg: transparenton the target cell, and - paints an inset
box-shadowwith your color (inset 0 0 0 9999px <color>),
so highlights look identical on striped and non-striped rows. Any custom css you pass still applies last and can override these (for example { 'box-shadow': 'none' }).
The extension does not alter your styling. To add the toggle in the standard Buttons bar, include the button type and you’re done:
$('#table').DataTable({
dom: 'Bfrtip',
buttons: ['copyHtml5','excelHtml5','colvis','print','toggleView'],
responsive: true
});If you are using Bootstrap 5 theme for Buttons, load the buttons.bootstrap5 CSS/JS; the toggle inherits that look automatically. A standalone button is also available via the attribute:
<button class="btn btn-primary btn-sm" data-hfx-toggle="#table">⇄ Switch to ScrollX</button>