Skip to content

Column filters #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion js/csv_to_html_table.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var CsvToHtmlTable = CsvToHtmlTable || {};
var CsvToHtmlTable_DataTable = {};

CsvToHtmlTable = {
init: function (options) {
Expand All @@ -8,6 +9,7 @@ CsvToHtmlTable = {
var allow_download = options.allow_download || false;
var csv_options = options.csv_options || {};
var datatables_options = options.datatables_options || {};
var column_filters = options.column_filters || false;
var custom_formatting = options.custom_formatting || [];
var customTemplates = {};
$.each(custom_formatting, function (i, v) {
Expand All @@ -29,6 +31,22 @@ CsvToHtmlTable = {
for (var headerIdx = 0; headerIdx < csvHeaderRow.length; headerIdx++) {
$tableHeadRow.append($("<th></th>").text(csvHeaderRow[headerIdx]));
}

if (column_filters) {
var $tableHeadRow_filter = $("<tr></tr>");
for (var headerIdx = 0; headerIdx < csvHeaderRow.length; headerIdx++) {
$tableHeadRow_filter.append($("<th></th>").html(
'<input type="text" id="col_f-' + headerIdx + '" placeholder="filter column" />'));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code uses Bootstrap, so we should add the form-control and form-control-sm classes. Currently the default styles don't resize to fit the screen width and have a different aesthetic.
Screen Shot 2023-03-02 at 1 55 31 PM

$tableHeadRow_filter[0].childNodes[headerIdx].childNodes[0].onchange = function(ee) {
var i = parseInt(ee.target.id.substr(6));
CsvToHtmlTable_DataTable.column(i).search(this.value).draw();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this is a new vaiable to keep track of the DataTable state. Is it necessary for this code to work? It seems a bit awkward to only use it for this feature. Maybe we should keep track of the DataTable this way generally?

}
$tableHeadRow_filter[0].childNodes[headerIdx].childNodes[0].onkeyup =
$tableHeadRow_filter[0].childNodes[headerIdx].childNodes[0].onchange
}
$tableHead.append($tableHeadRow_filter);
}

$tableHead.append($tableHeadRow);

$table.append($tableHead);
Expand All @@ -50,7 +68,7 @@ CsvToHtmlTable = {
}
$table.append($tableBody);

$table.DataTable(datatables_options);
CsvToHtmlTable_DataTable = $table.DataTable(datatables_options);

if (allow_download) {
$containerElement.append("<p><a class='btn btn-info' href='" + csv_path + "'><i class='glyphicon glyphicon-download'></i> Download as CSV</a></p>");
Expand Down