Coder Social home page Coder Social logo

harrego / html-table-tool Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 18 KB

Lightweight JS library that fills a HTML table with data in minimal code

Home Page: https://www.npmjs.com/package/html-table-tool

License: The Unlicense

JavaScript 100.00%

html-table-tool's Introduction

html-table-tool

html-table-tool is a lightweight JS library that fills a HTML table with data in minimal code.

Install

Webpack / ES6 / NPM

First, install the library like so: npm i html-table-tool, then import the library function in an ES6 JS file, import fillTable from "html-table-tool"

Browser JS File

Download the latest version from the releases page and place the downloaded JS file somewhere in your project structure then import it with HTML like so: <script type="text/javascript" src="table-tool-1.0.0.min.js"></script>, the tool is then usable as fillTable()

Make sure you change the src to the appropriate location in your project

Usage

The library uses one function fillTable with one parameter, an object with 5 required children:

Child name Expected type Description Return type
tableElement DOM table element The table to insert the data into N/A
sectionCount Int The amount of sections to produce the table, if a header if needed then 2 is generally set N/A
rowCountForSection Function The amount of rows within a section, a function is required with the parameter (section) Integer
columnCountForIndexPath Function The amount of columns for a particular row, a function is required with the parameter (indexPath) (more about index paths can be seen below), in this case the column child of the index path object is null Integer
cellForIndexPath Function The cell that will be inserted into the table, a function is required with the parameters (cell, indexPath) (more about index paths can be seen below), you must edit the cell and return it DOM td element (from cell parameter)

An example of a broken down object can be seen here:

Variable setup

// The data to display in the table
var people = ["Harry", "Jane", "Brian"]

// Create the table object
var tableObjects = {

tableElement

// DOM table element
tableElement: document.getElementById("example-table"),

sectionCount

// Amount of sections, in this case 1 for a header and another for the data
sectionCount: 2,

rowCountForSection

// Amount of rows in each section
rowCountForSection: function(section) {
	// If the first section (the header), we only need 1
	if (section == 0) { return 1 }

	// If it's the second section (the data), we need the match the rows with the data we want to display
	else { return people.length }
},

columnCountForIndexPath

// In this case only 1 column for both sections
columnCountForIndexPath: function(indexPath) { return 1 },

cellForIndexPath

// Returning the cell
cellForIndexPath: function(cell, indexPath) {
	// If it's the first section (the header), simply display the single column header
	if (indexPath.section == 0) { cell.innerHTML = "<b>Name</b>" }

	// If it's the second section (the data), display the data for the position the index path gives
	else { cell.innerHTML = people[indexPath.row] }
	return cell
}

Finalising

}
// Fill the table using the setup object
fillTable(tableObjects)

Building

NOTE: Building is only required for development.

The project uses webpack and babel to bundle a single JS file for the browser.

To build first install all depencides:

npm i

Then build with webpack:

npm run build (or npm run dev for development and debugging)

html-table-tool's People

Contributors

harrego avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.