node-red-contrib-tableify
Version:
Uses npm package tableify to convert json into an html table. Returns the html in msg.payload
124 lines (106 loc) • 4.82 kB
JavaScript
module.exports = function (RED) {
"use strict";
var tableify = require('tableify');
var replaceall = require('replaceall');
function ConvertJSONToTable(config) {
RED.nodes.createNode(this, config);
this.before = config.before;
this.after = config.after;
this.tableStyle = config.tableStyle;
this.theadStyle = config.theadStyle;
this.tbodyStyle = config.tbodyStyle;
this.trStyle = config.trStyle;
this.tdStyle = config.tdStyle;
this.on('input', function(msg){
var node = this;
var before = msg.before;
if(before === undefined || before === null || before === ""){
before = node.before;
}
var after = msg.after;
if(after === undefined || after === null || after === ""){
after = node.after;
}
var tableNew = 'font-family:Arial;width:100%;border:1px solid #ccc;border-collapse: collapse;color:#667677;';
var theadNew = 'border:1px solid #ccc;background:#6a9ef2;color:white !important; height:50px;padding:2px;';
var tbodyNew = ''
var trNew = 'border:1px solid #ccc;';
var tdNew = 'border:1px solid #ccc;text-align:center;padding:3px;';
var style= ' style="';
var end = '"';
var tableOld = '<table';
var theadOld = '<thead';
var tbodyOld = '<tbody';
var trOld = '<tr';
var tdOld = '<td';
var tableStyle = msg.tableStyle;
if(tableStyle === undefined || tableStyle === null || tableStyle === "" ){
tableStyle = node.tableStyle;
if(tableStyle === undefined || tableStyle === null || tableStyle === "" ){
tableStyle = tableNew;
}
}
tableStyle = tableOld+style+tableStyle+end;
var theadStyle = msg.theadStyle;
if(theadStyle === undefined || theadStyle === null || theadStyle === "" ){
theadStyle = node.theadStyle;
if(theadStyle === undefined || theadStyle === null || theadStyle === "" ){
theadStyle = theadNew;
}
}
theadStyle = theadOld+style+theadStyle+end;
var tbodyStyle = msg.tbodyStyle;
if(tbodyStyle === undefined || tbodyStyle === null || tbodyStyle === ""){
tbodyStyle = node.tbodyStyle;
if(tbodyStyle === undefined || tbodyStyle === null || tbodyStyle === "" ){
tbodyStyle = tbodyNew;
}
}
tbodyStyle = tbodyOld+style+tbodyStyle+end;
var trStyle = msg.trStyle;
if(trStyle === undefined || trStyle === null || trStyle === "" ){
trStyle = node.trStyle;
if(trStyle === undefined || trStyle === null || trStyle === ""){
trStyle = trNew;
}
}
trStyle = trOld+style+trStyle+end
var tdStyle = msg.tdStyle;
if(tdStyle === undefined || tdStyle === null || tdStyle === "" ){
tdStyle = node.tdStyle;
if(tdStyle === undefined || tdStyle === null || tdStyle === ""){
tdStyle = tdNew;
}
}
tdStyle = tdOld+style+tdStyle+end;
if(msg.payload === undefined || msg.payload === null){
node.log("Payload is blank. Handling by sending before, no results & after output");
msg.payload = "";
msg.payload += before +"<br>"
msg.payload += "- No results to show. -";
msg.payload += "<br>"+ after;
node.send(msg);
}else{
var html = tableify(msg.payload);
html = replaceall(tableOld,tableStyle,html);
html = replaceall(theadOld,theadStyle,html);
html = replaceall(tbodyOld, tbodyStyle, html);
html = replaceall(trOld,trStyle,html);
html = replaceall(tdOld, tdStyle,html);
console.log(html);
msg.payload = "";
if(before != undefined && before != null && before != ""){
msg.payload += before;
msg.payload += "<br>";
}
msg.payload += html ;
if(after != undefined && after != null && after != ""){
msg.payload += "<br>";
msg.payload += after;
}
node.send(msg);
}
});
};
RED.nodes.registerType("tableify", ConvertJSONToTable);
}