Keep the index value after a search

Keep the index value after a search

lionellionellionellionel Posts: 21Questions: 4Answers: 0

Hello everyone,

I find an answer from this question but It was quite old and I wasn't able to use it properly :/

I would like not to reinitialize the index after a search in order to have it logical. I create a fiddle to display the problem, when I write something in the input field, the index is recalculated and I would like to avoid that.

jsFiddle : https://jsfiddle.net/margarette/dr5o1v32/1/

Thank you in advance

Lionel

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949

    Instead of using the order and search events to process the indexes use the init event so it runs once at initialization.

    Kevin

  • lionellionellionellionel Posts: 21Questions: 4Answers: 0
    edited August 2021

    Thank you for your answer @kthorngren ,

    I replaced the order Event and the search Event by the init Event but it still doesn't work. :/

  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949

    Yes, you are right. The init invocation is after the Datatable has initialized so it does nothing :smile: Use initComplete instead, like this:
    https://jsfiddle.net/rv9nz7u5/

    Kevin

  • lionellionellionellionel Posts: 21Questions: 4Answers: 0

    Thank you!

    It works but not perfectly. If I search with 2 as a value, the line with the index 2 disapeared. Any idea to keep it?

  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949

    Use columns.searchable to define which columns you don't want to search.

    Kevin

  • lionellionellionellionel Posts: 21Questions: 4Answers: 0
    edited August 2021

    Thank you for your answer.

    Even with searchable: true, the line disappeared and the ordearable option doesn't work for the ID column.

    https://jsfiddle.net/margarette/8ygcsbm1/13/

  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949

    You need to use searchable: false to not search the column. Even with searchable: false the row with index 2 will be hidden when search for 2 because no other data in that row has a 2:

    2   Ashton Cox  Junior Technical Author San Francisco   66  $86,000
    

    Kevin

  • lionellionellionellionel Posts: 21Questions: 4Answers: 0

    Is there any way to have it include in the search result?

  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949
    Answer ✓

    Sorry, I misunderstood the question. I thought you didn't want it included in the search. You will need to use rows().invalidate() to have Datatables refresh its data cache after applying the indexes. For example:
    https://jsfiddle.net/my1htckz/

    Kevin

  • lionellionellionellionel Posts: 21Questions: 4Answers: 0

    Thank you for your answer!

    I finally used an index stored in database, easier to maintain and manage for me.

    Thank you a lot for your help!

  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949

    Thats a very good way to keep indexes :smile:

    Kevin

Sign In or Register to comment.