@@ -6,14 +6,18 @@ import { toast } from 'vue-sonner'
66interface Props {
77 columns: string []
88 records: Record <string , any >[]
9+ changes? : Record <string , Record <string , any []>>
910 editable? : boolean
1011 primaryKeys? : string []
1112}
1213
1314const props = withDefaults (defineProps <Props >(), {
15+ changes : () => ({}),
1416 primaryKeys : () => [],
1517})
1618
19+ const emit = defineEmits ([' update:changes' ])
20+
1721const Key = ` <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="#09090b">
1822 <path stroke-linecap="round" stroke-linejoin="round" d="M15.75 5.25a3 3 0 0 1 3 3m3 0a6 6 0 0 1-7.029 5.912c-.563-.097-1.159.026-1.563.43L10.5 17.25H8.25v2.25H6v2.25H2.25v-2.818c0-.597.237-1.17.659-1.591l6.499-6.499c.404-.404.527-1 .43-1.563A6 6 0 1 1 21.75 8.25Z" />
1923</svg>
@@ -56,7 +60,25 @@ VTable.register.icon('frozenCurrent', {
5660})
5761
5862const domRef = ref ()
59- const changes = ref <Record <string , Record <string , any []>>>({})
63+ const changes = useVModel (props , ' changes' , emit )
64+
65+ const records = computed (() => {
66+ if (! props .editable )
67+ return props .records
68+
69+ return props .records .map ((record ) => {
70+ const key = generateRowKeyFromRecord (record )
71+ record .__TABLITE_ROW_KEY = key
72+
73+ for (const column in record ) {
74+ const [_, changed] = changes .value [key ]?.[column ] ?? []
75+ if (changed )
76+ record [column ] = changed
77+ }
78+
79+ return record
80+ })
81+ })
6082
6183const columns = computed (() => {
6284 const constantColumns = [
@@ -107,7 +129,7 @@ function fieldFormatGenerator(key: string) {
107129
108130const options = computed <any >(() => ({
109131 columns: columns .value ,
110- records: props . records ,
132+ records: records . value ,
111133 theme: TABLITE_THEME ,
112134
113135 defaultRowHeight: 32 ,
@@ -141,7 +163,7 @@ onMounted(() => {
141163 const column = columns .value [col ]
142164 if (! column )
143165 return
144- const key = generateRowKeyFromIndex ( row )
166+ const key = instance . records [ row - 1 ]. __TABLITE_ROW_KEY
145167 if (! key )
146168 return
147169 if (! changes .value [key ])
@@ -176,34 +198,26 @@ function triggerUndoToast(key: string, field: string, origin: any, row: number,
176198
177199watch (props , async () => {
178200 instance .updateOption (options .value )
179- changes .value = {}
180201})
181202
182203function isEmpty(value : any ) {
183204 return [' ' , undefined , null ].includes (value )
184205}
185206
186- function hasChanged(col : number , row : any ) {
207+ function hasChanged(x : number , y : number ) {
187208 if (! props .editable || ! instance )
188209 return false
189- const record = instance .records [row - 1 ]
190- if (! record )
191- return false
192- const key = generateRowKeyFromIndex (row )
210+ const key = instance .records [y - 1 ]?.__TABLITE_ROW_KEY
193211 if (! key )
194212 return false
195- const column = columns .value [col ]
213+ const column = columns .value [x ]
196214 if (! column )
197215 return false
198-
199216 const [_, v] = changes .value [key ]?.[column .field ] ?? []
200217 return v
201218}
202219
203- function generateRowKeyFromIndex(index : number ) {
204- const record = instance .records [index - 1 ]
205- if (! record )
206- return
220+ function generateRowKeyFromRecord(record : any ) {
207221 const keys: Record <string , any > = {}
208222 props .primaryKeys .forEach ((k ) => {
209223 keys [k ] = record [k ]
0 commit comments