Blame assets/js/csv_to_html_table.js

401d07
var CsvToHtmlTable = CsvToHtmlTable || {};
401d07
401d07
CsvToHtmlTable = {
401d07
    init: function (options) {
401d07
        options = options || {};
401d07
        var csv_path = options.csv_path || "";
401d07
        var el = options.element || "table-container";
401d07
        var allow_download = options.allow_download || false;
401d07
        var csv_options = options.csv_options || {};
401d07
        var datatables_options = options.datatables_options || {};
401d07
        var custom_formatting = options.custom_formatting || [];
401d07
        var customTemplates = {};
401d07
        $.each(custom_formatting, function (i, v) {
401d07
            var colIdx = v[0];
401d07
            var func = v[1];
401d07
            customTemplates[colIdx] = func;
401d07
        });
401d07
401d07
        var $table = $("
");
401d07
        var $containerElement = $("#" + el);
401d07
        $containerElement.empty().append($table);
401d07
401d07
        $.when($.get(csv_path)).then(
401d07
            function (data) {
401d07
                var csvData = $.csv.toArrays(data, csv_options);
401d07
                var $tableHead = $("");
401d07
                var csvHeaderRow = csvData[0];
401d07
                var $tableHeadRow = $("");
401d07
                for (var headerIdx = 0; headerIdx < csvHeaderRow.length; headerIdx++) {
401d07
                  if ( headerIdx != 5 ) {
401d07
                    $tableHeadRow.append($("").text(csvHeaderRow[headerIdx]));
401d07
                  }
401d07
                }
401d07
                $tableHead.append($tableHeadRow);
401d07
401d07
                $table.append($tableHead);
401d07
                var $tableBody = $("");
401d07
401d07
                for (var rowIdx = 1; rowIdx < csvData.length; rowIdx++) {
401d07
                    var $tableBodyRow = $("");
401d07
                    for (var colIdx = 0; colIdx < csvData[rowIdx].length; colIdx++) {
401d07
                      if ( colIdx != 5 ) {
401d07
                        var $tableBodyRowTd = $("");
401d07
                        var cellTemplateFunc = customTemplates[colIdx];
401d07
                        if (cellTemplateFunc) {
401d07
                            $tableBodyRowTd.html(cellTemplateFunc(csvData[rowIdx][colIdx]));
401d07
                        } else {
401d07
                            $tableBodyRowTd.text(csvData[rowIdx][colIdx]);
401d07
                        }
401d07
                        $tableBodyRow.append($tableBodyRowTd);
401d07
                        $tableBody.append($tableBodyRow);
401d07
                      }
401d07
                    }
401d07
                }
401d07
                $table.append($tableBody);
401d07
401d07
                $table.DataTable(datatables_options);
401d07
401d07
                if (allow_download) {
401d07
                    $containerElement.append("

Download as CSV

");
401d07
                }
401d07
            });
401d07
    }
401d07
};