Dynamic Table v.2 (advanced)
by onury
HTML
<table id="data-table"></table>
<br />
<button id="btn-load">Load List 1</button>
<button id="btn-update">Load List 2</button>
<button id="btn-append">Append List 1</button>
<button id="btn-clear">Clear List</button>
CSS
html, body {
padding: 5px;
}
#data-table {
font-family: calibri, arial;
width: 400px;
}
#data-table thead td {
background-color: #8CCCF1;
}
#data-table td {
border: 1px solid #466E85;
width: 136px;
padding: 7px;
}
JavaScript
var dynamicTable = (function() {
var _tableId, _table,
_fields, _headers,
_defaultText;
/** Builds the row with columns from the specified names.
* If the item parameter is specified, the memebers of the names array will be used as property names of the item; otherwise they will be directly parsed as text.
*/
function _buildRowColumns(names, item) {
var row = '<tr>';
if (names && names.length > 0)
{
$.each(names, function(index, name) {
var c = item ? item[name+''] : name;
row += '<td>' + c + '</td>';
});
}
row += '</tr>';
return row;
}
/** Builds and sets the headers of the table. */
function _setHeaders() {
// if no headers specified, we will use the fields as headers.
_headers = (_headers == null || _headers.length < 1) ? _fields : _headers;
var h = _buildRowColumns(_headers);
if (_table.children('thead').length < 1) _table.prepend('<thead></thead>');
_table.children('thead').html(h);
}
function _setNoItemsInfo() {
if (_table.length < 1) return; //not configured.
var colspan = _headers != null && _headers.length > 0 ?
'colspan="' + _headers.length + '"' : '';
var content = '<tr class="no-items"><td ' + colspan + ' style="text-align:center">' +
_defaultText + '</td></tr>';
if (_table.children('tbody').length > 0)
_table.children('tbody').html(content);
else _table.append('<tbody>' + content + '</tbody>');
}
function _removeNoItemsInfo() {
var c = _table.children('tbody').children('tr');
if (c.length == 1 && c.hasClass('no-items')) _table.children('tbody').empty();
}
return {
/** Configres the dynamic table. */
config: function(tableId, fields, headers, defaultText) {
_tableId = tableId;
...