You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: adminforth/documentation/docs/tutorial/03-Customization/09-Actions.md
+46-1Lines changed: 46 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -48,12 +48,57 @@ Here's how to add a custom action:
48
48
-`name`: Display name of the action
49
49
-`icon`: Icon to show (using Flowbite icon set)
50
50
-`allowed`: Function to control access to the action
51
-
-`action`: Handler function that executes when action is triggered
51
+
-`action`: Handler function that executes when action is triggered for a **single** record
52
+
-`bulkHandler`: Handler function that executes when the action is triggered for **multiple** records at once (see [Bulk button with bulkHandler](#bulk-button-with-bulkhandler))
52
53
-`showIn`: Controls where the action appears
53
54
-`list`: whether to show in list view
54
55
-`listThreeDotsMenu`: whether to show in three dots menu in the list view
55
56
-`showButton`: whether to show as a button on show view
56
57
-`showThreeDotsMenu`: when to show in the three-dots menu of show view
58
+
-`bulkButton`: whether to show as a bulk-selection button in the list view
59
+
60
+
### Bulk button with `action`
61
+
62
+
When `showIn.bulkButton` is `true` and only `action` (not `bulkHandler`) is defined, AdminForth automatically calls your `action` function **once per selected record** using `Promise.all`. This is convenient for simple cases but means N separate handler invocations run in parallel:
63
+
64
+
```ts title="./resources/apartments.ts"
65
+
{
66
+
name: 'Auto submit',
67
+
action: async ({ recordId }) => {
68
+
// Called once per selected record when used as a bulk button
69
+
awaitdoSomething(recordId);
70
+
return { ok: true, successMessage: 'Done' };
71
+
},
72
+
showIn: {
73
+
bulkButton: true, // triggers Promise.all over selected records
74
+
showButton: true,
75
+
}
76
+
}
77
+
```
78
+
79
+
If your operation can be expressed more efficiently as a single batched query (e.g., a single `UPDATE … WHERE id IN (…)`), define `bulkHandler` instead. AdminForth will call it **once** with all selected record IDs:
80
+
81
+
```ts title="./resources/apartments.ts"
82
+
{
83
+
name: 'Auto submit',
84
+
// bulkHandler receives all recordIds in one call – use it for batched operations
// You can still keep `action` for the single-record show/edit buttons
90
+
action: async ({ recordId }) => {
91
+
awaitdoSomething(recordId);
92
+
return { ok: true, successMessage: 'Done' };
93
+
},
94
+
showIn: {
95
+
bulkButton: true,
96
+
showButton: true,
97
+
}
98
+
}
99
+
```
100
+
101
+
> ☝️ When both `action` and `bulkHandler` are defined, AdminForth uses `bulkHandler` for bulk operations and `action` for single-record operations. When only `action` is defined and `bulkButton` is enabled, AdminForth falls back to `Promise.all` over individual `action` calls.
errors.push(`Resource "${res.resourceId}" action "${action.name}" has showIn enabled for non-bulk locations (list, listThreeDotsMenu, showButton, showThreeDotsMenu) but has no "action" or "url" handler. Either add an "action" handler or set those showIn flags to false.`);
message: action?.bulkSuccessMessage||`${successResults.length} out of ${results.length} items processed successfully`,
364
+
message: action?.bulkSuccessMessage?action.bulkSuccessMessage:action?.hasBulkHandler?successResults[0].successMessage:`${successResults.length} out of ${results.length} items processed successfully`,
0 commit comments