### Basic Deferred Rendering Example
Source: https://datatables.net/extensions/select/examples/initialisation/deferRender.html
This example demonstrates the basic setup for deferred rendering in DataTables.net. It initializes the table with deferred rendering enabled, allowing the table to be processed in the background.
```javascript
var table = $('#example').DataTable( {
"deferRender": true
} );
```
--------------------------------
### Basic Table Initialization with Fill Plug-in
Source: https://datatables.net/extensions/autofill/examples/initialisation/plugins.html
Demonstrates initializing a DataTables table with basic fill plug-ins. This example shows how to set up a table with columns for Name, Position, Office, Age, Start date, and Salary.
```html
Name
Position
Office
Age
Start date
Salary
Tiger Nixon
System Architect
Edinburgh
61
2011-04-25
$320,800
Garrett Winters
Accountant
Tokyo
63
2011-07-25
$170,750
Ashton Cox
Junior Technical Author
San Francisco
66
2009-01-12
$86,000
Cedric Kelly
Senior JavaScript Developer
Edinburgh
22
2012-03-29
$433,060
Airi Satou
Accountant
Tokyo
33
2008-11-28
$162,700
Brielle Williamson
Integration Specialist
New York
61
2012-12-02
$372,000
Herrod Chandler
Sales Assistant
San Francisco
59
2012-08-06
$137,500
Rhona Davidson
Integration Specialist
Tokyo
55
2010-10-14
$327,900
Colleen Hurst
JavaScript Developer
San Francisco
39
2009-09-15
$205,500
Sonya Frost
Software Engineer
Edinburgh
23
2012-12-01
$103,600
```
--------------------------------
### Table Initialization Event
Source: https://datatables.net/extensions/keytable/examples/initialisation/events.html
This example demonstrates binding to the 'init' event, which is triggered after the table has been fully initialized. This is a good place to perform setup tasks that depend on the table being ready.
```javascript
$.fn.dataTable.moment('YYYY-MM-DD');
$('#example').on('init', function () {
console.log('DataTables has been initialized!');
});
$('#example').DataTable({
// ... other DataTables options
});
```
--------------------------------
### KeyTable Initialization with Event Binding
Source: https://datatables.net/extensions/keytable/examples/initialisation/events.html
This example demonstrates how to initialize KeyTable on a DataTables instance and bind to its events. It shows the basic setup required to enable keyboard navigation and event handling.
```javascript
$(document).ready(function() {
$('#example').DataTable();
var table = $('#example').DataTable();
new $.fn.dataTable.KeyTable( table );
table.on( 'key-focus', function ( e, datatable, cell, originalCell ) {
var row = table.row( cell.index().row );
// Example: Log the data of the focused row
console.log( 'Row data: '+ row.data() );
} );
} );
```
--------------------------------
### Custom Rendering Function for Start Date
Source: https://datatables.net/extensions/searchpanes/examples/advanced/renderSearch.html
This example shows a custom rendering function for the 'start_date' column. It formats the date into a more readable 'Month Day, Year' format. This is useful for presenting dates in a user-friendly way.
```javascript
function startDate( d ) {
var date = new Date( d.start_date );
var month = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
return month[ date.getUTCMonth() ] + ' ' + date.getUTCDate() + ', ' + date.getUTCFullYear();
}
```
--------------------------------
### Basic Table Initialization with startAndEndRender
Source: https://datatables.net/extensions/rowgroup/examples/initialisation/startAndEndRender.html
This example shows a basic DataTables initialization using jQuery. It includes the HTML table structure and the JavaScript code to initialize DataTables with custom start and end render functions for a specific column.
```html
Name
Position
Office
Age
Start date
Salary
Tiger Nixon
System Architect
Edinburgh
61
2011-04-25
$320,800
Garrett Winters
Accountant
Tokyo
63
2011-07-25
$170,750
Ashton Cox
Junior Technical Author
San Francisco
66
2009-01-12
$86,000
Cedric Kelly
Senior JavaScript Developer
Edinburgh
22
2012-03-29
$433,060
Airi Satou
Accountant
Tokyo
33
2008-11-28
$162,700
Brielle Williamson
Integration Specialist
New York
61
2012-12-02
$372,000
Herrod Chandler
Sales Assistant
San Francisco
59
2012-08-06
$137,500
Rhona Davidson
Integration Specialist
Tokyo
55
2010-10-14
$327,900
Colleen Hurst
JavaScript Developer
San Francisco
39
2009-09-15
$205,500
Sonya Frost
Software Engineer
Edinburgh
23
2008-12-13
$103,600
```
```javascript
$(document).ready(function() {
$('#example').DataTable( {
initComplete: function () {
this.api().columns().every( function () {
var column = this;
var select = $('')
.appendTo( $(column.footer()).empty() )
.on( 'change', function () {
column
.search( $(this).val() )
.draw();
} );
column.data().unique().sort().each( function ( d, j ) {
select.append( $('').text(d) );
} );
} );
}
} );
} );
```
--------------------------------
### Basic Row Selection Example
Source: https://datatables.net/extensions/fixedcolumns/examples/integration/select.html
This example shows how to initialize a DataTable and enable row selection. Clicking a row will highlight it.
```javascript
new DataTable('#example', {
orderCellsTop: true,
fixedHeader: true
});
```
--------------------------------
### HTML Table Structure for Reset Ordering Example
Source: https://datatables.net/extensions/colreorder/examples/initialisation/reset.html
The raw HTML for the table used in the reset ordering example. This includes the table element with the ID 'example' and a button with the ID 'reset' to trigger the reset functionality.
```html
Name
Position
Office
Age
Start date
Salary
Tiger Nixon
System Architect
Edinburgh
61
2011-04-25
$320,800
Garrett Winters
Accountant
Tokyo
63
2011-07-25
$170,750
```
--------------------------------
### Get/Set Fixed Columns at Start
Source: https://datatables.net/extensions/fixedcolumns/api
Use the `fixedColumns().start()` method to get or set the number of columns fixed at the start of a DataTables table.
```javascript
$( selector ).DataTable().fixedColumns().start();
```
--------------------------------
### Get DateTime Value
Source: https://datatables.net/extensions/datetime/api/val%28%29
Retrieve the current date value from a DateTime instance. This example shows how to get the value within a change event listener.
```javascript
let el = document.getElementById('example');
let dt = new DateTime(el);
el.addEventListener('change', function () {
console.log('Selected value is: ', dt.val());
});
```
--------------------------------
### Basic Table Initialization with Pre-Selected Rows
Source: https://datatables.net/extensions/searchpanes/examples/initialisation/preSelect.html
This example shows a standard HTML table that will be initialized with DataTables and SearchPanes. The pre-selection logic is handled by the `searchPanes.preSelect` option.
```html
Name
Position
Office
Age
Start date
Salary
Tiger Nixon
System Architect
Edinburgh
61
2011-04-25
$320,800
Garrett Winters
Accountant
Tokyo
63
2011-07-25
$170,750
Ashton Cox
Junior Technical Author
San Francisco
66
2009-01-12
$86,000
Cedric Kelly
Senior JavaScript Developer
Edinburgh
22
2012-03-29
$433,060
Airi Satou
Accountant
Tokyo
33
2008-11-28
$162,700
Brielle Williamson
Integration Specialist
New York
61
2012-12-02
$372,000
Herrod Chandler
Sales Assistant
San Francisco
59
2012-08-06
$137,500
Rhona Davidson
Integration Specialist
Tokyo
55
2010-10-14
$327,900
Colleen Hurst
JavaScript Developer
San Francisco
39
2009-09-15
$205,500
Sonya Frost
Software Engineer
Edinburgh
23
2008-12-01
$103,600
```
--------------------------------
### Bootstrap Styling Example
Source: https://datatables.net/extensions/scroller/examples/styling/bootstrap.html
This example shows a basic DataTables initialization with Bootstrap styling applied. Ensure Bootstrap CSS is included in your project for this styling to take effect.
```html
ID
First name
Last name
Zip
Country
1
Armand
Warren
56045
Taiwan
2
Xenos
Salas
71090
Liberia
3
Virginia
Whitaker
62723
Nicaragua
4
Kato
Patrick
97662
Palau
5
Penelope
Hensley
76634
Greenland
6
Georgia
Erickson
81358
Bolivia
7
Shad
Pena
20600
Palestinian Territory, Occupied
8
Tanisha
Humphrey
93371
Kenya
9
Claire
Espinoza
I8S 2S8
Panama
10
Raya
Tucker
O8D 8W7
Botswana
11
Otto
Briggs
57590
Anguilla
12
Logan
Burt
53041
Venezuela
13
Cooper
Pennington
36994
France
14
Kristen
Peterson
52917
Sao Tome and Principe
15
Jordan
Velasquez
08884
Switzerland
16
Zelda
Freeman
F9H 1J9
Holy See (Vatican City State)
17
Mary
Pacheco
A7Y 6X9
Niger
```
--------------------------------
### Luxon Date Formatting Example
Source: https://datatables.net/extensions/columncontrol/examples/searchDates/transform-luxon.html
This example shows how to use Luxon to format a date string for display in a DataTables column. It assumes the 'Start date' column contains date strings that Luxon can parse.
```javascript
const luxon = require('luxon');
const table = new DataTable('#example', {
columnDefs: [
{
targets: [4], // 'Start date' column
render: function (data, type, row) {
if (type === 'display') {
return luxon.DateTime.fromISO(data).toLocaleString(luxon.DateTime.DATE_MED);
}
return data;
}
}
]
});
```
--------------------------------
### Select API: Initialization and Configuration
Source: https://datatables.net/extensions/select/api
Methods for initializing the Select extension and configuring its behavior, including blurable state, information display, selectable items, keyboard navigation, and item selection style.
```javascript
select();
```
```javascript
select.blurable();
```
```javascript
select.info();
```
```javascript
select.items();
```
```javascript
select.keys();
```
```javascript
select.selectable();
```
```javascript
select.selector();
```
```javascript
select.style();
```
```javascript
select.toggleable();
```
--------------------------------
### Basic Date Formatting with Moment.js
Source: https://datatables.net/extensions/searchbuilder/examples/initialisation/date-fmt.html
This example shows how to format a date column using Moment.js. Ensure Moment.js is included in your project.
```javascript
var table = $('#example').DataTable({
columnDefs: [
{
targets: [2, 3],
render: function (data, type, row) {
return type === 'display' ?
moment.unix(data).format('YYYY/MM/DD') :
data;
}
}
]
});
```
--------------------------------
### Basic Table Initialization
Source: https://datatables.net/extensions/rowgroup/examples/initialisation/endRender.html
Initialize a DataTables table with default settings. This is the most common way to get started.
```javascript
$(document).ready( function() {
$('#example').DataTable();
} );
```
--------------------------------
### Basic JavaScript Initialisation
Source: https://datatables.net/extensions/buttons/examples/initialisation/new.html
Initialise DataTables on an existing HTML table using its ID. This is the most common way to get started.
```javascript
$(document).ready(function() {
$('#example').DataTable();
} );
```
--------------------------------
### Basic Buttons Initialisation
Source: https://datatables.net/extensions/buttons/examples/initialisation/topLevel.html
This example shows the fundamental way to initialise DataTables with the Buttons extension using the top-level `buttons` option. Ensure you have included the necessary DataTables and Buttons CSS and JavaScript files.
```javascript
new DataTable('#myTable', {
layout: {
topStart: 'buttons'
}
});
```
--------------------------------
### Basic FixedColumns Table
Source: https://datatables.net/extensions/colreorder/examples/integration/fixedcolumns.html
This example shows a basic DataTables setup with the FixedColumns extension enabled. It requires the DataTables and FixedColumns CSS and JS files.
```html
First name
Last name
Position
Office
Age
Start date
Salary
Extn.
E-mail
Tiger
Nixon
System Architect
Edinburgh
61
2011-04-25
$320,800
5421
t.nixon@datatables.net
Garrett
Winters
Accountant
Tokyo
63
2011-07-25
$170,750
8422
g.winters@datatables.net
Ashton
Cox
Junior Technical Author
San Francisco
66
2009-01-12
$86,000
1562
a.cox@datatables.net
Cedric
Kelly
Senior JavaScript Developer
Edinburgh
22
2012-03-29
$433,060
6224
c.kelly@datatables.net
Airi
Satou
Accountant
Tokyo
33
2008-11-28
$162,700
5407
a.satou@datatables.net
Brielle
Williamson
Integration Specialist
New York
61
2012-12-02
$372,000
4804
b.williamson@datatables.net
```
--------------------------------
### State Save with Data Example
Source: https://datatables.net/extensions/select/examples/initialisation/stateSave.html
This JSON data represents a typical structure for saving DataTables state, including information about columns, start index, search, and order.
```json
{
"data": [
{
"id": "1",
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$320,800",
"start_date": "2011/04/25",
"office": "Edinburgh",
"extn": "5421"
},
{
"id": "2",
"name": "Garrett Winters",
"position": "Accountant",
"salary": "$170,750",
"start_date": "2011/07/25",
"office": "Tokyo",
"extn": "8422"
},
{
"id": "3",
"name": "Ashton Cox",
"position": "Junior Technical Author",
"salary": "$86,000",
"start_date": "2009/01/12",
"office": "San Francisco",
"extn": "1562"
},
{
"id": "4",
"name": "Cedric Kelly",
"position": "Senior Javascript Developer",
"salary": "$433,060",
"start_date": "2012/03/29",
"office": "Edinburgh",
"extn": "6224"
},
{
"id": "5",
"name": "Airi Satou",
"position": "Accountant",
"salary": "$162,700",
"start_date": "2008/11/28",
"office": "Tokyo",
"extn": "5407"
},
{
"id": "6",
"name": "Brielle Williamson",
"position": "Integration Specialist",
"salary": "$372,000",
"start_date": "2012/12/02",
"office": "New York",
"extn": "4804"
},
{
"id": "7",
"name": "Herrod Chandler",
"position": "Sales Assistant",
"salary": "$137,500",
"start_date": "2012/08/06",
"office": "San Francisco",
"extn": "9608"
},
{
"id": "8",
"name": "Rhona Davidson",
"position": "Integration Specialist",
"salary": "$327,900",
"start_date": "2010/10/14",
"office": "Tokyo",
"extn": "6200"
},
{
"id": "9",
"name": "Colleen Hurst",
"position": "Javascript Developer",
"salary": "$205,500",
"start_date": "2009/09/15",
"office": "San Francisco",
"extn": "2360"
},
{
"id": "10",
"name": "Sonya Frost",
"position": "Software Engineer",
"salary": "$103,600",
"start_date": "2008/12/13",
"office": "Edinburgh",
"extn": "1667"
},
{
"id": "11",
"name": "Jena Gaines",
"position": "Office Manager",
"salary": "$90,560",
"start_date": "2008/12/19",
"office": "London",
"extn": "3814"
},
{
"id": "12",
"name": "Quinn Flynn",
"position": "Support Lead",
"salary": "$342,000",
"start_date": "2013/03/03",
"office": "Edinburgh",
"extn": "9497"
},
{
"id": "13",
"name": "Charde Marshall",
"position": "Regional Director",
"salary": "$470,600",
"start_date": "2008/10/16",
"office": "San Francisco",
"extn": "6741"
}
]
}
```
--------------------------------
### Initializing DateTime with Options
Source: https://datatables.net/extensions/datetime/option
Demonstrates how to initialize the DateTime picker with custom options using both vanilla JavaScript and jQuery.
```APIDOC
## Initializing DateTime with Options
### Description
This section shows how to apply configuration options when initializing the DataTables DateTime picker. You can pass an object containing various settings to customize its behavior.
### JavaScript Initialization
```javascript
new DateTime(document.getElementById('myInput'), {
format: 'D MMM YYYY HH:mm'
});
```
### jQuery Initialization
```javascript
$('#myInput').dtDateTime({
format: 'D MMM YYYY HH:mm'
});
```
### Options Reference
The following options can be configured:
**DateTime Core Options:**
- `alwaysVisible` (boolean): Makes the date picker always display.
- `buttons` (object): Container for navigation buttons.
- `buttons.clear` (boolean): Enable button to clear the value.
- `buttons.selected` (boolean): Enable button to move the current value into view.
- `buttons.today` (boolean): Enable button to move "today" into view.
- `disableDays` (array): Specify days that cannot be selected.
- `firstDay` (number): Change which day of the week is first on the calendar.
- `format` (string): The format of the date data.
- `hoursAvailable` (array): Sets which hours are selectable.
- `i18n` (object): DateTime language options.
- `i18n.amPm` (array): The strings appended to hours to distinguish between am and pm.
- `i18n.clear` (string): Clear button text value.
- `i18n.hours` (string): Label for the hours selection.
- `i18n.minutes` (string): Label for the minutes selection.
- `i18n.months` (array): Text used to indicate the months available for selection.
- `i18n.next` (string): String to use in the next button.
- `i18n.previous` (string): String to use in the previous button.
- `i18n.seconds` (string): Label for the seconds selection.
- `i18n.selected` (string): Selected button text value.
- `i18n.today` (string): Today button text value.
- `i18n.unknown` (string): String to use if a value is unknown.
- `i18n.weekdays` (array): Text used to indicate the days of the week.
- `locale` (string): The locale that DateTime should use.
- `maxDate` (string|Date): Set the maximum date that can be selected and displayed.
- `minDate` (string|Date): Set the minimum date that can be selected and displayed.
- `minutesAvailable` (array): Sets which minutes are selectable.
- `onChange` (function): Function that is called whenever the value selected for DateTime changes.
- `secondsAvailable` (array): Sets which seconds are selectable.
- `showWeekNumber` (boolean): Whether the week number should be shown.
- `yearRange` (string): The range of years provided for selection.
```
--------------------------------
### DataTables Initialization with Column Reordering and Visibility
Source: https://datatables.net/extensions/columncontrol/examples/colReorder/withColumnVisibility.html
Example of initializing DataTables with extensions for column reordering and visibility control. This setup allows for dynamic manipulation of table columns.
```javascript
new DataTable('#example', {
layout: {
top: 'search'
},
columnDefs: [
{
targets: [0, 1, 2, 3, 4, 5],
// You can also use 'visible' to control the initial visibility of columns
// visible: false
}
],
colReorder: true,
columnVisibility: true
});
```
--------------------------------
### JSON Data for DataTables
Source: https://datatables.net/extensions/responsive/examples/column-control/classes.html
This is an example of the JSON data structure used to populate the DataTables instance. It includes fields for name, position, salary, start date, office, and extension.
```json
{
"data": [
{
"id": "1",
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$320,800",
"start_date": "2011-04-25",
"office": "Edinburgh",
"extn": "5421"
},
{
"id": "2",
"name": "Garrett Winters",
"position": "Accountant",
"salary": "$170,750",
"start_date": "2011-07-25",
"office": "Tokyo",
"extn": "8422"
},
{
"id": "3",
"name": "Ashton Cox",
"position": "Junior Technical Author",
"salary": "$86,000",
"start_date": "2009-01-12",
"office": "San Francisco",
"extn": "1562"
},
{
"id": "4",
"name": "Cedric Kelly",
"position": "Senior JavaScript Developer",
"salary": "$433,060",
"start_date": "2012-03-29",
"office": "Edinburgh",
"extn": "6224"
},
{
"id": "5",
"name": "Airi Satou",
"position": "Accountant",
"salary": "$162,700",
"start_date": "2008-11-28",
"office": "Tokyo",
"extn": "5407"
},
{
"id": "6",
"name": "Brielle Williamson",
"position": "Integration Specialist",
"salary": "$372,000",
"start_date": "2012-12-02",
"office": "New York",
"extn": "4804"
},
{
"id": "7",
"name": "Herrod Chandler",
"position": "Sales Assistant",
"salary": "$137,500",
"start_date": "2012-08-06",
"office": "San Francisco",
"extn": "9632"
}
```
--------------------------------
### Initialize DataTables with KeyTable using Vanilla JS
Source: https://datatables.net/extensions/keytable/examples/styling/bootstrap.html
This snippet demonstrates initializing DataTables with KeyTable using the vanilla JavaScript API. Ensure the necessary DataTables/KeyTable Bootstrap JS files are included.
```javascript
new DataTable( "#example", {
keys: true
});
```
--------------------------------
### Server-Side Processing Script Example (PHP)
Source: https://datatables.net/extensions/staterestore/examples/initialisation/predefined.html
This PHP script demonstrates a basic server-side processing setup for DataTables. It handles data retrieval and formatting for dynamic table updates.
```php
connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Fetch data from the database
$sql = "SELECT name, position, office, age, start_date, salary FROM employees";
$result = $conn->query($sql);
$data = array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$data[] = array(
"name" => $row['name'],
"position" => $row['position'],
"office" => $row['office'],
"age" => $row['age'],
"start_date" => $row['start_date'],
"salary" => $row['salary']
);
}
}
// Output data as JSON
header('Content-Type: application/json');
echo json_encode(array(
"data" => $data
));
$conn->close();
?>
```