Skip to content

Commit fa33b62

Browse files
author
Fred Wu
committed
filter on date and boolean columns, data viewer UI changes
1 parent c0ec3e0 commit fa33b62

3 files changed

Lines changed: 50 additions & 36 deletions

File tree

R/session/vsc.R

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,16 @@ get_column_def <- function(name, field, value) {
7878
)
7979
if (is.numeric(value)) {
8080
type <- "numericColumn"
81+
filter <- "agNumberColumnFilter"
8182
if (is.null(attr(value, "class"))) {
8283
filter <- "agNumberColumnFilter"
8384
}
8485
} else if (inherits(value, "Date")) {
8586
type <- "dateColumn"
8687
filter <- "agDateColumnFilter"
88+
} else if (is.logical(value)) {
89+
type <- "booleanColumn"
90+
filter <- "agNumberColumnFilter"
8791
} else {
8892
type <- "textColumn"
8993
filter <- "agTextColumnFilter"
@@ -130,15 +134,18 @@ dataview_table <- function(data, start = 0, end = NULL, sortModel = NULL, filter
130134
fd <- filterModel[[fld]]
131135
col_name <- field_map[[fld]]
132136

133-
if (!is.null(fd$type) && !is.null(fd$filter)) {
137+
if ((!is.null(fd$type) && !is.null(fd$filter))) {
138+
134139
op <- fd$type
135-
raw <- fd$filter
140+
raw <- if (fd$filterType == "date") fd$dateFrom else fd$filter
136141

137142
# quote or coerce the filter literal
138143
lit <- if (inherits(data[[col_name]], "Date")) {
139144
sprintf('as.Date("%s")', raw)
140145
} else if (is.numeric(data[[col_name]])) {
141146
as.numeric(raw)
147+
} else if (is.logical(data[[col_name]])) {
148+
as.logical(raw)
142149
} else {
143150
sprintf('"%s"', gsub('"', '\\\\"', raw))
144151
}
@@ -159,8 +166,8 @@ dataview_table <- function(data, start = 0, end = NULL, sortModel = NULL, filter
159166
blank = sprintf('is.na(get("%s")) | get("%s") == ""', col_name, col_name),
160167
notBlank = sprintf('!is.na(get("%s")) & get("%s") != ""', col_name, col_name),
161168
inRange = {
162-
hi <- if (inherits(data[[col_name]], "Date")) {
163-
sprintf('as.Date("%s")', fd$filterTo)
169+
hi <- if (inherits(data[[col_name]], "Date") && !is.null(fd$dateTo)) {
170+
sprintf('as.Date("%s")', fd$dateTo)
164171
} else {
165172
as.numeric(fd$filterTo)
166173
}
@@ -206,9 +213,6 @@ dataview_table <- function(data, start = 0, end = NULL, sortModel = NULL, filter
206213
class(rows) <- "data.frame"
207214
attr(rows, "row.names") <- .set_row_names(nrow(rows))
208215

209-
rows <- jsonlite::fromJSON(
210-
jsonlite::toJSON(rows, dataframe = "rows", na = "null", auto_unbox = TRUE)
211-
)
212216
columns <- .mapply(
213217
get_column_def,
214218
list(.colnames, fields, rows),

package-lock.json

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/session.ts

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,11 @@ export async function getTableHtml(webview: Webview, file: string): Promise<stri
621621
}
622622
}
623623
};
624+
const booleanFilterParams = {
625+
filterOptions: ['equals'],
626+
defaultOption: 'equals',
627+
filterPlaceholder: '1=TRUE, 0=FALSE...'
628+
};
624629
const data = ${String(content)};
625630
const displayDataSource = {
626631
rowCount: undefined,
@@ -665,6 +670,23 @@ export async function getTableHtml(webview: Webview, file: string): Promise<stri
665670
};
666671
667672
const columnDefs = data.columns.map(col => {
673+
if (col.type === "booleanColumn") {
674+
return {
675+
...col,
676+
valueFormatter: params =>
677+
params.value === true
678+
? 'TRUE'
679+
: params.value === false
680+
? 'FALSE'
681+
: ''
682+
};
683+
} else if (col.type === "dateColumn") {
684+
return {
685+
...col,
686+
width: 200
687+
};
688+
}
689+
668690
if (col.field === "x2") {
669691
return {
670692
...col,
@@ -676,15 +698,18 @@ export async function getTableHtml(webview: Webview, file: string): Promise<stri
676698
...col,
677699
sortable: false,
678700
filter: false,
701+
lockPosition: 'left',
679702
suppressHeaderMenuButton: true,
680-
width: 60,
703+
width: 150,
681704
headerValueGetter: () => {
682705
const a = displayDataSource._TotalRows || 0;
683706
const b = displayDataSource._TotalUnfiltered || 0;
684707
return '(' + a + '/' + b + ')';
685708
}
686709
};
687710
}
711+
712+
688713
return col;
689714
});
690715
@@ -693,9 +718,11 @@ export async function getTableHtml(webview: Webview, file: string): Promise<stri
693718
sortable: true,
694719
resizable: true,
695720
filter: true,
696-
width: 100,
697-
minWidth: 80,
721+
width: 150,
722+
minWidth: 100,
698723
floatingFilter: true,
724+
suppressHeaderMenuButton: true,
725+
lockPinned: true,
699726
filterParams: {
700727
buttons: ['apply', 'reset'],
701728
closeOnApply: true,
@@ -767,37 +794,18 @@ export async function getTableHtml(webview: Webview, file: string): Promise<stri
767794
768795
document.addEventListener('DOMContentLoaded', () => {
769796
gridOptions.columnDefs.forEach(function(column) {
770-
if (column.filter === 'agDateColumnFilter') {
797+
if (column.type === 'dateColumn') {
771798
column.filterParams = dateFilterParams;
772-
}
773-
else if (column.filter === 'agNumberColumnFilter') {
774-
column.filterParams = {
775-
filterOptions: [
776-
'equals', 'notEqual',
777-
'lessThan', 'lessThanOrEqual',
778-
'greaterThan', 'greaterThanOrEqual',
779-
'inRange', 'blank', 'notBlank'
780-
],
781-
defaultOption: 'equals'
782-
};
783-
}
784-
else if (column.filter === 'agTextColumnFilter') {
785-
column.filterParams = {
786-
filterOptions: [
787-
'equals', 'notEqual', 'contains', 'notContains',
788-
'startsWith', 'endsWith', 'blank', 'notBlank', 'regexp'
789-
],
790-
defaultOption: 'contains'
791-
};
792-
}
799+
}
800+
else if (column.type == 'booleanColumn') {
801+
column.filterParams = booleanFilterParams;
802+
}
793803
});
794804
795805
const gridDiv = document.querySelector('#myGrid');
796-
797806
const gridApi = agGrid.createGrid(gridDiv, gridOptions);
798-
799-
displayDataSource.api = gridApi;
800-
807+
808+
displayDataSource.api = gridApi;
801809
gridApi.setGridOption('datasource', displayDataSource);
802810
803811
});

0 commit comments

Comments
 (0)