Keep the index value after a search
Keep the index value after a search
lionellionel
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
Instead of using the
order
andsearch
events to process the indexes use theinit
event so it runs once at initialization.Kevin
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.
Yes, you are right. The
init
invocation is after the Datatable has initialized so it does nothing UseinitComplete
instead, like this:https://jsfiddle.net/rv9nz7u5/
Kevin
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?
Use
columns.searchable
to define which columns you don't want to search.Kevin
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/
You need to use
searchable: false
to not search the column. Even withsearchable: false
the row with index2
will be hidden when search for2
because no other data in that row has a2
:Kevin
Is there any way to have it include in the search result?
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
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!
Thats a very good way to keep indexes
Kevin