JavaScript Array/Object To Table Generator – vanillajs-table

Category: Javascript , Table | June 12, 2020
Author:zezyulinsv
Views Total:3,033 views
Official Page:Go to website
Last Update:June 12, 2020
License:MIT

Preview:

JavaScript Array/Object To Table Generator – vanillajs-table

Description:

A Vanilla JavaScript table generator that helps you dynamically create an HTML table with JavaScript array or object.

Installation:

1. Load the vanillajs-table.js library in the document.

<script src="src/vanillajs-table.js"></script>

2. Create a container in which you want to generate the HTML table.

<div id="table"></div>

Convert an array to a table:

var fruits = ['Apple', 'Banana', 'Orange', 'Blackberry'];
var options = {
    element: document.getElementById("table"),
    data: fruits
};
var table = new Table(options);
table.view();

Convert an array of JS arrays to a table:

var eats = [
    ['Apple', 'Banana', 'Orange', 'Blackberry'],
    ['Cabbage', 'Turnip', 'Radish', 'Carrot']
];
var options = {
    element: document.getElementById("table"),
    data: eats
};
var table = new Table(options);
table.view();

Conver an array of objects to a table:

var discography = [
    {
        album: "MCMXC a.D.",
        year: 1990
    }, {
        album: "The Cross of Changes",
        year: 1993
    }
];
var options = {
    element: document.getElementById("table"),
    data: discography
};
var table = new Table(options);
table.view();

Convert an object to a table:

var song = {
    title: "Le Roi Est Mort, Vive Le Roi!",
    year: 1996,
    project: "Enigma"
};
var options = {
    element: document.getElementById("table"),
    data: song
};
var table = new Table(options);
table.view();

With Custom Table Header:

var artists = [
    ["Andy Warhol", "1928-1987"],
    ["Jan van Eyck", "1390-1441"],
    ["El Greco", "1541-1614"]
];
var headers = [
    {name: "Artist"},
    {name: "Lifetime"}
];
var options = {
    element: document.getElementById("table"),
    headers: headers,
    data: artists
};
var table = new Table(options);
table.view();

More Configuration Options:

1. Determine the height of the generated table.

var table = new Table({
    height: 500
});

2. Determine whether or not to show undefined headers.

var table = new Table({
    undefined_headers: true
});

3. Specify the character for undefined headers.

var table = new Table({
    empty: "----"
});

You Might Be Interested In:


Leave a Reply