Tidy Table

Create a HTML table from JSON that can be sorted, selected, and post-processed using a simple callback.

Features

Check out the demo provided with this package.

Installation

  1. Download the latest sources to your computer using a web browser.
  2. Extract the contents of the .zip into a folder on your local computer.
  3. Upload the folder with the following files to your web site.
Filename Role
jquery.tidy.table.min.js The main script to be included from within your HTML document.
jquery.tidy.table.min.css This style sheet that defines the "look & feel" of the HTML table.

Set-up

It's as simple as defining the target element #container using the jQuery selector and passing tabular data as JSON. There are options available for post-processing table/column/menu elements, if needed.

$(selector).TidyTable(options, data [columnTitles, columnValues, menuOptions, postProcess]);

Source Code

Add the following Javascript between the <head></head> tags of your HTML document.

Use Example 1 - Options defined

<script src="http://www.google.com/jsapi"></script>
<script>
	google.load('jquery','1.7.1');
</script>

<link rel="stylesheet" type="text/css" href="/path/to/jquery.tidy.table.min.css">

<script src="/path/to/jquery.tidy.table.min.js"></script>
<script>
$(document).ready(function() {
	$('#container')
		.TidyTable({
			enableCheckbox : true,
			enableMenu     : true
		},
		{
			columnTitles : ['Column A','Column B','Column C','Column D','Column E'],
			columnValues : [
				['1','1A','1B','1C','1D','1E'],
				['2','2A','2B','2C','2D','2E'],
				['3','3A','3B','3C','3D','3E'],
				['4','4A','4B','4C','4D','4E'],
				['5','5A','5B','5C','5D','5E']
			],

			// do something with selected results
			menuOptions : [
				['Option 1', { callback : doSomething1 }],
				['Option 2', { callback : doSomething2 }]
			],

			// post-process DOM elements
			postProcess : {
				table  : doSomething3,
				column : doSomething4,
				menu   : doSomething5
			}
		});
</script>

Use Example 2 - Quick and dirty

<script src="http://www.google.com/jsapi"></script>
<script>
	google.load('jquery','1.7.1');
</script>

<link rel="stylesheet" type="text/css" href="/path/to/jquery.tidy.table.min.css">

<script src="/path/to/jquery.tidy.table.min.js"></script>
<script>
$(document).ready(function() {
	$('#container')
		.TidyTable({
			columnTitles : ['Column A','Column B','Column C','Column D','Column E'],
			columnValues : [
				['1','1A','1B','1C','1D','1E'],
				['2','2A','2B','2C','2D','2E'],
				['3','3A','3B','3C','3D','3E'],
				['4','4A','4B','4C','4D','4E'],
				['5','5A','5B','5C','5D','5E']
			]
		});
});
</script>

Plug-in Settings

The following options can be passed to the plug-in main function as JSON

Option Description Default Value
enableCheckbox add checkbox functionality to table output false
enableMenu add select menu functionality to manipulate table output false

Select Menu Events

When a callback function is defined a response is returned. The following functions correspond to the examples provided above.

function doSomething1(rows) {
	alert('callback1(rows=' + rows.length + "')");
}

function doSomething2(rows) {
	alert('callback2(rows=' + rows.length + "')");
}

Post-processing Elements

There are times where you may need to customize the table result behavior. This can be easily achieved using optional postProcess hooks. The following functions correspond to the examples provided above.

Hide the second column of results

function doSomething3(table) {
	table.find('th:nth-child(2), td:nth-child(2)').css('display','none');
}

Create form element (for inline editing) on mouse event

function doSomething4(col) {
	col.bind('click', function() {
		var $this = $(this);

		if ($this.find('form')[0]) return;

		var form = $('<form></form>')
			.bind('submit', function() {
				$.ajax({
					type : 'POST',
					url  : 'http://domain.com/path/to/script'
				});
			});

		var field = $('<input></input>')
			.attr({
				type  : 'text',
				value : $this.text()
			});

		var button = $('<input></input>')
			.attr({
				type  : 'submit',
				value : 'Save'
			});

		$this.html( form.append(field, button) );
	});
}

Hide menus when a session cookie doesn't exist

function doSomething5(menu) {
	if (!$.cookie('session')) {
		menu.css('display','none');
	}
}

Releases

I have included with this package a packed version (3.2 kB) and developers version (unpacked 7.3 kB)

You can always find the latest updates within this projects repository.

Code Repository

This projects repository is currently hosted on Github

https://github.com/nuxy/Tidy-Table

Maintainer

For feedback, bug reports, or other comments, feel free to contact me at: devel at mbrooks dot info

License and Warranty

This package is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.

Tidy Table is provided under the terms of the MIT license.

Tidy Table ©2012-2013 Marc S. Brooks