Created
May 7, 2012 19:01
-
-
Save randyburden/2629702 to your computer and use it in GitHub Desktop.
Fluent Table Cells - An easily extendible fluent interface for programmatically creating table cells in JavaScript.
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
/*! | |
* Fluent Table Cells - An easily extendible fluent interface for programmatically creating table cells in JavaScript. | |
* Author: Randy Burden | |
* http://www.randyburden.com | |
* Open Source Url: https://gist.github.com/2629702 | |
* | |
* Example usage: | |
var row = '<tr>'; | |
row += Cell() | |
.Id( 'SomeId' ) | |
.Value( 'Some text' ) | |
.Title( 'A title for this cell's content ' ); | |
row += Cell() | |
.Value( 'Some more text' ) | |
.Title ( 'Another title' ) | |
.Attribute( 'a_custom_attribute', 'the custom attributes value' ) | |
.Class( 'aClassName' ); | |
row += '</tr>'; | |
$( '#SomeTableName > tbody' ).html( row ); | |
*/ | |
var Cell = function () { | |
this.id = ''; | |
this.value = ''; | |
this.title = ''; | |
this.className = ''; | |
this.customAttribute = ''; | |
this.Text = function () { | |
return '<td id="' + this.id + '" title="' + this.title + '" class="' + this.className + '"' + this.customAttribute + '>' + this.value + '</td>'; | |
}; | |
this.Id = function ( value ) { | |
this.id = value; | |
return this; | |
}; | |
this.Value = function ( value ) { | |
this.value = value; | |
return this; | |
}; | |
this.Title = function ( title ) { | |
this.title = title; | |
return this; | |
}; | |
this.Class = function ( className ) { | |
this.className = this.className + ' ' + className; | |
return this; | |
}; | |
this.Attribute = function ( attributeName, value ) { | |
var newAttribute = attributeName + '="' + value + '"'; | |
this.customAttribute = this.customAttribute + ' ' + newAttribute; | |
return this; | |
}; | |
// toString override | |
this.toString = function () { | |
return this.Text(); | |
}; | |
if ( this instanceof Cell ) { | |
return this.Cell; | |
} | |
else { | |
return new Cell(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment