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/02-customFieldRendering.md
+91Lines changed: 91 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -233,6 +233,97 @@ Now you can use this component in the configuration of the resource:
233
233
}
234
234
```
235
235
236
+
### Custom record editing (updating other fields)
237
+
238
+
Sometimes a custom editor needs to update not only its own field, but also other fields of the record (for example, generate a slug from a title).
239
+
240
+
For this, custom `edit`/`create` components can emit an `update:recordFieldValue` event with the payload `{ fieldName, fieldValue }`. AdminForth will update the corresponding field in the record.
241
+
242
+
```html title='./custom/TitleWithSlugEditor.vue'
243
+
<template>
244
+
<div class="flex flex-col gap-2">
245
+
<Input
246
+
:model-value="record[column.name]"
247
+
:placeholder="$t('Title')"
248
+
@update:model-value="onTitleChange"
249
+
/>
250
+
<Input
251
+
:model-value="record.slug"
252
+
:placeholder="$t('Slug')"
253
+
readonly
254
+
/>
255
+
</div>
256
+
</template>
257
+
258
+
<script setup lang="ts">
259
+
importInputfrom"@/afcl/Input.vue";
260
+
import type {
261
+
AdminForthResourceColumnCommon,
262
+
AdminForthResourceCommon,
263
+
AdminUser,
264
+
} from"@/types/Common";
265
+
266
+
constprops= defineProps<{
267
+
column: AdminForthResourceColumnCommon;
268
+
record: any;
269
+
meta: any;
270
+
resource: AdminForthResourceCommon;
271
+
adminUser: AdminUser;
272
+
readonly: boolean;
273
+
}>();
274
+
275
+
constemit=defineEmits([
276
+
"update:value", // update current column value
277
+
"update:recordFieldValue", // update any other field in the record
278
+
]);
279
+
280
+
functionslugify(value:string) {
281
+
return value
282
+
?.toLowerCase()
283
+
.trim()
284
+
.replace(/[^a-z0-9]+/g, "-")
285
+
.replace(/(^-|-$)+/g, "");
286
+
}
287
+
288
+
functiononTitleChange(newTitle:string) {
289
+
// update current column value
290
+
emit("update:value", newTitle);
291
+
292
+
// update another field in the record (e.g. slug)
293
+
emit("update:recordFieldValue", {
294
+
fieldName:"slug",
295
+
fieldValue:slugify(newTitle),
296
+
});
297
+
}
298
+
</script>
299
+
```
300
+
301
+
And use it in the resource configuration for both `edit` and `create` views:
302
+
303
+
```ts title='./resources/apartments.ts'
304
+
{
305
+
...
306
+
resourceId:'aparts',
307
+
columns: [
308
+
...
309
+
{
310
+
name:'title',
311
+
components: {
312
+
edit:'@@/TitleWithSlugEditor.vue',
313
+
create:'@@/TitleWithSlugEditor.vue',
314
+
},
315
+
},
316
+
{
317
+
name:'slug',
318
+
// standard input; value will be kept in sync
319
+
...
320
+
},
321
+
...
322
+
],
323
+
...
324
+
}
325
+
```
326
+
236
327
### Custom inValidity inside of the custom create/edit components
237
328
238
329
Custom componets can emit `update:inValidity` event to parent to say that the field is invalid.
0 commit comments