Skip to content

Commit 1c1b265

Browse files
authored
Add server-side pagination (#228)
1 parent d7e0fc6 commit 1c1b265

File tree

4 files changed

+21
-51
lines changed

4 files changed

+21
-51
lines changed

jbrowse/src/client/JBrowse/VariantSearch/components/ArrowPagination.tsx

Lines changed: 0 additions & 35 deletions
This file was deleted.

jbrowse/src/client/JBrowse/VariantSearch/components/VariantTableWidget.tsx

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import SearchIcon from '@mui/icons-material/Search';
1414
import React, { useEffect, useState, useMemo } from 'react';
1515
import { getConf } from '@jbrowse/core/configuration';
1616
import { AppBar, Box, Button, Dialog, Paper, Popover, Toolbar, Tooltip, Typography } from '@mui/material';
17-
import ArrowPagination from './ArrowPagination';
1817
import { FilterFormModal } from './FilterFormModal';
1918
import { getAdapter } from '@jbrowse/core/data_adapters/dataAdapterCache';
2019
import { NoAssemblyRegion } from '@jbrowse/core/util/types';
@@ -41,8 +40,6 @@ const VariantTableWidget = observer(props => {
4140
const { assemblyNames, assemblyManager } = session
4241
const { view } = session
4342

44-
const currentOffset = parseInt(new URLSearchParams(window.location.search).get('offset') || '0');
45-
4643
// The code expects a proper GUID, yet the trackId is a string containing the GUID + filename
4744
const trackGUID = truncateToValidGUID(props.trackId)
4845

@@ -61,25 +58,30 @@ const VariantTableWidget = observer(props => {
6158

6259
return obj
6360
}))
61+
setTotalHits(data.totalHits)
6462
setDataLoaded(true)
6563
}
6664

6765
function handleModalClose(widget) {
6866
session.hideWidget(widget)
6967
}
7068

71-
function handleQuery(passedFilters, pushToHistory) {
69+
function handleQuery(passedFilters, pushToHistory, pageQueryModel = pageSizeModel) {
70+
const { page = pageSizeModel.page, pageSize = pageSizeModel.pageSize } = pageQueryModel;
71+
7272
const encodedSearchString = createEncodedFilterString(passedFilters, false);
7373
const currentUrl = new URL(window.location.href);
7474
currentUrl.searchParams.set("searchString", encodedSearchString);
75+
currentUrl.searchParams.set("page", page.toString());
76+
currentUrl.searchParams.set("pageSize", pageSize.toString());
7577

7678
if (pushToHistory) {
7779
window.history.pushState(null, "", currentUrl.toString());
7880
}
7981

8082
setFilters(passedFilters);
8183
setDataLoaded(false)
82-
fetchLuceneQuery(passedFilters, sessionId, trackGUID, currentOffset, (json)=>{handleSearch(json)}, (error) => {setDataLoaded(true);setError(error)});
84+
fetchLuceneQuery(passedFilters, sessionId, trackGUID, page, pageSize, (json)=>{handleSearch(json)}, (error) => {setDataLoaded(true); setError(error)});
8385
}
8486

8587
const TableCellWithPopover = (props: { value: any }) => {
@@ -220,6 +222,7 @@ const VariantTableWidget = observer(props => {
220222

221223
const [filterModalOpen, setFilterModalOpen] = useState(false);
222224
const [filters, setFilters] = useState([]);
225+
const [totalHits, setTotalHits] = useState(0);
223226
const [fieldTypeInfo, setFieldTypeInfo] = useState<FieldModel[]>([]);
224227
const [allowedGroupNames, setAllowedGroupNames] = useState<string[]>([]);
225228
const [promotedFilters, setPromotedFilters] = useState<Map<string, Filter[]>>(null);
@@ -233,7 +236,10 @@ const VariantTableWidget = observer(props => {
233236
// False until initial data load or an error:
234237
const [dataLoaded, setDataLoaded] = useState(!parsedLocString)
235238

236-
const [pageSizeModel, setPageSizeModel] = React.useState<GridPaginationModel>({ page: 0, pageSize: 50 });
239+
const urlParams = new URLSearchParams(window.location.search);
240+
const page = parseInt(urlParams.get('page') || '0');
241+
const pageSize = parseInt(urlParams.get('pageSize') || '50');
242+
const [pageSizeModel, setPageSizeModel] = React.useState<GridPaginationModel>({ page, pageSize });
237243

238244
// API call to retrieve the requested features.
239245
useEffect(() => {
@@ -386,7 +392,12 @@ const VariantTableWidget = observer(props => {
386392
columnVisibilityModel={columnVisibilityModel}
387393
pageSizeOptions={[10,25,50,100]}
388394
paginationModel={ pageSizeModel }
389-
onPaginationModelChange= {(newModel) => setPageSizeModel(newModel)}
395+
rowCount={ totalHits }
396+
paginationMode="server"
397+
onPaginationModelChange = {(newModel) => {
398+
setPageSizeModel(newModel)
399+
handleQuery(filters, true, newModel)
400+
}}
390401
onColumnVisibilityModelChange={(model) => {
391402
setColumnVisibilityModel(model)
392403
}}
@@ -470,13 +481,6 @@ const VariantTableWidget = observer(props => {
470481
);
471482
})}
472483
</div>
473-
474-
<div style={{ marginLeft: "auto" }}>
475-
<ArrowPagination
476-
offset={currentOffset}
477-
onOffsetChange={handleOffsetChange}
478-
/>
479-
</div>
480484
</div>
481485

482486
{filterModal}

jbrowse/src/client/JBrowse/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ function generateLuceneString(field, operator, value) {
414414
return luceneQueryString;
415415
}
416416

417-
export async function fetchLuceneQuery(filters, sessionId, trackGUID, offset, successCallback, failureCallback) {
417+
export async function fetchLuceneQuery(filters, sessionId, trackGUID, offset, pageSize, successCallback, failureCallback) {
418418
if (!offset) {
419419
offset = 0
420420
}
@@ -444,7 +444,7 @@ export async function fetchLuceneQuery(filters, sessionId, trackGUID, offset, su
444444
failure: function(res) {
445445
failureCallback("There was an error: " + res.status + "\n Status Body: " + res.responseText + "\n Session ID:" + sessionId)
446446
},
447-
params: {"searchString": createEncodedFilterString(filters, true), "sessionId": sessionId, "trackId": trackGUID, "offset": offset},
447+
params: {"searchString": createEncodedFilterString(filters, true), "sessionId": sessionId, "trackId": trackGUID, "offset": offset, "pageSize": pageSize},
448448
});
449449
}
450450

jbrowse/src/org/labkey/jbrowse/JBrowseLuceneSearch.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ else if(numericQueryParserFields.contains(fieldName))
261261
}
262262

263263
results.put("data", data);
264+
results.put("totalHits", topDocs.totalHits.value);
264265

265266
//TODO: we should probably stream this
266267
return results;

0 commit comments

Comments
 (0)