Dynamic Table v.2 (advanced)
HTML
<table id="data-table"></table>
<select class="montadoras" >
<option value="clear"></option> <!-- opção 1 -->
<option value="data1">FIAT</option> <!-- opção 2 -->
<option value="data2">GM</option> <!-- opção 3 -->
</select>
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;
}
/**/
/* Style The Dropdown Button */
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
/* The container <div> - needed to position the dropdown content */
.dropdown {
position: relative;
display: inline-block;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover .dropbtn {
background-color: #3e8e41;
}
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;
...