Search functionality
Search functionality
kms
Posts: 15Questions: 2Answers: 0
When I populate the search box with one character (e.g. f), the table does not always update accordingly and see records that do not have that character in the row.
I would like to know why this happens and whether there is a bug fix or a solution to this problem.
Please advise as to what should be done.
Thanks
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Please can you link to a test case showing the issue. I'm not aware of any bugs in DataTables at this time relating to what you describe, so to be able to offer any help I would need a test case (as noted in the forum rules).
Allan
I have a row whereby there are words and none of them have the letter 'f' in them, the moment I insert that letter into the search box, the data in the dataTables table does not update accordingly (e.g. remove the data based on the search criteria where there are no records that meet the conditions required in the search box).
I would need a link to a test case as I noted above.
If you take a look at this example and type an
f
into the search box, you will see that it does work in that case.Allan
I do have <javascript></javascript> show and display input type="checkbox" at the end of each row and have realised that this is the reason as to why if I put say 'f' into the search box it does not update accordingly based on the data in the table.
These show and display checkboxes are used in order to change the data in the table.
DataTables should be stripping the HTML tags from the search data, but if you have inline Javascript, then that would present an issue since that isn't stripped. I would have been able to tell you that in the first instance with a test case :-)
Allan
You are correct regarding the second part of your answer e.g. " you have inline Javascript, then that would present an issue since that isn't stripped " so know where I am coming from and would like to know if there is anything that can be done in that case as this is being handled in javascript by doing a document.write for it to appear.
My personal suggestion would be to not have inline Javascript and it is not in keeping with best practices. However, what you could do is use
columns.render
and when thefilter
type is requested perform a regex remove on the data to strip out the Javascript.Allan
I can see that you cannot have an <input> tag inside a <td> (e.g. <td><input></input></td> as this makes the search return with results that do not change the table view.
You can, but you would need to invalidate the search cache for the row on every change, since DataTables caches the search term. Reading it from the DOM every time would be far too slow for general use.
Allan
Please could you explain how I would invalidate the search cache for the row on every change, as per your previous message.
Consider that the tables retrieve data by sql and that the amount of rows in each table varies and is not consistent.
Thanks.
You would use
row().invalidate()
. You'd need to do that on each key press and also have the row source the filter data from the input element (which you would do in thecolumns.render
option.Allan
Is there any means of being able to control where the search operates. i.e number of columns (td tags) - 2, as an example?
I don't really understand I'm afraid. You can use
columns.searchable
to remove a column from being searchable, or usecolumn().search()
if you want to search only a specific column.Allan
Is it possible to create a string and then use that to getElementById or getElementByName so that those specific elements are not used in the table as per your earlier message using columns.searchableDT
For example, I would get the element by id or name and then say "targets":: "string_name_here"
I'm afraid I don't really understand what you are looking to do. You want to search the table based on an element id in the table? That isn't possible by default since DataTables strips the HTML for the search, but you could create a filtering plug-in to not strip it - or set the
columns.type
explicitly tostring
which will also not strip the HTML.Allan
I have various rows of data in the table and at each end of the <tr> are <input type="checkbox"> these seem to make the search results not update accordingly to the very first question that I had sent. I could have up to three of these checkboxes on any given page, however, I could have say one of these checkboxes.
Is this the reason as to why if you want to do this:-
$('#example').dataTable( {
"columnDefs": [
{ "searchable": false, "targets": 0 }
]
} );
"targets" cannot be handled as say the name of the element by id or the name of the element as I have found that I do not want those input types to be included in the search as it affects the results returned, at present I have to manually have this "targets": -1.
However, I have a delete function and copy function as well so it would depend on whether the user can delete/copy/edit.
In your table (http://datatables.net/examples/basic_init/zero_configuration.html)
you do not have any input types where the user would be able to change/remove or add data (e.g. buttons/checkboxes), therefore at the end of each table row in my tables, I have three checkboxes, although sometimes one or two. Therefore I need to know how I would handle these as these input types create a problem when searching for data, hence why in your table that is possible, but is not in my tables.
Okay, so am I right in thinking that some cells have the
input
element and some don't. Can you don't want any of them to be searchable?If so, can you try using the nightly version of DataTables please it fixes a bug in this area.
Allan
Yes that is correct.
Just to let you know that I have tried using the nightly version of DataTables and yes it works perfectly.
Thanks.
Good to hear - thanks.
Allan