Editor example QR Reader
This example shows how a QR Reader can be integrated into Editor. It uses the third party library Instascan to scan QR codes and insert the results into the description column. For more information on how to implement this, please refer to the blog post.
| Name | City | Description |
|---|---|---|
| Name | City | Description |
- JavaScript
- HTML
- CSS
- Ajax
- Server-side script
The JavaScript shown below is used to initialise the table shown in this example:
/**
* [Instascan](https://github.com/schmich/instascan) Is a real-time webcam-driven HTML5 QR code scanner.
* It is a really useful tool in combination with editor that can allow for quick and easy scanning of
* QR codes in an integrated manner within the normal Editor form.
*
* @name Instascan
* @summary A real-time webcam-driven HTML5 QR code scanner.
* @requires [Instascan](https://www.npmjs.com/package/instascan)
*
* @depjs //rawgit.com/schmich/instascan-builds/master/instascan.min.js
*
* @example
*
* new DataTable.Editor( {
* "ajax": "/api/documents",
* "table": "#documents",
* "fields": [ {
* "label": "Description:",
* "name": "description",
* "type": "qr"
* },
* // additional fields...
* ]
* } );
*/
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define(['jquery', 'datatables', 'datatables-editor'], factory);
}
else if (typeof exports === 'object') {
// Node / CommonJS
module.exports = function ($, dt) {
if (!$) {
$ = require('jquery');
}
factory($, dt || DataTable || require('datatables'));
};
}
else if (jQuery) {
// Browser standard
factory(jQuery, jQuery.fn.dataTable);
}
})(function ($, DataTable) {
'use strict';
if (!DataTable.ext.editorFields) {
DataTable.ext.editorFields = {};
}
var _fieldTypes = DataTable.Editor
? DataTable.Editor.fieldTypes
: DataTable.ext.editorFields;
_fieldTypes.qr = {
create: function (conf) {
var safeId = DataTable.Editor.safeId(conf.id);
var container = $('<div/>');
var video = $('<video/>').css({
display: ' none',
'max-width': '100%',
padding: '2% 0%'
});
var scanner = new Instascan.Scanner({ video: video[0] });
var langScan = conf.scanButton || 'Scan';
var langStop = conf.stopButton || 'Stop';
conf.qrInput = $('<input id="' + safeId + '"/>').appendTo(container);
$('<button></button>')
.text(langScan)
.css({ margin: '0% 1%' })
.on('click', function () {
if (this.innerHTML === langScan) {
Instascan.Camera.getCameras()
.then(function (cameras) {
if (cameras.length > 0) {
$(scanner.video).css({ display: 'block' });
scanner.start(cameras[0]);
}
else {
console.error('No cameras found.');
}
})
.catch(function (e) {
console.error(e);
});
this.innerHTML = langStop;
}
else if (this.innerHTML === langStop) {
$(scanner.video).css({ display: 'none' });
scanner.stop();
this.innerHTML = lang.langScan;
}
})
.appendTo(container);
video.appendTo(container);
scanner.addListener('scan', function (content) {
conf.qrInput.val(content).css({ border: 'blue 2px solid' });
video.css({ border: 'blue 2px solid' });
setTimeout(() => {
conf.qrInput.css({ border: 'none' });
video.css({ border: 'none' });
}, 500);
});
this.on('close', function () {
scanner.stop();
});
return container;
},
get: function (conf) {
return conf.qrInput.val();
},
set: function (conf, val) {
conf.qrInput.val(val !== null ? val : '');
},
enable: function (conf) {},
disable: function (conf) {}
};
});
$(document).ready(function () {
var editor = new DataTable.Editor({
ajax: '../php/staff-description.php',
table: '#example',
fields: [
{
label: 'First name:',
name: 'first_name'
},
{
label: 'Last name:',
name: 'last_name'
},
{
label: 'City:',
name: 'city'
},
{
label: 'Description:',
name: 'description',
type: 'qr'
}
]
});
$('#example').DataTable({
dom: 'Bfrtip',
ajax: '../php/staff-description.php',
columns: [
{
data: null,
render: function (data, type, row) {
// Combine the first and last names into a single table field
return data.first_name + ' ' + data.last_name;
}
},
{
data: 'city',
type: 'qr'
},
{
data: 'description',
type: 'qr'
}
],
select: true,
buttons: [
{ extend: 'create', editor: editor },
{ extend: 'edit', editor: editor },
{ extend: 'remove', editor: editor }
]
});
});
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:
video {
max-width: 100%;
padding: 2% 0%;
}
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.