Back to page number after state.clear

Back to page number after state.clear

CSTecnoCSTecno Posts: 6Questions: 2Answers: 0

Hello, I have a problem that I can't find a solution to, I want the table to return me to the page number after doing an action. The problem is that I'm calling state.clear to clean the filters of my table and when I reload the table it returns me again to page 0 and I want it to return me to the page number I was on.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,342Questions: 26Answers: 4,954

    Are you reloading the table using ajax.reload()? If yes then see the 2nd example in the docs for staying on the same page.

    If not you can use the page() API to both get (before reloading) and set (after reloading) the page number.

    Kevin

  • CSTecnoCSTecno Posts: 6Questions: 2Answers: 0

    Hi kthorngren , I'm actually calling
    drawCallback: function () {
    this.api().state.clear();
    }
    every time I perform an operation on my table.

  • kthorngrenkthorngren Posts: 21,342Questions: 26Answers: 4,954
    edited December 2022

    As far a I know calling state.clear() doesn't change the page you are on. Are you calling draw() for the operations you are performing? It has a similar paging control.

    Can you post a link to your page or a test case replicating the issue so we can see what you have and help debug?
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • CSTecnoCSTecno Posts: 6Questions: 2Answers: 0

    This is my table, although I can't get it to work as it should,
    http://live.datatables.net/voqaliva/1/edit

  • kthorngrenkthorngren Posts: 21,342Questions: 26Answers: 4,954
    edited December 2022

    You need to add the selector .datatable-validation to the table tag so jQuery can find it.
    http://live.datatables.net/jixawoke/1/edit

    When using the global search and sorting the table Datatables defaults to showing the first page. Using state.clear() does not affect this behavior. Are you wanting to change the default behavior of sorting and the global search?

    You could create your own search input and remove the default using the dom option. Something like this example from this thread. Pass the page parameter in the draw() API to stay on the same page, assuming its still available. Do the same here:

                                    column
                                        .search(val ? '^' + val + '$' : '', true, false)
                                        .draw();
    

    I'm not sure there is an easy way to work around the sorting behavior.

    EDIT: I don't understand why you are clearing the save state in drawCallback. What is the reason?

    Kevin

  • CSTecnoCSTecno Posts: 6Questions: 2Answers: 0

    Hi Kevin, I use drawCallback to clear the custom search filter, every time I reload the page, the filter is still there when I enter the table from another place, it should be cleared, is there a way to remove that filter when reloading my page ? would solve my problem without having to use a state.clear. Cheers and thanks for your time

  • kthorngrenkthorngren Posts: 21,342Questions: 26Answers: 4,954
    Answer ✓

    I use drawCallback to clear the custom search filter, every time I reload the page,

    crawCallback is called every time there is a draw event, ie, search sort, page. If you want something to occur only when the page is reloaded use initComplete.

    If I understand correctly you want to clear the column searches when the table state is restored. You can use stateLoadParams to clear the saved column searches. Use 'console.log( data );` in the function to see data structure of the save data. Loop through the columns to clear the column search terms. Something like this:
    http://live.datatables.net/jiribeqi/1/edit

    Kevin

  • CSTecnoCSTecno Posts: 6Questions: 2Answers: 0

    Thank you very much for your help Kevin, I was able to fix it, what I did was iterate through the column and clean the filter as you mentioned, again thank you very much for your time.

Sign In or Register to comment.