Create HTML tables from a JSON in a both Node.js (0.10+) and browsers enviroments.
Installation
yarn add --production table-builder
or
npm i --production table-builder
--production
flag skips devDependencies of the table-builder (testing framework).
Usage
node.js and webpack
browser (without build tool)
-
Copy built UMD module:
cp node_modules/table-builder/tablebuilder.js dist/tablebuilder.js
-
Insert tag:
<script src="/dist/tablebuilder.js"></script>
Simple Example
Each object represents one row in the data array.
var data = /* see data section above */; // You can put key-value pairs if you strongly want keep headers order:// [['name', 'User name'], ['age', 'User age'], ['link', 'Homepage']]var headers = "name" : "User name" "age": "User age" "link": "Homepage" ; var Table = ;console;
Rendered to:
User name User age Homepage Larry Wall 57 www.wall.org/~larry/ Bill Gates 56 www.microsoft.com Daffy Duck 75
Example of simple scrapper with tablebuilder result representation
const process = const TableBuilder = const table = class: 'avito' const headers = price: 'Price' title: 'Title'const thrw = const fetch = const getHttp = const parseHtml = const uri = processargv2 || 'https://www.avito.ru/moskva/telefony/iphone?q=iphone+se' const retreiveData = Array const main = const style = `<style>body { text-align: center; } .avito {width: 100%;} thead { text-align: left; } .price-td { text-align: right; }</style>`
API
Prisms
Prism are callbacks-preprocessors for specified fields.
var data = // Look the previous case differences: link format changed and name splitted into firstname and surname "firstname":"Larry" "surname":"Wall" "age":57 "link": "www.wall.org/~larry/" "firstname":"Bill" "surname":"Gates" "age":56 "link": "www.microsoft.com" "firstname":"Daffy" "surname":"Duck" "age":75 "link": "" ; 'class': 'some-table'
Render output is equal the previous case.
Also, prism callback may return {presentation: '...', raw: '...'}
object
for splitting html wrapped cell values and raw values.
For example, raw values uses in totals.
Totals
See following code:
table;
It adds tfoot
in the table with average age:
62
Grouping
Grouping fields util (setGroup
).
// ...table // ... ;
Group removes the field (product_category
) from the table
and adds row-separators with the field's values (group names). and referenced items.
Body of the setGroup callback may contains processor of group name.
Additionaly processor may use the group's recordsCount
and totals
collection for group
if setTotal
for whole table have installed.
If callback is not defined then tableBuilder uses group name without processing, as is.
Empty data collection
// Show table replacer block if data set is empty// ...table // ... || 'Data collection is empty!';
Client side sorting, filtering
You can use list.js with table builder.
TODO
- Unit tests, CI
- Decompose methods
- More unit tests
- Run building and another activity only in the render() method, push intermediate methods into preordered list
- Framefork agnostic: possibility to use with React and another frameworks
tagBuilder
as a dependency injection (for compatibility with either:innerHTML
,createElement
,React.Component
)
- Internal type constructors with asserts
- Data model, changing/accessing data api
- Client-side filters, multisort s
- Plural versions of methods:
setPrisms
,setTotals
- Plugins system (call hooks for different cells)
- N/A maps
- Escaping
See also another solutions
React based:
- react-data-grid - I did not use it stil
- react-table - I revealed problems with custom styles and stbility
- suggestions are welcome
Framework agnostic:
- suggestions are welcome