1- <script setup lang="ts">
1+ <script setup lang="ts" generic = " T = any " >
22import { message } from ' ant-design-vue'
33import type { ComputedRef } from ' vue'
44import type { StdTableProps } from ' ./StdTable.vue'
@@ -7,7 +7,7 @@ import StdDataEntry from '@/components/StdDesign/StdDataEntry'
77import type { Column } from ' @/components/StdDesign/types'
88import StdCurdDetail from ' @/components/StdDesign/StdDataDisplay/StdCurdDetail.vue'
99
10- export interface StdCurdProps {
10+ export interface StdCurdProps < T > extends StdTableProps < T > {
1111 cardTitleKey? : string
1212 modalMaxWidth? : string | number
1313 modalMask? : boolean
@@ -16,28 +16,28 @@ export interface StdCurdProps {
1616
1717 disableAdd? : boolean
1818 onClickAdd? : () => void
19- // eslint-disable-next-line @typescript-eslint/no-explicit-any
20- onClickEdit? : (id : number | string , record : any , index : number ) => void
19+
20+ onClickEdit? : (id : number | string , record : T , index : number ) => void
2121 // eslint-disable-next-line @typescript-eslint/no-explicit-any
2222 beforeSave? : (data : any ) => Promise <void >
2323}
2424
25- const props = defineProps <StdTableProps & StdCurdProps >()
26- const route = useRoute ()
27- const router = useRouter ()
25+ const props = defineProps <StdTableProps <T > & StdCurdProps <T >>()
2826const visible = ref (false )
29- const update = ref (0 )
3027// eslint-disable-next-line @typescript-eslint/no-explicit-any
3128const data: any = reactive ({ id: null })
3229const modifyMode = ref (true )
3330const editMode = ref <string >()
31+ const shouldRefetchList = ref (false )
3432
3533provide (' data' , data )
3634provide (' editMode' , editMode )
35+ provide (' shouldRefetchList' , shouldRefetchList )
3736
3837// eslint-disable-next-line @typescript-eslint/no-explicit-any
3938const error: any = reactive ({})
4039const selected = ref ([])
40+
4141// eslint-disable-next-line @typescript-eslint/no-explicit-any
4242function onSelect(keys : any ) {
4343 selected .value = keys
@@ -49,19 +49,35 @@ const editableColumns = computed(() => {
4949 })
5050}) as ComputedRef <Column []>
5151
52- function add() {
52+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
53+ function add(preset : any = undefined ) {
54+ if (props .onClickAdd )
55+ return
5356 Object .keys (data ).forEach (v => {
5457 delete data [v ]
5558 })
5659
60+ if (preset )
61+ Object .assign (data , preset )
62+
5763 clear_error ()
5864 visible .value = true
5965 editMode .value = ' create'
6066 modifyMode .value = true
6167}
68+
6269const table = ref ()
6370
64- const selectedRowKeys = ref ([])
71+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
72+ const selectedRowKeys = defineModel <any []>(' selectedRowKeys' , {
73+ default : () => [],
74+ })
75+
76+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
77+ const selectedRows = defineModel <any []>(' selectedRows' , {
78+ type: Array ,
79+ default : () => [],
80+ })
6581
6682const getParams = reactive ({
6783 trash: false ,
@@ -91,13 +107,14 @@ function clear_error() {
91107}
92108
93109const stdEntryRef = ref ()
110+
94111async function ok() {
95112 const { formRef } = stdEntryRef .value
96113
97114 clear_error ()
98115 try {
99116 await formRef .validateFields ()
100- await props ?.beforeSave ?.(data )
117+ props ?.beforeSave ?.(data )
101118 props
102119 .api ! .save (data .id , { ... data , ... props .overwriteParams }, { params: { ... props .overwriteParams } })
103120 .then (r => {
@@ -111,37 +128,26 @@ async function ok() {
111128 Object .assign (error , e .errors )
112129 })
113130 }
114- catch { }
131+ catch {
132+ message .error ($gettext (' Please fill in the required fields' ))
133+ }
115134}
116135
117136function cancel() {
118137 visible .value = false
119138
120139 clear_error ()
121- }
122140
123- watch (visible , v => {
124- if (! v ) {
125- router .push ({
126- query: {
127- ... route .query ,
128- [` open.${props .rowKey || ' id' } ` ]: undefined ,
129- },
130- })
141+ if (shouldRefetchList .value ) {
142+ get_list ()
143+ shouldRefetchList .value = false
131144 }
132- })
133-
134- watch (modifyMode , v => {
135- router .push ({
136- query: {
137- ... route .query ,
138- modify_mode: v .toString (),
139- },
140- })
141- })
145+ }
142146
143147function edit(id : number | string ) {
144- get (id , true ).then (() => {
148+ if (props .onClickEdit )
149+ return
150+ get (id ).then (() => {
145151 visible .value = true
146152 modifyMode .value = true
147153 editMode .value = ' modify'
@@ -151,15 +157,15 @@ function edit(id: number | string) {
151157}
152158
153159function view(id : number | string ) {
154- get (id , false ).then (() => {
160+ get (id ).then (() => {
155161 visible .value = true
156162 modifyMode .value = false
157163 }).catch (e => {
158164 message .error ($gettext (e ?.message ?? ' Server error' ), 5 )
159165 })
160166}
161167
162- async function get(id : number | string , _modifyMode : boolean ) {
168+ async function get(id : number | string ) {
163169 return props
164170 .api ! .get (id , { ... props .overwriteParams })
165171 .then (async r => {
@@ -168,32 +174,14 @@ async function get(id: number | string, _modifyMode: boolean) {
168174 })
169175 data .id = null
170176 Object .assign (data , r )
171- if (! props .disabledModify ) {
172- await router .push ({
173- query: {
174- ... route .query ,
175- [` open.${props .rowKey || ' id' } ` ]: id ,
176- modify_mode: _modifyMode .toString (),
177- },
178- })
179- }
180177 })
181178}
182179
183- onMounted (async () => {
184- const id = route .query [` open.${props .rowKey || ' id' } ` ] as string
185- const _modifyMode = (route .query .modify_mode as string ) === ' true'
186- if (id && ! props .disabledModify && _modifyMode )
187- edit (id )
188-
189- if (id && ! props .disabledView && ! _modifyMode )
190- view (id )
191- })
192-
193180const modalTitle = computed (() => {
194181 return data .id ? modifyMode .value ? $gettext (' Modify' ) : $gettext (' View Details' ) : $gettext (' Add' )
195182})
196183
184+ const localOverwriteParams = reactive (props .overwriteParams ?? {})
197185 </script >
198186
199187<template >
@@ -209,7 +197,7 @@ const modalTitle = computed(() => {
209197 <ASpace >
210198 <slot name =" beforeAdd" />
211199 <a
212- v-if =" !disableAdd"
200+ v-if =" !disableAdd && !getParams.trash "
213201 @click =" add"
214202 >{{ $gettext('Add') }}</a >
215203 <slot name =" extra" />
@@ -224,19 +212,20 @@ const modalTitle = computed(() => {
224212 v-else
225213 @click =" getParams.trash = false"
226214 >
227- {{ $gettext('List ') }}
215+ {{ $gettext('Back to list ') }}
228216 </a >
229217 </template >
230218 </ASpace >
231219 </template >
232220
233221 <StdTable
234222 ref =" table"
235- :key =" update"
236223 v-model:selected-row-keys =" selectedRowKeys"
224+ v-model:selected-rows =" selectedRows"
237225 v-bind =" {
238226 ...props,
239227 getParams,
228+ overwriteParams: localOverwriteParams,
240229 }"
241230 @click-edit =" edit"
242231 @click-view =" view"
@@ -269,7 +258,7 @@ const modalTitle = computed(() => {
269258 @ok =" ok"
270259 >
271260 <div
272- v-if =" !disabledModify && !disabledView "
261+ v-if =" !disableModify && !disableView && editMode === 'modify' "
273262 class =" m-2 flex justify-end"
274263 >
275264 <ASwitch
@@ -294,7 +283,7 @@ const modalTitle = computed(() => {
294283 ref =" stdEntryRef"
295284 :data-list =" editableColumns"
296285 :data-source =" data"
297- :error =" error"
286+ :errors =" error"
298287 />
299288
300289 <slot
0 commit comments