2013-03-06 13:38:40 -05:00
|
|
|
// requires jQuery, druidTable, dataTables
|
|
|
|
var oTable = [];
|
|
|
|
|
|
|
|
// flattens JSON from Druid and builds a table row per segment
|
|
|
|
function buildTable(data, el, dontDisplay, table, row) {
|
|
|
|
table = typeof table !== 'undefined' ? table : new DruidTable();
|
|
|
|
row = typeof row !== 'undefined' ? row : 0;
|
|
|
|
dontDisplay = typeof dontDisplay !== 'undefined' ? dontDisplay : [];
|
|
|
|
|
|
|
|
if (!Array.isArray(data) || data.length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oTable[el.attr('id')] != null) {
|
|
|
|
oTable[el.attr('id')].fnDestroy();
|
|
|
|
el.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse JSON
|
|
|
|
for (var item in data) {
|
2013-03-15 17:28:55 -04:00
|
|
|
setTable(data[item], el, dontDisplay, table, row, "");
|
2013-03-06 13:38:40 -05:00
|
|
|
row++;
|
|
|
|
}
|
|
|
|
|
|
|
|
table.toHTMLTable(el);
|
|
|
|
initDataTable(el);
|
|
|
|
}
|
|
|
|
|
2013-03-15 17:28:55 -04:00
|
|
|
function setTable(data, el, dontDisplay, table, row, fieldNamespace) {
|
2013-03-06 13:38:40 -05:00
|
|
|
for (var field in data) {
|
|
|
|
if (_.contains(dontDisplay, field)) {
|
|
|
|
// do nothing
|
|
|
|
} else if (Array.isArray(data[field])) {
|
2013-03-15 17:28:55 -04:00
|
|
|
table.setCell(row, fieldNamespace + field, JSON.stringify(data[field]));
|
2013-03-06 13:38:40 -05:00
|
|
|
} else if (!(data[field] instanceof Object)) {
|
2013-03-15 17:28:55 -04:00
|
|
|
table.setCell(row, fieldNamespace + field, data[field]);
|
2013-03-06 13:38:40 -05:00
|
|
|
} else {
|
2013-03-15 17:28:55 -04:00
|
|
|
setTable(data[field], el, dontDisplay, table, row, fieldNamespace + field + " ");
|
2013-03-06 13:38:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function initDataTable(el) {
|
|
|
|
// dataTable stuff (http://www.datatables.net/)
|
|
|
|
var asInitVals = [];
|
|
|
|
|
|
|
|
oTable[el.attr('id')] = el.dataTable({
|
|
|
|
"oLanguage": {
|
|
|
|
"sSearch": "Search all columns:"
|
|
|
|
},
|
|
|
|
"oSearch": {
|
|
|
|
"sSearch": "",
|
|
|
|
"bRegex": true
|
|
|
|
},
|
|
|
|
"sPaginationType": "full_numbers",
|
|
|
|
"bProcessing": true
|
|
|
|
});
|
|
|
|
|
|
|
|
$("thead input").keyup(function() {
|
|
|
|
var tbl = oTable[$(this).parents('table').attr('id')];
|
|
|
|
tbl.fnFilter(this.value, tbl.children("thead").find("input").index(this), true);
|
|
|
|
});
|
|
|
|
|
|
|
|
$("thead input").each(function(i) {
|
|
|
|
asInitVals[i] = this.value;
|
|
|
|
});
|
|
|
|
|
|
|
|
$("thead input").focus(function() {
|
|
|
|
if (this.className === "search_init" ) {
|
|
|
|
this.className = "";
|
|
|
|
this.value = "";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$("thead input").blur(function(i) {
|
|
|
|
if (this.value === "" ) {
|
|
|
|
this.className = "search_init";
|
|
|
|
this.value = asInitVals[$("thead input").index(this)];
|
|
|
|
}
|
|
|
|
});
|
2013-07-29 20:49:09 -04:00
|
|
|
}
|