row.add().draw() causes all rows to be detached

row.add().draw() causes all rows to be detached

radoubiwanradoubiwan Posts: 4Questions: 1Answers: 0

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:
Is there a way for a row to be added to the grid and rendered without all current rows to be detached?
We are using version 1.10.16.

We call row.add().draw() to add a new entry to the table, however in _fnDraw() a call to children().detach() is made. That causes focus on selected input in the grid to loos focus.
We need to be able to add a row without disturbing the existing ones (and not using paging).

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    You can try passing page into draw(), that should do the trick,

    Colin

  • radoubiwanradoubiwan Posts: 4Questions: 1Answers: 0

    Thanks Colin, I have tried and it doesn't make a difference. The application is not and can't use paging. Does the draw("page") only works when paging is enabled?

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    We need to be able to add a row without disturbing the existing ones

    Please provide more details about this. Why do you need to add rows while an input is in focus? Can you hold off adding the rows until there is no focused inputs?

    Kevin

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    A test case would be useful here. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • radoubiwanradoubiwan Posts: 4Questions: 1Answers: 0
    edited October 2020

    Hi Colin,
    I have added test case here:
    http://live.datatables.net/zelaquvu/32/edit?html,js,output

    Disable js autorun. run the js and scroll down until you don't see the input.
    Then the row addition will trigger and will cause 2 issues:
    1/ The grid will jump back to the top
    2/ Make the input lose focus

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    I didn't see a jump, but yep, the input lost focus. One thing you could do is work out in preDraw which element has the focus, and then put that focus back on draw. It's clumsy and messy, but I can't think of any other alternative.

    Colin

  • radoubiwanradoubiwan Posts: 4Questions: 1Answers: 0

    Thank you Colin.
    We have tried that approach but restoring the focus is what caused the grid to jump back to the focused cell. So if user is scrolling, a row added in the background will cause the scroll to jump.

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    Maybe one of the solutions in this SO thread will help. I tried one of them here:
    http://live.datatables.net/zelaquvu/37/edit

    Kevin

This discussion has been closed.