@@ -15,7 +15,6 @@ import {
1515 HostListener ,
1616 inject ,
1717 input ,
18- Input ,
1918 IterableDiffer ,
2019 IterableDiffers ,
2120 linkedSignal ,
@@ -230,12 +229,7 @@ export class DatatableComponent<TRow extends Row = any>
230229 * The current offset ( page - 1 ) shown.
231230 * Default value: `0`
232231 */
233- @Input ( { transform : numberAttribute } ) set offset ( val : number ) {
234- this . _offset = val ;
235- }
236- get offset ( ) : number {
237- return Math . max ( Math . min ( this . _offset , Math . ceil ( this . rowCount ( ) / this . pageSize ( ) ) - 1 ) , 0 ) ;
238- }
232+ readonly offset = model < number > ( 0 ) ;
239233
240234 /**
241235 * Show the linear loading bar.
@@ -667,7 +661,6 @@ export class DatatableComponent<TRow extends Row = any>
667661 private readonly _rowDiffCount = signal ( 0 ) ;
668662
669663 _offsetX = 0 ;
670- _offset = 0 ;
671664 readonly _internalRows = computed ( ( ) => {
672665 this . _rowDiffCount ( ) ; // to trigger recalculation when row differ detects a change
673666 let rows = this . rows ( ) ?. slice ( ) ?? [ ] ;
@@ -734,6 +727,18 @@ export class DatatableComponent<TRow extends Row = any>
734727 )
735728 )
736729 ) ;
730+
731+ /**
732+ * Computed signal that returns the corrected offset value.
733+ * It ensures the offset is within valid bounds based on rowCount and pageSize.
734+ */
735+ readonly correctedOffset = computed ( ( ) => {
736+ const offset = this . offset ( ) ;
737+ const rowCount = this . rowCount ( ) ;
738+ const pageSize = this . pageSize ( ) ;
739+ return Math . max ( Math . min ( offset , Math . ceil ( rowCount / pageSize ) - 1 ) , 0 ) ;
740+ } ) ;
741+
737742 _subscriptions : Subscription [ ] = [ ] ;
738743 _defaultColumnWidth = this . configuration ?. defaultColumnWidth ?? 150 ;
739744 /**
@@ -952,14 +957,14 @@ export class DatatableComponent<TRow extends Row = any>
952957 return ;
953958 }
954959
955- this . offset = offset ;
960+ this . offset . set ( offset ) ;
956961
957- if ( ! isNaN ( this . offset ) ) {
962+ if ( ! isNaN ( this . correctedOffset ( ) ) ) {
958963 this . page . emit ( {
959964 count : this . count ( ) ,
960965 pageSize : this . pageSize ( ) ,
961966 limit : this . limit ( ) ,
962- offset : this . offset ,
967+ offset : this . correctedOffset ( ) ,
963968 sorts : this . sorts ( )
964969 } ) ;
965970 }
@@ -977,14 +982,14 @@ export class DatatableComponent<TRow extends Row = any>
977982 * The footer triggered a page event.
978983 */
979984 onFooterPage ( event : PagerPageEvent ) {
980- this . offset = event . page - 1 ;
981- this . _bodyComponent ( ) . updateOffsetY ( this . offset ) ;
985+ this . offset . set ( event . page - 1 ) ;
986+ this . _bodyComponent ( ) . updateOffsetY ( this . correctedOffset ( ) ) ;
982987
983988 this . page . emit ( {
984989 count : this . count ( ) ,
985990 pageSize : this . pageSize ( ) ,
986991 limit : this . limit ( ) ,
987- offset : this . offset ,
992+ offset : this . correctedOffset ( ) ,
988993 sorts : this . sorts ( )
989994 } ) ;
990995
@@ -1132,14 +1137,14 @@ export class DatatableComponent<TRow extends Row = any>
11321137 this . sorts . set ( event . sorts ) ;
11331138
11341139 // Always go to first page when sorting to see the newly sorted data
1135- this . offset = 0 ;
1136- this . _bodyComponent ( ) . updateOffsetY ( this . offset ) ;
1140+ this . offset . set ( 0 ) ;
1141+ this . _bodyComponent ( ) . updateOffsetY ( this . correctedOffset ( ) ) ;
11371142 // Emit the page object with updated offset value
11381143 this . page . emit ( {
11391144 count : this . count ( ) ,
11401145 pageSize : this . pageSize ( ) ,
11411146 limit : this . limit ( ) ,
1142- offset : this . offset ,
1147+ offset : this . correctedOffset ( ) ,
11431148 sorts : this . sorts ( )
11441149 } ) ;
11451150 this . sort . emit ( event ) ;
0 commit comments