Skip to content

Commit f5c1e43

Browse files
committed
feat: add defaultFilters callback
1 parent 9d19f66 commit f5c1e43

File tree

5 files changed

+3148
-161
lines changed

5 files changed

+3148
-161
lines changed

custom/InlineList.vue

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ const filters = ref([]);
153153
const filtersShow = ref(false);
154154
const columnsMinMax = ref(null);
155155
156+
const defaultFilters = ref([]);
157+
156158
const listResourceRefColumn = computed(() => {
157159
if (!listResource.value) {
158160
return null;
@@ -202,6 +204,7 @@ const endFilters = computed(() => {
202204
203205
204206
return [
207+
...defaultFilters.value,
205208
...filters.value,
206209
{
207210
field: refColumn.name,
@@ -308,6 +311,21 @@ async function getList() {
308311
await nextTick();
309312
}
310313
314+
async function getDefaultFilters() {
315+
const data = await callAdminForthApi({
316+
path: `/plugin/${props.meta.pluginInstanceId}/get_default_filters`,
317+
method: 'POST',
318+
body: {
319+
record: props.record,
320+
},
321+
});
322+
if (data.ok) {
323+
defaultFilters.value = data.filters;
324+
} else {
325+
showErrorTost(data.error);
326+
}
327+
}
328+
311329
onMounted( async () => {
312330
loading.value = true;
313331
if (props.meta?.defaultSort && sort.value.length === 0) {
@@ -332,6 +350,9 @@ onMounted( async () => {
332350
}
333351
});
334352
loading.value = false;
353+
if (props.meta.defaultFiltersOn) {
354+
await getDefaultFilters();
355+
}
335356
await getList();
336357
337358
});

index.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,24 @@ export default class ForeignInlineListPlugin extends AdminForthPlugin {
9999
}
100100
}
101101
})
102+
server.endpoint({
103+
method: 'POST',
104+
path: `/plugin/${this.pluginInstanceId}/get_default_filters`,
105+
handler: async ({body}) => {
106+
if (!this.options.defaultFilters) {
107+
return { error: 'No default filters function defined', ok: false };
108+
}
109+
const record = body.record;
110+
if (!record) {
111+
return { error: 'No record provided in request body', ok: false };
112+
}
113+
const filters = this.options.defaultFilters(record);
114+
if (!Array.isArray(filters)) {
115+
throw new Error('defauiltFilters must return an array of FilterParams');
116+
}
117+
return {ok: true, filters};
118+
}
119+
})
102120
}
103121

104122
async modifyResourceConfig(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
@@ -112,9 +130,6 @@ export default class ForeignInlineListPlugin extends AdminForthPlugin {
112130
throw new Error(`ForeignInlineListPlugin: Resource with ID "${this.options.foreignResourceId}" not found. ${similar ? `Did you mean "${similar}"?` : ''}`);
113131
}
114132

115-
if (this.options.modifyTableResourceConfig) {
116-
this.options.modifyTableResourceConfig(this.foreignResource);
117-
}
118133

119134
const defaultSort = this.foreignResource.options?.defaultSort;
120135
const newColumn = {
@@ -132,6 +147,7 @@ export default class ForeignInlineListPlugin extends AdminForthPlugin {
132147
showRow: {
133148
file: this.componentPath('InlineList.vue'),
134149
meta: {
150+
defaultFiltersOn: this.options.defaultFilters ? true : false,
135151
...this.options,
136152
pluginInstanceId: this.pluginInstanceId,
137153
...(defaultSort

0 commit comments

Comments
 (0)