Skip to content

Commit

Permalink
Instead of changing CSV data value with its formatted value now we ma…
Browse files Browse the repository at this point in the history
…king formatting just in time of rendering so original csvData variable is unchanged.

 To make it easier to determine template function corresponding to cell index we convert array of tuples into usual JS object (i.e. map/dictionary)
  • Loading branch information
sponomarev committed Apr 20, 2018
1 parent 34393bb commit 6076437
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions js/csv_to_html_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ CsvToHtmlTable = {
var csv_options = options.csv_options || {};
var datatables_options = options.datatables_options || {};
var custom_formatting = options.custom_formatting || [];
var customTemplates = {};
$.each(custom_formatting, function (i, v) {
var colIdx = v[0];
var func = v[1];
customTemplates[colIdx] = func;
});

var $table = $("<table class='table table-striped table-condensed' id='" + el + "-table'></table>");
var $containerElement = $("#" + el);
$containerElement.empty().append($table);
Expand All @@ -29,18 +36,13 @@ CsvToHtmlTable = {

for (var rowIdx = 1; rowIdx < csvData.length; rowIdx++) {
var row_html = "<tr>";

//takes in an array of column index and function pairs
if (custom_formatting != []) {
$.each(custom_formatting, function (i, v) {
var colIdx = v[0];
var func = v[1];
csvData[rowIdx][colIdx] = func(csvData[rowIdx][colIdx]);
})
}

for (var colIdx = 0; colIdx < csvData[rowIdx].length; colIdx++) {
row_html += "<td>" + csvData[rowIdx][colIdx] + "</td>";
var cellTemplateFunc = customTemplates[colIdx];
if (cellTemplateFunc) {
row_html += "<td>" + cellTemplateFunc(csvData[rowIdx][colIdx]) + "</td>";
} else {
row_html += "<td>" + csvData[rowIdx][colIdx] + "</td>";
}
}

row_html += "</tr>";
Expand Down

0 comments on commit 6076437

Please sign in to comment.