Home

Awesome

TABLE binding for Knockout

The table binding provides a fast method for displaying tables of data using Knockout. table is about ten times faster than nested foreach bindings.

Examples

This example outputs a two-dimensional array as a table.

var vm = {
    data: [
        [ 1, 2, 3 ],
        [ 4, 5, 6 ],
        [ 7, 8, 9 ]
    ]
};
<table data-bind="table: data"></table>

This example uses a header array (which determines the number of columns in the table) and a data two-dimensional array.

var vm = {
    header: [ 'x', 'y', 'z' ],
    data: [
        [ 1, 2, 3 ],
        [ 4, 5, 6 ],
        [ 7, 8, 9 ]
    ]
};
<table data-bind="table: { header: header, data: data }"></table>

This example uses rows and columns definition arrays and a data object.

var vm = {
    columns: [ 'x', 'y', 'z' ],
    rows: [ 'a', 'b', 'c' ],
    data: {
        a: { x: 1, y: 2, z: 3 },
        b: { x: 4, y: 5, z: 6 },
        c: { x: 7, y: 8, z: 9 }
    }
};
<table data-bind="table: { header: columns, rows: rows, columns: columns, data: data }"></table>

This example uses header and dataItem options to define the values for those items.

var vm = {
    columns: [
        { heading: 'x', datavalue: 'col1' },
        { heading: 'y', datavalue: 'col2' },
        { heading: 'x', datavalue: 'col3' } ]
    data: [
        { col1: 1, col2: 2, col3: 3 },
        { col1: 4, col2: 5, col3: 6 },
        { col1: 7, col2: 8, col3: 9 }
    ]
};
<table data-bind="table: { columns: columns, data: data, header: 'heading', dataItem: 'datavalue' }"></table>

This example uses header and dataItem functions to define the values (uses same view model as above).

<table data-bind="table: { columns: columns,
                           data: data,
                           header: function(col) { return col.heading },
                           dataItem: function(row, col, data) { return data[row][col.datavalue] } }">
</table>

This example uses a dataItem function to output a multiplication table (up to five).

<table data-bind="table: { columns: 5, rows: 5, dataItem: function(row, col) { return (row+1) * (col+1) } }"></table>

Parameters

The table binding expects a single parameter of a two-dimensional array to output. It also accepts an object literal with the following properties:

Any of the above parameters can be an observable and will cause the table to be regenerated if updated. For the purpose of making the binding faster, the entries in data, rows, columns, or headers cannot be observables. The actual data items, though, can be observable, and if updated, will update only the corresponding table cell’s contents.

How this binding works

The table binding uses a very fast method of table generation. 1) It first generates the table HTML as a string. 2) It then parses the string into DOM elements using innerHTML on a detached node. 3) Finally, it inserts the table contents into the table element in the document.

Additional interfaces

License and Contact

License: MIT (http://www.opensource.org/licenses/mit-license.php)

Michael Best<br> https://github.com/mbest<br> mbest@dasya.com