Skip to content

Commit 3596e19

Browse files
committed
to fix dataview_table
1 parent 2e1076d commit 3596e19

2 files changed

Lines changed: 51 additions & 53 deletions

File tree

R/session/vsc.R

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -134,28 +134,14 @@ dataview_table <- function(data, start = 0, end = NULL, sortModel = NULL) {
134134
# ── SORT data before slicing ──
135135
if (!is.null(sortModel) && length(sortModel) > 0) {
136136

137-
sort <- sortModel[[1]]
138-
col_f <- sort$colId
139-
dir <- sort$sort
140-
real <- field_map[[col_f]]
141-
142-
# only sort if it's one of your real data columns
143-
if (!is.null(real) && real %in% .colnames[-1]) {
144-
145-
# attach rownames_ as a helper column
146-
data[, "__rownames__" := rownames_]
147-
148-
# sort in place: order=1L for asc, -1L for desc
149-
data.table::setorderv(
150-
data,
151-
cols = real,
152-
order = if (dir == "asc") 1L else -1L
153-
)
137+
cols <- vapply(sortModel, function(s) field_map[[s$colId]], FUN.VALUE = "")
138+
ords <- vapply(sortModel, function(s) if (s$sort == "asc") 1L else -1L, FUN.VALUE = integer(1))
139+
data[, "__rn__" := rownames_]
154140

155-
# pull the helper back out as rownames_
156-
rownames_ <- data[["__rownames__"]]
157-
data[, "__rownames__" := NULL]
158-
}
141+
data.table::setorderv(data, cols, order = ords)
142+
143+
rownames_ <- data[["__rn__"]]
144+
data[, "__rn__" := NULL]
159145
}
160146

161147
if (is.null(end)) end <- .nrow

src/session.ts

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ export async function showDataView(source: string, type: string, title: string,
437437
start,
438438
end,
439439
rows: rows as object[],
440-
totalRows: totalRows,
440+
lastRow: (totalRows <= (end ?? totalRows) ? totalRows : -1),
441441
requestId
442442
});
443443
} catch (error) {
@@ -600,6 +600,40 @@ export async function getTableHtml(webview: Webview, file: string): Promise<stri
600600
}
601601
};
602602
const data = ${String(content)};
603+
const displayDataSource = {
604+
rowCount: undefined,
605+
getRows(params) {
606+
const msg = {
607+
command: 'fetchRows',
608+
start: params.startRow,
609+
end: params.endRow,
610+
sortModel: params.sortModel,
611+
requestId: Math.random().toString(36).substr(2, 9) // Generate unique requestId
612+
};
613+
614+
const handler = event => {
615+
const m = event.data;
616+
if (m.command === 'fetchedRows' && m.requestId === msg.requestId) {
617+
console.log('Fetched rows:', m.rows);
618+
params.successCallback(m.rows, m.lastRow);
619+
window.removeEventListener('message', handler);
620+
}
621+
};
622+
window.addEventListener('message', handler);
623+
vscode.postMessage(msg);
624+
}
625+
};
626+
const colDefs = data.columns.map(col => {
627+
if (col.field === 'x1') {
628+
return {
629+
...col,
630+
sortable: false,
631+
filter: false,
632+
suppressMenu: true,
633+
};
634+
}
635+
return col;
636+
});
603637
const gridOptions = {
604638
defaultColDef: {
605639
sortable: true,
@@ -612,45 +646,23 @@ export async function getTableHtml(webview: Webview, file: string): Promise<stri
612646
closeOnApply: true
613647
}
614648
},
615-
columnDefs: data.columns,
616-
rowSelection: 'multiple',
649+
datasource: displayDataSource,
650+
getRowId: params => params.data.x1,
651+
columnDefs: colDefs,
652+
rowSelection: { mode: "multiRow", headerCheckbox: false },
617653
pagination: ${pageSize > 0 ? 'true' : 'false'},
618654
paginationPageSize: ${pageSize},
619655
enableCellTextSelection: true,
620656
ensureDomOrder: true,
621657
tooltipShowDelay: 100,
622658
onFirstDataRendered: onFirstDataRendered,
623659
rowModelType: 'infinite',
624-
getRowNodeId: rowData => rowData.x1,
625660
cacheBlockSize: 100,
626-
maxBlocksInCache: 0,
627-
datasource: {
628-
getRows: function(params) {
629-
const msg = {
630-
command: 'fetchRows',
631-
start: params.startRow,
632-
end: params.endRow,
633-
sortModel: params.sortModel,
634-
requestId: Math.random().toString(36).substr(2, 9) // Generate unique requestId
635-
};
636-
637-
const handler = event => {
638-
const m = event.data;
639-
if (
640-
m.command === 'fetchedRows' &&
641-
m.requestId === msg.requestId
642-
) {
643-
console.log('Fetched rows:', m.rows);
644-
params.successCallback(m.rows, m.totalRows);
645-
window.removeEventListener('message', handler);
646-
}
647-
};
648-
window.addEventListener('message', handler);
649-
vscode.postMessage(msg);
650-
651-
}
652-
}
653-
};
661+
maxBlocksInCache: 10,
662+
cacheOverflowSize: 2,
663+
maxConcurrentDatasourceRequests: 2,
664+
infiniteInitialRowCount: 1
665+
};
654666
655667
function onFirstDataRendered(params) {
656668
params.api.autoSizeAllColumns(false);

0 commit comments

Comments
 (0)