javascript - 在闪亮的应用程序中将 Editor QR Scanner 包装到 DT 中
问题描述
我正在尝试将 JavaScript QR Scanner 包装到闪亮的应用程序中。目标是在 DT 的可编辑单元格中包含 QR 码。以下博客 介绍了如何使用instascan
库将 QR Scanner 制作成 DT。
是否可以直接使用博客中使用的JS代码在shiny app中自定义DT。
我按照文章为闪亮(链接)打包 JavaScript 代码,但没有成功。
这里和闪亮应用程序中可编辑 DT 的示例
library(shiny)
library(DT)
shinyApp(
ui = fluidPage(
DTOutput('x2')
),
server = function(input, output, session) {
x = head(iris)
x$barCode = ""
output$x2 = renderDT(x, editable = TRUE)
}
)
将instascan库添加到 DT 的 JS 代码。
(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 || $.fn.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) {
// Section 1 - DOM setup
var safeId = DataTable.Editor.safeId(conf.id);
var video = $('<video/>').css({
display: 'none',
'max-width': '100%',
padding: '2% 0%',
});
var input = $('<input id="' + safeId + '"/>');
var scan = $('<button>Scan</button>').css({ margin: '0% 1%' });
var container = $('<div/>').append(input).append(scan).append(video);
conf.qrInput = input;
// Section 2 - Instascan setup
var scanner = new Instascan.Scanner({ video: video[0] });
scanner.addListener('scan', function (content) {
input.val(content).css({ border: 'blue 2px solid' });
video.css({ border: 'blue 2px solid' });
setTimeout(() => {
input.css({ border: 'none' });
video.css({ border: 'none' });
}, 500);
});
// Section 3 - Toggle control
scan.on('click', function () {
if (this.innerHTML === 'Scan') {
Instascan.Camera.getCameras()
.then(function (cameras) {
if (cameras.length > 0) {
video.css({ display: 'block' });
scanner.start(cameras[0]);
} else {
console.error('No cameras found.');
}
})
.catch(function (e) {
console.error(e);
});
this.innerHTML = 'Stop';
} else if (this.innerHTML === 'Stop') {
video.css({ display: 'none' });
scanner.stop();
this.innerHTML = 'Scan';
}
});
// Section 4 - Close behaviour
this.on('close', function () {
video.css({ display: 'none' });
scanner.stop();
this.innerHTML = 'Scan';
});
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) {},
};
}));
解决方案
推荐阅读
- r - LAPACK 的 `dtrcon` 底层算法
- javascript - 使用 JQuery 更改输入字段的值后如何刷新输入字段?
- java - 在画布上绘制的每个位图之间添加边距
- css - CSS:以 Bootstrap 形式垂直对齐输入
- python - 在 Python/Pandas 中检查发票/账单中是否存在多个项目
- mysql - 有没有办法从“ER_DUP_ENTRY”错误中找到重复的行 ID
- java - 使用嵌套对象列表反序列化 json
- ruby - 为什么这不会像我想要的那样遍历文件?
- javascript - 跨数组访问对象值
- python - python shell 如何输出与 python idle 不同的输出?