Start row calculated incorrectly when deleting row from last page

Start row calculated incorrectly when deleting row from last page

aando1136aando1136 Posts: 1Questions: 1Answers: 0

This bug can be found on the example provided for row selection and deletion:

http://www.datatables.net/examples/api/select_single_row.html

Steps to reproduce:
1. Navigate to
http://www.datatables.net/examples/api/select_single_row.html
2. Select page 6 in the paginator
3. Select any row in page 6 and click the 'Delete Selected Row' button

You will see that after following these steps, page 5 of the table will be displayed, yet page 6 will still be selected in the paginator. Page 6 should still be displayed after the deletion, not page 5. Clicking page 6 in the paginator will then show the proper page 6.

This appears to be due to the way the start row is calculated when the table is redrawn after deleting the row. In the function '_fnLengthOverflow' in 'jquery.dataTables.js' the start row is calculated with the following:

start = end - len;

However, this calculation appears to only be appropriate when the row being deleted is in the last row in the last page. For instance, in the example above the variables for 'start', 'end' and 'len' in function '_fnLengthOverflow' will be set as follows:

start = 50
end = 56
len = 10

Later in this function, 'start' is recalculated by subtracting 'len' from 'end' (56 - 10 = 46) which gives us 46 for 'start'. As a result, row 46, which is found on page 5, is considered the starting row. This results in page 5 being displayed (even though page 6 remains selected in the paginator).

It appears as though 'start' should only be recalculated if 'start' is equal to the length of the 'aiDisplay' array. Therefore to correct this locally in the '_fnLengthOverflow' function in 'jquery.dataTables.js' I replaced:

if ( end === settings.fnRecordsDisplay() )
{
start = end - len;
}

with:

if ( start === settings.fnRecordsDisplay() )
{
start = end - len;
}

After making this change the deletions now appear to be functioning properly.

This discussion has been closed.