Skip to content

Commit

Permalink
Add sorting option
Browse files Browse the repository at this point in the history
  • Loading branch information
guerler committed Oct 20, 2023
1 parent 7a45261 commit bb54ead
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 12 deletions.
46 changes: 40 additions & 6 deletions client/src/components/Grid/GridList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ const totalRows = ref(0);
// loading indicator
const loading = ref(true);
// search term
const searchTerm = ref("");
// current grid configuration
const gridConfig = computed(() => {
return registry[props.id];
Expand All @@ -54,13 +57,27 @@ const gridConfig = computed(() => {
// check if loading has completed and data rows are available
const isAvailable = computed(() => !loading.value && totalRows.value > 0);
// sort references
const sortBy = ref(gridConfig.value ? gridConfig.value.sortBy : "");
const sortDesc = ref(gridConfig.value ? gridConfig.value.sortDesc : false);
/**
* Request grid data
*/
async function getGridData() {
if (gridConfig.value) {
try {
const response = await axios.get(withPrefix(gridConfig.value.getUrl(currentPage.value, props.perPage, "")));
const response = await axios.get(
withPrefix(
gridConfig.value.getUrl(
currentPage.value,
props.perPage,
sortBy.value,
sortDesc.value,
searchTerm.value
)
)
);
if (response.headers.total_matches) {
totalRows.value = parseInt(response.headers.total_matches);
}
Expand All @@ -77,7 +94,7 @@ async function getGridData() {
/**
* Execute grid operation and display message if available
*/
async function executeOperation(operation: Operation, rowData: RowData) {
async function onOperation(operation: Operation, rowData: RowData) {
const response = await operation.handler(rowData, router);
if (response) {
await getGridData();
Expand All @@ -86,6 +103,14 @@ async function executeOperation(operation: Operation, rowData: RowData) {
}
}
function onSort(sortKey: string) {
if (sortBy.value !== sortKey) {
sortBy.value = sortKey;
} else {
sortDesc.value = !sortDesc.value;
}
}
/**
* Process tag inputs
*/
Expand All @@ -106,7 +131,7 @@ onMounted(() => {
/**
* Load current page
*/
watch(currentPage, () => getGridData());
watch([currentPage, sortDesc, sortBy], () => getGridData());
/**
* Operation message timeout handler
Expand All @@ -131,8 +156,17 @@ watch(operationMessage, () => {
<BAlert v-else-if="!isAvailable" v-localize variant="info" show>No entries found.</BAlert>
<table v-else class="grid-table">
<thead>
<th v-for="(fieldEntry, fieldIndex) in gridConfig.fields" :key="fieldIndex" class="px-2">
{{ fieldEntry.title || fieldEntry.key }}
<th v-for="(fieldEntry, fieldIndex) in gridConfig.fields" :key="fieldIndex" class="text-nowrap px-2">
<span v-if="gridConfig.sortKeys.includes(fieldEntry.key)">
<b-link @click="onSort(fieldEntry.key)">
<span>{{ fieldEntry.title || fieldEntry.key }}</span>
<span v-if="sortBy === fieldEntry.key">
<icon v-if="sortDesc" icon="caret-up" />
<icon v-else icon="caret-down" />
</span>
</b-link>
</span>
<span v-else>{{ fieldEntry.title || fieldEntry.key }}</span>
</th>
</thead>
<tr v-for="(rowData, rowIndex) in gridData" :key="rowIndex" :class="{ 'grid-dark-row': rowIndex % 2 }">
Expand All @@ -141,7 +175,7 @@ watch(operationMessage, () => {
v-if="fieldEntry.type == 'operations'"
:title="rowData.title"
:operations="fieldEntry.operations"
@execute="executeOperation($event, rowData)" />
@execute="onOperation($event, rowData)" />
<GridText v-else-if="fieldEntry.type == 'text'" :text="rowData[fieldEntry.key]" />
<GridSharing
v-else-if="fieldEntry.type == 'sharing'"
Expand Down
8 changes: 6 additions & 2 deletions client/src/components/Grid/configs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ type Field = FieldKey | FieldOperations;
// TODO: type FieldType = "date" | "operations" | "sharing" | "tags" | "text" | undefined;

interface FieldKey {
key?: string;
key: string;
type: string;
handler?: FieldKeyHandler;
}
Expand All @@ -16,15 +16,19 @@ interface OperationHandlerMessage {
type OperationHandlerReturn = OperationHandlerMessage | void;

export interface Config {
getUrl: (currentPage: number, perPage: number, search: string) => string;
getUrl: (currentPage: number, perPage: number, sortBy: string, sortDesc: boolean, search: string) => string;
resource: string;
item: string;
plural: string;
title: string;
fields: Array<Field>;
sortBy: string;
sortKeys: Array<string>;
sortDesc: boolean;
}

export interface FieldOperations {
key: string;
title: string;
operations: Array<Operation>;
}
Expand Down
10 changes: 7 additions & 3 deletions client/src/components/Grid/configs/visualizations.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ import { withPrefix } from "@/utils/redirect";
import { errorMessageAsString, rethrowSimple } from "@/utils/simple-error";

export const VisualizationsGrid = {
getUrl: (currentPage, perPage, searchTerm) => {
getUrl: (currentPage, perPage, sortBy, sortDesc, searchTerm) => {
const offset = perPage * (currentPage - 1);
return `/api/visualizations/detailed?limit=${perPage}&offset=${offset}`;
return `/api/visualizations/detailed?limit=${perPage}&offset=${offset}&sort_by=${sortBy}&sort_desc=${sortDesc}`;
},
resource: "visualizations",
item: "visualization",
plural: "Visualizations",
title: "Saved Visualizations",
sortBy: "update_time",
sortDesc: true,
sortKeys: ["create_time", "title", "update_time"],
fields: [
{
title: "Title",
key: "title",
type: "operations",
operations: [
{
Expand Down Expand Up @@ -107,7 +111,7 @@ export const VisualizationsGrid = {
},
{
key: "update_time",
title: "Last updated",
title: "Updated",
type: "date",
},
{
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/schema/visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
)


VisualizationSortByEnum = Literal["update_time", "title", "username"]
VisualizationSortByEnum = Literal["create_time", "title", "update_time", "username"]


class VisualizationIndexQueryPayload(Model):
Expand Down

0 comments on commit bb54ead

Please sign in to comment.