@@ -29,36 +29,34 @@ export const agGrid = {
2929 entity . title ??= "Data Grid"
3030 entity . tickCount ??= 0
3131 entity . rowIdField ??= "id"
32- entity . compactColumns ??= false
3332 entity . themeClass ??= DEFAULT_THEME_CLASS
3433 entity . defaultColDef = {
3534 ...DEFAULT_COL_DEF ,
3635 ...( entity . defaultColDef || { } ) ,
3736 }
37+ entity . gridApiId ??= null
38+ entity . gridStatus ??= "mounting"
3839 } ,
3940
40- render ( entity ) {
41+ render ( entity , api ) {
4142 const instance = gridInstances . get ( entity . id )
42- const apiId = instance ? getApiId ( instance . api ) : "not-mounted"
43+ const apiId =
44+ entity . gridApiId ?? ( instance ? getApiId ( instance . api ) : "pending" )
4345
4446 return html `
4547 < section class ="iw-ag-grid ">
4648 < div class ="iw-ag-grid-meta ">
4749 < span > < b > Entity:</ b > ${ entity . id } </ span >
4850 < span > < b > Render Tick:</ b > ${ entity . tickCount } </ span >
4951 < span > < b > Grid API ID:</ b > ${ apiId } </ span >
50- < span
51- > < b > Status:</ b > ${ instance
52- ? "mounted (reused)"
53- : "initializing" } </ span
54- >
52+ < span > < b > Status:</ b > ${ entity . gridStatus } </ span >
5553 </ div >
5654
5755 < div
5856 class ="${ entity . themeClass } iw-ag-grid-host "
5957 ${ ref ( ( el ) => {
6058 if ( ! el ) return
61- mountOrUpdateGrid ( entity , el )
59+ mountOrUpdateGrid ( entity , el , api )
6260 } ) }
6361 > </ div >
6462 </ section >
@@ -108,11 +106,9 @@ export const agGrid = {
108106 ]
109107 } ,
110108
111- toggleColumnSet ( entity ) {
112- entity . compactColumns = ! entity . compactColumns
113- entity . columnDefs = entity . compactColumns
114- ? getCompactColumns ( )
115- : getDefaultColumns ( )
109+ gridMounted ( entity , payload ) {
110+ entity . gridApiId = payload ?. gridApiId ?? entity . gridApiId
111+ entity . gridStatus = "mounted (reused)"
116112 } ,
117113
118114 destroy ( entity ) {
@@ -124,7 +120,7 @@ export const agGrid = {
124120 } ,
125121}
126122
127- function mountOrUpdateGrid ( entity , element ) {
123+ function mountOrUpdateGrid ( entity , element , api ) {
128124 registerModulesOnce ( )
129125
130126 const existing = gridInstances . get ( entity . id )
@@ -135,16 +131,25 @@ function mountOrUpdateGrid(entity, element) {
135131 gridInstances . delete ( entity . id )
136132 }
137133
138- const api = createGrid ( element , buildGridOptions ( entity ) )
134+ const gridApi = createGrid ( element , buildGridOptions ( entity ) )
139135
140136 gridInstances . set ( entity . id , {
141- api,
137+ api : gridApi ,
142138 element,
139+ lastColumnDefs : entity . columnDefs ,
140+ lastRowData : entity . rowData ,
141+ lastDefaultColDef : entity . defaultColDef ,
142+ } )
143+
144+ queueMicrotask ( ( ) => {
145+ api . notify ( `#${ entity . id } :gridMounted` , {
146+ gridApiId : getApiId ( gridApi ) ,
147+ } )
143148 } )
144149 return
145150 }
146151
147- syncGrid ( existing . api , entity )
152+ syncGrid ( existing , entity )
148153}
149154
150155function registerModulesOnce ( ) {
@@ -163,12 +168,23 @@ function buildGridOptions(entity) {
163168 }
164169}
165170
166- function syncGrid ( api , entity ) {
167- api . updateGridOptions ( {
171+ function syncGrid ( instance , entity ) {
172+ const hasChanges =
173+ instance . lastDefaultColDef !== entity . defaultColDef ||
174+ instance . lastRowData !== entity . rowData ||
175+ instance . lastColumnDefs !== entity . columnDefs
176+
177+ if ( ! hasChanges ) return
178+
179+ instance . api . updateGridOptions ( {
168180 defaultColDef : entity . defaultColDef ,
169181 rowData : entity . rowData ,
170182 columnDefs : entity . columnDefs ,
171183 } )
184+
185+ instance . lastDefaultColDef = entity . defaultColDef
186+ instance . lastRowData = entity . rowData
187+ instance . lastColumnDefs = entity . columnDefs
172188}
173189
174190function getApiId ( api ) {
@@ -179,31 +195,3 @@ function getApiId(api) {
179195
180196 return apiIds . get ( api )
181197}
182-
183- function getDefaultColumns ( ) {
184- return [
185- { field : "id" , maxWidth : 100 } ,
186- { field : "product" } ,
187- { field : "category" } ,
188- { field : "country" } ,
189- {
190- field : "revenue" ,
191- valueFormatter : ( { value } ) => `$${ value . toLocaleString ( "en-US" ) } ` ,
192- } ,
193- { field : "price" , valueFormatter : ( { value } ) => `$${ value } ` } ,
194- { field : "rating" } ,
195- { field : "growth" , valueFormatter : ( { value } ) => `${ value } %` } ,
196- ]
197- }
198-
199- function getCompactColumns ( ) {
200- return [
201- { field : "id" , maxWidth : 100 } ,
202- { field : "product" } ,
203- {
204- field : "revenue" ,
205- valueFormatter : ( { value } ) => `$${ value . toLocaleString ( "en-US" ) } ` ,
206- } ,
207- { field : "growth" , valueFormatter : ( { value } ) => `${ value } %` } ,
208- ]
209- }
0 commit comments