1- import { Component , Host , h , State , Prop } from '@stencil/core' ;
1+ import { Component , Host , h , State , Prop , Watch } from '@stencil/core' ;
22import formatter from 'format-number' ;
33
44const sort = (
@@ -57,6 +57,7 @@ export class DataTable {
5757 @State ( ) isEditingIndex : number = - 1 ;
5858 @State ( ) editingState : { [ rowColumnId : string ] : { prevValue : TField ; newValue : TField } } = { } ;
5959 // TODO: Need to find a way to use TColumn here
60+ @State ( ) columnNames : string [ ] = [ ] ;
6061 @Prop ( ) columns : {
6162 id : number | string ;
6263 key : string ;
@@ -88,7 +89,15 @@ export class DataTable {
8889 cellClass ?: string ;
8990 } ;
9091 } [ ] = [ ] ;
92+ @Watch ( 'columns' )
93+ watchPropHandler ( newValue : any , oldValue : any ) {
94+ if ( newValue !== oldValue ) {
95+ const updatedColumns = this . columns . map ( item => item . name ) ;
96+ this . columnNames = updatedColumns ;
97+ }
98+ }
9199 @Prop ( ) data : Array < any > = [ ] ;
100+ @State ( ) processedData : Array < any > = [ ] ;
92101 @Prop ( ) showActions : boolean = false ;
93102 @Prop ( ) onEdit : ( id : number | string , changes : Array < { prevValue : number | Date | string ; newValue : number | Date | string ; name : string } > ) => Promise < any > ;
94103 @Prop ( ) onDelete : ( index : number , row : { [ field : string ] : number | Date | string } ) => Promise < any > ;
@@ -206,6 +215,23 @@ export class DataTable {
206215 handlePaginate ( ) {
207216 this . onPaginate ( this . currentPage , this . limit ) ;
208217 }
218+ dataProcessor ( data ) {
219+ const newData = data . map ( row => {
220+ const processedRow = { ...row } ;
221+
222+ this . columns
223+ . map ( item => item . name )
224+ . forEach ( column => {
225+ if ( ! Object . keys ( row ) . includes ( column ) ) {
226+ processedRow [ column ] = '' ;
227+ }
228+ } ) ;
229+
230+ return processedRow ;
231+ } ) ;
232+
233+ return newData ;
234+ }
209235
210236 render ( ) {
211237 const renderAction = ( row : { [ field : string ] : TField } , rowId : number ) => {
@@ -352,13 +378,15 @@ export class DataTable {
352378 </ tr >
353379 </ thead >
354380 < tbody class = "bg-white divide-y divide-gray-200" >
355- { this . data . map ( ( row , rowId ) => {
381+ { this . dataProcessor ( this . data ) . map ( ( row , rowId ) => {
356382 return (
357383 < tr class = "hover:bg-gray-100 transition" >
358384 { renderAction ( row , rowId ) }
359- { Object . keys ( row ) . map ( ( fieldKey , columnId ) => {
360- return renderRow ( fieldKey , row [ fieldKey ] , rowId , columnId ) ;
361- } ) }
385+ { this . columns
386+ . map ( item => item . name )
387+ . map ( ( fieldKey , columnId ) => {
388+ return renderRow ( fieldKey , row [ fieldKey ] , rowId , columnId ) ;
389+ } ) }
362390 </ tr >
363391 ) ;
364392 } ) }
@@ -381,6 +409,7 @@ export class DataTable {
381409 onChange = { e => {
382410 // @ts -expect-error
383411 this . limit = e . target . value ;
412+ this . currentPage = 1 ;
384413 this . handlePaginate ( ) ;
385414 } }
386415 class = "form-select px-3 py-1.5 border-none text-inherit font-inherit text-gray-700 bg-transparent bg-clip-padding bg-no-repeat rounded transition ease-in-out focus:text-gray-700 focus:bg-white focus:border-blue-600 focus:outline-none"
0 commit comments