1
1
import { ColDef } from 'ag-grid-community' ;
2
2
import { AgGridReact } from 'ag-grid-react' ;
3
- import { useRef , useState } from 'react' ;
3
+ import { useEffect , useRef , useState } from 'react' ;
4
4
5
5
import { Box } from '@/components' ;
6
6
import { useGristTableData } from '@/features/grist' ;
@@ -21,34 +21,51 @@ export const DatabaseGrid = ({
21
21
tableId,
22
22
} ) ;
23
23
24
- const rowData = [
25
- { make : 'Tesla' , model : 'Model Y' , price : 64950 , electric : true } ,
26
- { make : 'Ford' , model : 'F-Series' , price : 33850 , electric : false } ,
27
- { make : 'Toyota' , model : 'Corolla' , price : 29600 , electric : false } ,
28
- ] ;
29
-
30
- // Column Definitions: Defines the columns to be displayed.
31
- const [ colDefs , setColDefs ] = useState < ColDef [ ] > ( [
32
- { field : 'make' , sort : 'desc' } ,
33
- {
34
- field : 'model' ,
35
- } ,
36
- { field : 'price' } ,
37
- { field : 'electric' } ,
38
- {
39
- headerComponentParams : {
40
- innerHeaderComponent : ( ) =>
41
- AddButtonComponent ( {
42
- addColumn,
43
- } ) ,
44
- } ,
45
- unSortIcon : false ,
46
- editable : false ,
47
- sortable : false ,
48
- spanRows : true ,
49
- filter : false ,
24
+ const [ rowData , setRowData ] =
25
+ useState < Record < string , string | number | boolean > [ ] > ( ) ;
26
+ const [ colDefs , setColDefs ] = useState < ColDef [ ] > ( ) ;
27
+
28
+ const addColumnColDef : ColDef = {
29
+ headerComponentParams : {
30
+ innerHeaderComponent : ( ) =>
31
+ AddButtonComponent ( {
32
+ addColumn,
33
+ } ) ,
50
34
} ,
51
- ] ) ;
35
+ unSortIcon : false ,
36
+ editable : false ,
37
+ sortable : false ,
38
+ spanRows : true ,
39
+ filter : false ,
40
+ } ;
41
+
42
+ useEffect ( ( ) => {
43
+ const filteredEntries = Object . entries ( tableData ) . filter (
44
+ ( [ key ] ) => key !== 'id' && key !== 'manualSort' ,
45
+ ) ;
46
+
47
+ const rowData1 : Record < string , string | number | boolean > [ ] = [ ] ;
48
+
49
+ const numRows = filteredEntries [ 0 ] ?. [ 1 ] . length ;
50
+
51
+ for ( let i = 0 ; i < numRows ; i ++ ) {
52
+ const row : Record < string , string | boolean | number > = { } ;
53
+ for ( const [ key , values ] of filteredEntries ) {
54
+ row [ key ] = values [ i ] ?? '' ;
55
+ }
56
+ rowData1 . push ( row ) ;
57
+ }
58
+
59
+ setRowData ( rowData1 ) ;
60
+
61
+ const columnNames = Object . keys ( Object . fromEntries ( filteredEntries ) ) ;
62
+ const columns : ColDef [ ] = columnNames . map ( ( key ) => ( {
63
+ field : key ,
64
+ } ) ) ;
65
+
66
+ setColDefs ( columns . concat ( addColumnColDef ) ) ;
67
+ // eslint-disable-next-line react-hooks/exhaustive-deps
68
+ } , [ tableData ] ) ;
52
69
53
70
const defaultColDef = {
54
71
flex : 1 ,
0 commit comments