Editor example Home page example
| Name | Position | Office | Extn. | Start date | Salary | ||
|---|---|---|---|---|---|---|---|
| Name | Position | Office | Extn. | Start date | Salary |
- JavaScript
- HTML
- CSS
- Ajax
- Server-side script
The JavaScript shown below is used to initialise the table shown in this example:
(function () {
const reRgbToHex = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i;
const colors = [
'#b8ddf7',
'#c2e6e6',
'#cbe5be',
'#d1cfe7',
'#f9d7e7',
'#fbdeda',
'#f1cb9d',
'#fff5a0',
'#dfd1b4',
'#a7bfe3',
'#91d6d3',
'#b0d89a',
'#bfbbde',
'#f8c2da',
'#f8bca4',
'#f47725',
'#fff681',
'#b3a99d',
'#60cdf6',
'#7aced8',
'#a5d49e',
'#c7a2cd',
'#e59eac',
'#f48478',
'#f15524',
'#ffe47d',
'#7b6d6a',
'#22b1c2',
'#54c1c4',
'#abc5bc',
'#746ba0',
'#f49ab6',
'#f07276',
'#dd8720',
'#fef200',
'#483433',
'#2c91bf',
'#4ebaad',
'#afcc36',
'#5d2876',
'#f1729b',
'#f17050',
'#da5c29',
'#f8de3f',
'#d1d2d4',
'#0055a6',
'#1aa2a0',
'#8ec63f',
'#942b70',
'#ee4799',
'#c8292d',
'#bf4627',
'#e7c034',
'#88898d',
'#2d2b76',
'#2d8e95',
'#66a463',
'#462c76',
'#f05978',
'#a51e24',
'#995f5b',
'#d7bf5f',
'#58585a',
'#190e5d',
'#23767c',
'#487a47',
'#602d58',
'#dc166b',
'#922b2e',
'#644445',
'#e1cf85',
'#231f20'
];
function foregroundColor(hex) {
let split = reRgbToHex.exec(hex);
if (split) {
let r = parseInt(split[1], 16);
let g = parseInt(split[2], 16);
let b = parseInt(split[3], 16);
return r * 0.299 + g * 0.587 + b * 0.114 > 186 ? '#000000' : '#ffffff';
}
return '#000000';
}
DataTable.render.pill = () => {
let store = new Map();
return (data, type, row) => {
let background = '';
if (type === 'display') {
if (store.has(data)) {
background = store.get(data);
}
else {
background = colors[store.size % colors.length];
store.set(data, background);
}
let foreground = foregroundColor(background);
return `<span class="dt-pill" style="background: ${background}; color: ${foreground}">${data}</span>`;
}
return data;
};
};
})();
function getImageUrl(rowId) {
let id = rowId.replace('row_', '');
return id <= 57
? `../resources/images/${id}.png`
: '../resources/images/unknown.png';
}
const editor = new DataTable.Editor({
ajax: '../php/autoComplete.php',
fields: [
{
label: 'First name:',
name: 'first_name'
},
{
label: 'Last name:',
name: 'last_name'
},
{
label: 'Position:',
name: 'position'
},
{
label: 'Office:',
name: 'office',
type: 'autocomplete'
},
{
label: 'Extension:',
name: 'extn'
},
{
label: 'Start date:',
name: 'start_date',
type: 'datetime'
},
{
label: 'Salary:',
name: 'salary'
}
],
table: '#example'
});
editor.on('open', (e, mode, action) => {
if (action === 'remove' && mode === 'main') {
let rows = table.rows(editor.modifier()).data();
let lis = rows.map(d => `<li>${d.first_name} ${d.last_name}</li>`).join('');
let count = rows.length > 1 ? lis.length + ' staff members' : 'staff member';
editor.message(
`<p>Are you sure you want to delete the data for the following ${count}?</p><ul>${lis}</ul>`
);
}
else if (action === 'edit' && mode === 'main') {
let rows = table.rows(editor.modifier()).data();
let src = getImageUrl(rows[0].DT_RowId);
editor.message(
`<img class="staff-image" src="${src}">`
);
}
else {
editor.message('');
}
});
var table = new DataTable('#example', {
ajax: '../php/autoComplete.php',
columnControl: [
'order',
['search', 'spacer', 'orderAsc', 'orderDesc', 'orderClear']
],
columns: [
{
data: null,
orderable: false,
render: DataTable.render.select()
},
{
data: 'DT_RowId',
render: data => {
let src = getImageUrl(data);
return `<img class="staff-image" src="${src}">`;
},
orderable: false,
searchable: false
},
{
data: null,
render: (data) => data.first_name + ' ' + data.last_name,
editField: ['first_name', 'last_name']
},
{ data: 'position' },
{ data: 'office', render: DataTable.render.pill() },
{ data: 'extn', render: data => `<a href="tel:${data}">${data}</a>` },
{ data: 'start_date', render: DataTable.render.date() },
{ data: 'salary', render: DataTable.render.number(null, null, 0, '$') }
],
columnDefs: [
{
targets: [0, 1],
columnControl: [],
},
{
targets: [3, 4],
columnControl: [
'order',
['searchList', 'spacer', 'orderAsc', 'orderDesc', 'orderClear']
],
}
],
layout: {
topStart: {
buttons: [
{ extend: 'create', editor: editor },
{ extend: 'edit', editor: editor },
{ extend: 'remove', editor: editor },
{ extend: 'spacer' },
{
extend: 'collection',
text: 'Export',
buttons: ['copy', 'excel', 'csv', 'print']
}
]
}
},
order: [[2, 'asc']],
select: {
style: 'os',
selector: 'td:first-child'
},
ordering: {
indicators: false
},
responsive: true,
language: {
entries: {
_: 'staff members',
1: 'staff member'
}
}
});
// Activate an inline edit on click of a table cell
table.on('click', 'tbody td', function (e) {
let idx = table.column(this).index();
if (idx === 2) {
editor.bubble(this);
}
else if (idx > 2) {
editor.inline(this);
}
});
In addition to the above code, the following JavaScript library files are loaded for use in this example:
The HTML shown below is the raw HTML table element, before it has been enhanced by DataTables:
This example uses a little bit of additional CSS beyond what is loaded from the library files (below), in order to correctly display the table. The additional CSS used is shown below:
table .staff-image {
max-width: 22px;
border-radius: 13px;
margin: -4px 0;
vertical-align: middle;
background: white;
border: 2px solid white;
}
div.DTE .staff-image {
display: block;
max-width: 80px;
background: white;
border-radius: 40px;
margin: 0 auto;
}
div.DTE_Form_Info ul {
text-align: left;
margin: 0 20% 1em 20%;
list-style: none;
}
span.dt-pill {
border-radius: 0.33em;
display: inline-block;
padding: 0 0.33em;
}
td.dt-type-numeric a {
color: inherit;
border-bottom: 1px dotted black;
}
:root.dark td.dt-type-numeric a {
border-bottom: 1px dotted white;
}
The following CSS library files are loaded for use in this example to provide the styling of the table:
This table loads data by Ajax. The latest data that has been loaded is shown below. This data will update automatically as any additional data is loaded.
The script used to perform the server-side processing for this table is shown below. Please note that this is just an example script using PHP. Server-side processing scripts can be written in any language, using the protocol described in the DataTables documentation.