Skip to content

Commit 52f5890

Browse files
committed
V5 - JSON support
V5 - JSON support Styling fixes Bug fixes Signed-off-by: Jose Antonio Ciccio <jciccio@gmail.com>
1 parent 8696c03 commit 52f5890

File tree

5 files changed

+82
-25
lines changed

5 files changed

+82
-25
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ React JS Table and log viewer with CSV download functionality
99

1010
# Description
1111

12-
React JS Table and log viewer with Search and CSV download functionality. You can display data table information passing JS objects and an array of dats you want to show and download the full data into a csv file. You can pick what fields of the object you want to display and download the full objects.
12+
React JS Table and log viewer with Search and CSV download functionality. You can display data table information passing JS objects and an array of dats you want to show and download the full data into a csv file. You can pick what fields of the object you want to display and download the full objects. Also you can pass a String with a JSON and will be rendered.
1313

1414

1515
# How it looks
@@ -119,6 +119,12 @@ Changes the background to white and the text to blue
119119
| caseInsensitive | boolean |N| do searches without casing|
120120

121121
# What's new
122+
v0.5.0
123+
* Table now speaks JSON, you can pass as param a JSON in string format and will be rendered.
124+
125+
v0.4.12
126+
* Download button CSS fix
127+
122128
v0.4.11
123129
* links style fixed
124130
* Search box fixed

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-js-table-with-csv-dl",
3-
"version": "0.4.11",
3+
"version": "0.5.0",
44
"description": "React JS tables and log viewer with stats if needed. Has the functionality to dowload table contents in CSV file with data stored in class prop.",
55
"main": "build/index.js",
66
"peerDependencies": {
@@ -11,7 +11,8 @@
1111
"webpack": "^2.6.1",
1212
"file-saver": "^1.3.8",
1313
"react-js-paginator": "^0.1.7",
14-
"react-js-search": "^0.2.4"
14+
"react-js-search": "^0.2.4",
15+
"react-html-parser": "^2.0.2"
1516
},
1617
"scripts": {
1718
"test": "echo \"Error: no test specified\" && exit 1",
@@ -40,7 +41,8 @@
4041
"babel-polyfill": "^6.26.0",
4142
"react-js-paginator": "^0.1.7",
4243
"file-saver": "^1.3.8",
43-
"react-js-search": "^0.2.4"
44+
"react-js-search": "^0.2.4",
45+
"react-html-parser": "^2.0.2"
4446
},
4547
"repository": {
4648
"type": "git",

react-table-example/src/App.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,20 @@ class App extends Component {
99

1010
let headers = ["number", "position", "name"];
1111

12-
let table = [ {number: 12, name:"Buffon", position: "ST", success: true},
13-
{number: 21, name: "Pirlo", position: "MC", success: false},
12+
13+
let json = {
14+
"Key1": {"id":10,"values":"1-2"},
15+
"Key2":{},
16+
"Key3":"(Zone 1)",
17+
"description":"","array":[100],
18+
"array2":[],
19+
"Object":[{"id":1000,"values":"K-1"}]};
20+
21+
let table = [ {number: 12, name:"Buffon", position: JSON.stringify(json), success: true},
22+
{number: 21, name: "Pirlo", position: JSON.stringify(json), success: false},
1423
{number: 10, name: "Ruiz", position: "MDI"},
1524
{number: 7, name: "Nesta", position: "RB", success: true},
16-
{number: 4, name: "Cannavaro", position: "CB"},
25+
{number: 4, name: "Cannavaro", position: JSON.stringify(json)},
1726
{number: 2, name: "Puyol", position: "CB", success: false},
1827
{number: 15, name: "Abate", position: "LB"},
1928
{number: 16, name: "Locatelli", position: "MDI"},

src/index.js

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Paginator from 'react-js-paginator';
66
import SearchBar from 'react-js-search';
77
import FileSaver from 'file-saver/FileSaver';
88
import PropTypes from 'prop-types';
9-
9+
import ReactHtmlParser from "react-html-parser";
1010
import "./logViewer.css";
1111

1212
/**
@@ -28,7 +28,7 @@ class TableViewer extends Component {
2828
}
2929
}
3030

31-
highlightSyntax(json) {
31+
highlightSyntax(json) {
3232
if (json) {
3333
json = json
3434
.replace(/&/g, "&amp;")
@@ -38,17 +38,17 @@ class TableViewer extends Component {
3838
return json.replace(
3939
/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+-]?\d+)?)/g,
4040
function(match) {
41-
var cls = "number";
41+
var cls = "hljs-number";
4242
if (/^"/.test(match)) {
4343
if (/:$/.test(match)) {
44-
cls = "key";
44+
cls = "hljs-key";
4545
} else {
46-
cls = "string";
46+
cls = "hljs-string";
4747
}
4848
} else if (/true|false/.test(match)) {
49-
cls = "boolean";
49+
cls = "hljs-boolean";
5050
} else if (/null/.test(match)) {
51-
cls = "null";
51+
cls = "hljs-null";
5252
}
5353
return '<span class="' + cls + '">' + match + "</span>";
5454
}
@@ -308,13 +308,43 @@ class TableViewer extends Component {
308308
rowData.push(this.renderLineNumber(number));
309309
}
310310
let rowContent = headers.map((header, element) => {
311-
return (
312-
<div
313-
key={`table_row_${i}_cell_${element}`}
314-
className="divTableCell">
315-
{row[header]}
316-
</div>
317-
)
311+
312+
let content = row[header];
313+
let isJson = false;
314+
try {
315+
if (isNaN(content)){
316+
content = JSON.parse(content);
317+
isJson = true;
318+
}
319+
} catch (e) {
320+
content = row[header];
321+
isJson = false;
322+
}
323+
324+
if (isJson){
325+
let jsonText =JSON.stringify(content,undefined,2);
326+
let highlight = this.highlightSyntax(jsonText);
327+
let parsedHtml = ReactHtmlParser(highlight, true);
328+
return (
329+
<div
330+
key={`table_row_${i}_cell_${element}`}
331+
className="divTableCell">
332+
<pre>
333+
{parsedHtml}
334+
</pre>
335+
</div>
336+
)
337+
}
338+
else{
339+
return (
340+
<div
341+
key={`table_row_${i}_cell_${element}`}
342+
className="divTableCell">
343+
{content}
344+
</div>
345+
)
346+
}
347+
318348
});
319349
return [...rowData, ...rowContent];
320350
}

src/logViewer.css

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@
168168
.logViewer .csvFileDownloader{
169169
margin-bottom: 10px;
170170
float: left;
171-
height: 10px;
172171
}
173172

174173

@@ -256,10 +255,21 @@
256255
}
257256

258257
.logViewer .search-container {
259-
float: right;
260-
padding-right: 0px;
261-
margin-right: -7px;
258+
float: right;
259+
padding-right: 0px;
260+
margin-right: -7px;
262261
}
263262

263+
.hljs-string { color: #0052cc; }
264+
.hljs-number { color: #009933; }
265+
.hljs-boolean { color: blue; }
266+
.hljs-null { color: magenta; }
267+
.hljs-key { color: #000; }
264268

269+
.jsonviewer_json pre{
270+
height: 746px;
271+
}
265272

273+
pre {
274+
text-align: left;
275+
}

0 commit comments

Comments
 (0)