Skip to content

Commit 8323c53

Browse files
authored
Merge pull request #53 from devforth/fix-virtual-columns-sorting
Fix virtual columns sorting
2 parents 40c5b90 + bc350d0 commit 8323c53

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

adminforth/modules/configValidator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ export default class ConfigValidator implements IConfigValidator {
326326

327327
col.label = col.label || guessLabelFromName(col.name);
328328
//define default sortable
329-
if (!Object.keys(col).includes('sortable')) { col.sortable = true; }
329+
if (!Object.keys(col).includes('sortable')) { col.sortable = !col.virtual; }
330330
if (col.showIn && !Array.isArray(col.showIn)) {
331331
errors.push(`Resource "${res.resourceId}" column "${col.name}" showIn must be an array`);
332332
}

adminforth/modules/restApi.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
AfterDataSourceResponseFunction,
99
BeforeDataSourceRequestFunction,
1010
IAdminForthRestAPI,
11+
IAdminForthSort,
1112
HttpExtra,
1213
} from "../types/Back.js";
1314

@@ -531,7 +532,6 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
531532
method: 'POST',
532533
path: '/get_resource_data',
533534
handler: async ({ body, adminUser, headers, query, cookies, requestUrl }) => {
534-
535535
const { resourceId, source } = body;
536536
if (['show', 'list', 'edit'].includes(source) === false) {
537537
return { error: 'Invalid source, should be list or show' };
@@ -546,15 +546,15 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
546546
if (!resource) {
547547
return { error: `Resource ${resourceId} not found` };
548548
}
549-
549+
550550
const meta = { requestBody: body, pk: undefined };
551551
if (source === 'edit' || source === 'show') {
552552
meta.pk = body.filters.find((f) => f.field === resource.columns.find((col) => col.primaryKey).name)?.value;
553553
}
554554

555555
const { allowedActions } = await interpretResource(
556-
adminUser,
557-
resource,
556+
adminUser,
557+
resource,
558558
meta,
559559
{
560560
'show': ActionCheckSource.ShowRequest,
@@ -581,10 +581,10 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
581581
}[source];
582582

583583
for (const hook of listify(resource.hooks?.[hookSource]?.beforeDatasourceRequest)) {
584-
const resp = await hook({
585-
resource,
586-
query: body,
587-
adminUser,
584+
const resp = await hook({
585+
resource,
586+
query: body,
587+
adminUser,
588588
extra: {
589589
body, query, headers, cookies, requestUrl
590590
},
@@ -600,6 +600,12 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
600600
}
601601
const { limit, offset, filters, sort } = body;
602602

603+
// remove virtual fields from sort if still presented after
604+
// beforeDatasourceRequest hook
605+
const sortFiltered = sort.filter((sortItem: IAdminForthSort) => {
606+
return !resource.columns.find((col) => col.name === sortItem.field && col.virtual);
607+
});
608+
603609
for (const filter of (filters || [])) {
604610
if (!Object.values(AdminForthFilterOperators).includes(filter.operator)) {
605611
throw new Error(`Operator '${filter.operator}' is not allowed`);
@@ -614,18 +620,17 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
614620
throw new Error(`Value for operator '${filter.operator}' should be an array`);
615621
}
616622
}
617-
618-
619623
}
620624

621625
const data = await this.adminforth.connectors[resource.dataSource].getData({
622626
resource,
623627
limit,
624628
offset,
625629
filters,
626-
sort,
630+
sort: sortFiltered,
627631
getTotals: true,
628632
});
633+
629634
// for foreign keys, add references
630635
await Promise.all(
631636
resource.columns.filter((col) => col.foreignResource).map(async (col) => {
@@ -681,12 +686,11 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
681686

682687
// only after adminforth made all post processing, give user ability to edit it
683688
for (const hook of listify(resource.hooks?.[hookSource]?.afterDatasourceResponse)) {
684-
const resp = await hook({
685-
resource,
689+
const resp = await hook({
690+
resource,
686691
query: body,
687-
response:
688-
data.data,
689-
adminUser,
692+
response: data.data,
693+
adminUser,
690694
extra: {
691695
body, query, headers, cookies, requestUrl
692696
},
@@ -703,7 +707,7 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
703707
}
704708

705709
return {
706-
...data,
710+
...data,
707711
options: resource?.options,
708712
};
709713
},

adminforth/spa/src/components/ResourceListTable.vue

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,13 +332,10 @@ watch(() => sort.value, (newSort) => {
332332
});
333333
334334
watch(() => checkboxesInternal.value, (newCheckboxes) => {
335-
console.log('checkboxesInternal ch changed, emiting', newCheckboxes)
336-
337335
emits('update:checkboxes', newCheckboxes);
338336
});
339337
340338
watch(() => props.checkboxes, (newCheckboxes) => {
341-
console.log('Props ch changed', newCheckboxes)
342339
checkboxesInternal.value = newCheckboxes;
343340
});
344341
@@ -351,7 +348,6 @@ watch(() => props.page, (newPage) => {
351348
});
352349
353350
function addToCheckedValues(id) {
354-
console.log('checking', checkboxesInternal.value, 'id', id)
355351
if (checkboxesInternal.value.includes(id)) {
356352
checkboxesInternal.value = checkboxesInternal.value.filter((item) => item !== id);
357353
} else {

0 commit comments

Comments
 (0)