Last active
December 10, 2015 02:48
-
-
Save bencevans/4369782 to your computer and use it in GitHub Desktop.
JS Table DOM Helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var jstable = function(DOMTable, data, options) { | |
return new JSTable(DOMTable, data, options); | |
}; | |
var JSTable = function() { | |
this.element = DOMTable; | |
this.data = data || []; | |
this.options = options || {}; | |
if(typeof this.options.noDataMessage == 'undefined') | |
this.options.noDataMessage = null; | |
return this; | |
}; | |
JSTable.prototype.render = function() { | |
this.element.innerHTML = null; | |
for (var i = 0; i < this.data.length; i++) { | |
this.element.innerHTML += '</tr><td>' + data.join('</td><td>') + '</td></tr>'; | |
} | |
if(this.data.length === 0) { | |
this.element.innerHTML = this.options.noDataMessage; | |
} | |
}; | |
JSTable.prototype.push = function(data) { | |
this.data.push(data); | |
if(this.data.length === 1) { | |
this.element.innerHTML = ''; | |
} | |
this.element.innerHTML += '</tr><td>' + data.join('</td><td>') + '</td></tr>'; | |
}; | |
JSTable.prototype.pop = function() { | |
var item = this.data.pop(); | |
this.render(); | |
return item; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment