@@ -17,11 +17,13 @@ import {
17
17
import { shallowEqual } from "react-redux" ;
18
18
import { JSONObject , JSONValue } from "util/jsonTypes" ;
19
19
import { lastValueIfEqual } from "util/objectUtils" ;
20
+ import { SummaryColumnComp } from "./tableSummaryColumnComp" ;
20
21
21
22
/**
22
23
* column list
23
24
*/
24
25
const ColumnListTmpComp = list ( ColumnComp ) ;
26
+ const SummaryColumnListTmpComp = list ( SummaryColumnComp ) ;
25
27
26
28
/**
27
29
* rowExample is used for code prompts
@@ -190,3 +192,139 @@ export class ColumnListComp extends ColumnListTmpComp {
190
192
return this . forEachAction ( ColumnComp . setSelectionAction ( key ) ) ;
191
193
}
192
194
}
195
+
196
+ export class SummaryColumnListComp extends SummaryColumnListTmpComp {
197
+ override reduce ( action : CompAction ) : this {
198
+ if ( isMyCustomAction < ActionDataType > ( action , "dataChanged" ) ) {
199
+ const rowExample = action . value . rowExample ;
200
+ const { readOnly } = getReduceContext ( ) ;
201
+ let comp = this ;
202
+ if ( action . value . doGeneColumn && ( action . value . dynamicColumn || ! readOnly ) ) {
203
+ const actions = this . geneColumnsAction ( rowExample , action . value . data ) ;
204
+ comp = this . reduce ( this . multiAction ( actions ) ) ;
205
+ }
206
+ return comp ;
207
+ }
208
+ return super . reduce ( action ) ;
209
+ }
210
+
211
+ getChangeSet ( ) {
212
+ const changeSet : Record < string , Record < string , JSONValue > > = { } ;
213
+ const columns = this . getView ( ) ;
214
+ columns . forEach ( ( column ) => {
215
+ const columnChangeSet = column . getChangeSet ( ) ;
216
+ Object . keys ( columnChangeSet ) . forEach ( ( dataIndex ) => {
217
+ Object . keys ( columnChangeSet [ dataIndex ] ) . forEach ( ( key ) => {
218
+ if ( ! _ . isNil ( columnChangeSet [ dataIndex ] [ key ] ) ) {
219
+ if ( ! changeSet [ key ] ) changeSet [ key ] = { } ;
220
+ changeSet [ key ] [ dataIndex ] = columnChangeSet [ dataIndex ] [ key ] ;
221
+ }
222
+ } ) ;
223
+ } ) ;
224
+ } ) ;
225
+ return changeSet ;
226
+ }
227
+
228
+ dispatchClearChangeSet ( ) {
229
+ const columns = this . getView ( ) ;
230
+ columns . forEach ( ( column ) => column . dispatchClearChangeSet ( ) ) ;
231
+ }
232
+
233
+ /**
234
+ * If the table data changes, call this method to trigger the action
235
+ */
236
+ dataChangedAction ( param : {
237
+ rowExample : JSONObject ;
238
+ doGeneColumn : boolean ;
239
+ dynamicColumn : boolean ;
240
+ data : Array < JSONObject > ;
241
+ } ) {
242
+ return customAction < ActionDataType > (
243
+ {
244
+ type : "dataChanged" ,
245
+ ...param ,
246
+ } ,
247
+ true
248
+ ) ;
249
+ }
250
+
251
+ /**
252
+ * According to the data, adjust the column
253
+ */
254
+ private geneColumnsAction ( rowExample : RowExampleType , data : Array < JSONObject > ) {
255
+ // If no data, return directly
256
+ if ( rowExample === undefined || rowExample === null ) {
257
+ return [ ] ;
258
+ }
259
+ const dataKeys = Object . keys ( rowExample ) ;
260
+ if ( dataKeys . length === 0 ) {
261
+ return [ ] ;
262
+ }
263
+ const columnsView = this . getView ( ) ;
264
+ const actions : Array < any > = [ ] ;
265
+ let deleteCnt = 0 ;
266
+ columnsView . forEach ( ( column , index ) => {
267
+ if ( column . getView ( ) . isCustom ) {
268
+ return ;
269
+ }
270
+ const dataIndex = column . getView ( ) . dataIndex ;
271
+ if ( dataIndex === COLUMN_CHILDREN_KEY || ! dataKeys . find ( ( key ) => dataIndex === key ) ) {
272
+ // to Delete
273
+ actions . push ( this . deleteAction ( index - deleteCnt ) ) ;
274
+ deleteCnt += 1 ;
275
+ }
276
+ } ) ;
277
+ // The order should be the same as the data
278
+ dataKeys . forEach ( ( key ) => {
279
+ if ( key === COLUMN_CHILDREN_KEY && supportChildrenTree ( data ) ) {
280
+ return ;
281
+ }
282
+ if ( ! columnsView . find ( ( column ) => column . getView ( ) . dataIndex === key ) ) {
283
+ // to Add
284
+ actions . push ( this . pushAction ( newPrimaryColumn ( key , calcColumnWidth ( key , data ) ) ) ) ;
285
+ }
286
+ } ) ;
287
+ if ( actions . length === 0 ) {
288
+ return [ ] ;
289
+ }
290
+ return actions ;
291
+ }
292
+
293
+ withParamsNode ( ) {
294
+ const columns = this . getView ( ) ;
295
+ const nodes = _ ( columns )
296
+ . map ( ( col ) => col . children . render . getOriginalComp ( ) . node ( ) )
297
+ . toPairs ( )
298
+ . fromPairs ( )
299
+ . value ( ) ;
300
+ const result = lastValueIfEqual (
301
+ this ,
302
+ "withParamsNode" ,
303
+ [ fromRecord ( nodes ) , nodes ] as const ,
304
+ ( a , b ) => shallowEqual ( a [ 1 ] , b [ 1 ] )
305
+ ) [ 0 ] ;
306
+ return result ;
307
+ }
308
+
309
+ getColumnsNode < T extends keyof SummaryColumnComp [ "children" ] > (
310
+ field : T
311
+ ) : RecordNode < Record < string , ReturnType < SummaryColumnComp [ "children" ] [ T ] [ "node" ] > > > {
312
+ const columns = this . getView ( ) ;
313
+ const nodes = _ ( columns )
314
+ . map ( ( col ) => col . children [ field ] . node ( ) as ReturnType < SummaryColumnComp [ "children" ] [ T ] [ "node" ] > )
315
+ . toPairs ( )
316
+ . fromPairs ( )
317
+ . value ( ) ;
318
+ const result = lastValueIfEqual (
319
+ this ,
320
+ "col_nodes_" + field ,
321
+ [ fromRecord ( nodes ) , nodes ] as const ,
322
+ ( a , b ) => shallowEqual ( a [ 1 ] , b [ 1 ] )
323
+ ) [ 0 ] ;
324
+ return result ;
325
+ }
326
+
327
+ setSelectionAction ( key : string ) {
328
+ return this . forEachAction ( ColumnComp . setSelectionAction ( key ) ) ;
329
+ }
330
+ }
0 commit comments