GOPHERSPACE.DE - P H O X Y
gophering on codemadness.org
<- Back


# jsdatatable: a small datatable Javascript

Last modification on 2020-07-20

This is a small datatable Javascript with no dependencies.


## Features

* Small:
  * Filesize: +- 9.1KB.
  * Lines: +- 300, not much code, so hopefully easy to understand.
  * No dependencies on other libraries like jQuery.
* Sorting on columns, multi-column support with shift-click.
* Filtering values: case-insensitively, tokenized (separated by space).
* Able to add custom filtering, parsing and sorting functions.
* Helper function for delayed (150ms) filtering, so filtering feels more
  responsive for big datasets.
* Permissive ISC license, see LICENSE file.
* "Lazy scroll" mode:
  * fixed column headers and renders only visible rows, this allows you to
    "lazily" render millions of rows.
* Officially supported browsers are:
  * Firefox and Firefox ESR.
  * Chrome and most recent webkit-based browsers.
  * IE10+.


## Why? and a comparison

It was created because all the other datatable scripts suck balls.

Most Javascripts nowadays have a default dependency on jQuery, Bootstrap or
other frameworks.

jQuery adds about 97KB and Bootstrap adds about 100KB to your scripts and CSS
as a dependency.  This increases the CPU, memory and bandwidth consumption and
latency. It also adds complexity to your scripts.

jQuery was mostly used for backwards-compatibility in the Internet Explorer
days, but is most often not needed anymore. It contains functionality to query
the DOM using CSS-like selectors, but this is now supported with for example
document.querySelectorAll.  Functionality like a JSON parser is standard
available now: JSON.parse().


### Size comparison

All sizes are not "minified" or gzipped.

        Name                             |   Total |      JS |   CSS | Images | jQuery
        ---------------------------------+---------+---------+-------+--------+-------
        jsdatatable                      |  12.9KB |   9.1KB | 2.5KB |  1.3KB |      -
        datatables.net (without plugins) | 563.4KB | 449.3KB |  16KB |  0.8KB | 97.3KB
        jdatatable                       | 154.6KB |    53KB |   1KB |  3.3KB | 97.3KB

* »datatables.net« (without plugins).
* jdatatable

Of course jsdatatable has less features (less is more!), but it does 90% of
what's needed.  Because it is so small it is also much simpler to understand and
extend with required features if needed.

See also:
The website obesity crisis


## Clone

        git clone git://git.codemadness.org/jscancer


## Browse

You can browse the source-code at:

* https://git.codemadness.org/jscancer/
* gopher://codemadness.org/1/git/jscancer

It is in the datatable directory.


## Download releases

Releases are available at:

* https://codemadness.org/releases/jscancer/
* gopher://codemadness.org/1/releases/jscancer


## Usage

### Examples


See example.html for an example. A stylesheet file datatable.css is also
included, it contains the icons as embedded images.

A table should have the classname "datatable" set, it must contain a 
for the column headers ( or ) and  element for the data. The
minimal code needed for a working datatable:

        
        
        
        
                
                        Click me
                
                
                        a
                        b
                
        
        
        var datatables = datatable_autoload();
        
        


### Column attributes

The following column attributes are supported:

* data-filterable: if "1" or "true" specifies if the column can be filtered,
  default: "true".
* data-parse: specifies how to parse the values, default: "string", which is
  datatable_parse_string(). See PARSING section below.
* data-sort: specifies how to sort the values: default: "default", which is
  datatable_sort_default(). See SORTING section below.
* data-sortable: if "1" or "true" specifies if the column can be sorted,
  default: "true".


### Parsing

By default only parsing for the types: date, float, int and string are
supported, but other types can be easily added as a function with the name:
datatable_parse_(). The parse functions parse the data-value
attribute when set or else the cell content (in order). Because of this
behaviour you can set the actual values as the data-value attribute and use the
cell content for display. This is useful to display and properly sort
locale-aware currency, datetimes etc.


### Filtering

Filtering will be done case-insensitively on the cell content and when set also
on the data-value attribute. The filter string is split up as tokens separated
by space. Each token must match at least once per row to display it.


### Sorting

Sorting is done on the parsed values by default with the function:
datatable_sort_default(). To change this you can set a customname string on
the data-sort attribute on the column which translates to the function:
datatable_sort_().

In some applications locale values are used, like for currency, decimal numbers
datetimes. Some people also like to use icons or extended HTML elements inside
the cell. Because jsdatatable sorts on the parsed value (see section PARSING)
it is possible to sort on the data-value attribute values and use the cell
content for display.

For example:

* currency, decimal numbers: use data-value attribute with floating-point
  number, set data-parse column to "float".
* date/datetimes: use data-value attribute with UNIX timestamps (type int), set
  data-parse on column to "int" or set the data-parse attribute on column to
  "date" which is datatable_parse_date(), then make sure to use Zulu times, like:
  "2016-01-01T01:02:03Z" or other time strings that are parsable as the
  data-value attribute.
* icons: generally use data-value attribute with integer as weight value to
  sort on, set data-parse column to "int".


### Dynamically update data

To update data dynamically see example-ajax.html for an example how to do this.


### Caveats

* A date, integer, float or other values must be able to parse properly, when
  the parse function returns NaN, null or undefined etc. the sorting behaviour is
  also undefined. It is recommended to always set a zero value for each type.
*  is not supported in datatables in "lazy" mode.


## Demo / example

**For the below example to work you need to have Javascript enabled.**

datatable-example.html