Created
December 21, 2010 16:48
-
-
Save mythz/750202 to your computer and use it in GitHub Desktop.
JavaScript createTableFn
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 createTableWithCaption = function (id, caption, tableStyle) | |
{ | |
var sb = "<div class='sd-info'><table id='" + id + "' class='itfa_sectionbody' style='" + tableStyle + "'>"; | |
if (caption) sb += "<caption>" + caption + "</caption>"; | |
return sb; | |
} | |
var createTableFn = function (id, fieldNames, caption, tableStyle, tfoot) | |
{ | |
var sb = createTableWithCaption(id, caption, tableStyle); | |
sb += "<thead><tr>"; | |
for (var i = 0, len = fieldNames.length; i < len; i++) | |
{ | |
sb += '<th class="c' + (i+1) + '">' + fieldNames[i] + '</th>'; | |
} | |
sb += "</tr></thead><tfoot>"; | |
if (tfoot) sb += evalToString(tfoot); | |
return sb + "</tfoot><tbody>"; | |
} | |
var createRowsFn = function (rows, fieldNames, filters) | |
{ | |
var sb = ""; | |
for (var i = 0; i < rows.length; i++) | |
{ | |
var row = rows[i]; | |
var className = i % 2 == 0 ? "r1" : "r2"; | |
sb += "<tr class='" + className + "' data-id='" + row["Id"] + "'>"; | |
for (var j = 0; j < fieldNames.length; j++) | |
{ | |
var fieldName = fieldNames[j]; | |
var fieldValue = row[fieldName]; | |
if (filters && typeof (filters[fieldName]) == 'function') | |
{ | |
fieldValue = filters[fieldName](fieldValue, row); | |
} | |
if (typeof fieldValue == "undefined") | |
{ | |
fieldValue = ""; | |
} | |
sb += '<td class="c' + (j+1) + '">' + fieldValue + '</td>'; | |
} | |
sb += "</tr>"; | |
} | |
return sb; | |
} | |
var createResultsFn = function (options) | |
{ | |
var fieldNames = [], fieldLabels = [], fields = options.fieldLabels, id = options.id || "wsResults"; | |
for (var fieldName in fields) | |
{ | |
fieldNames.push(fieldName); | |
fieldLabels.push(fields[fieldName]); | |
} | |
var tableStyle = options.show ? '' : 'display:none'; | |
var footer = options.footer || ""; | |
if ((!options.data || options.data.length == 0)) | |
{ | |
var tableHtml = options.showHeadersIfEmpty | |
? createTableFn(id, fieldLabels, options.caption, tableStyle, options.tfoot) | |
+ '<tr class="empty"><td colspan="' + fieldNames.length + '">' + options.emptyCaption + '</td></tr>' | |
: createTableWithCaption(id, options.emptyCaption, tableStyle) | |
return tableHtml + "</table>" + footer + "</div>"; | |
} | |
var sb = createTableFn(id, fieldLabels, options.caption, tableStyle, options.tfoot); | |
sb += createRowsFn(options.data, fieldNames, options.filters); | |
sb += "</tbody></table>" + footer + "</div>"; | |
return sb; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment