Trying to use regex to search with a number
Trying to use regex to search with a number
So I have a table with username and number. I want to search an exact number e.g. 2 but when I use the regular .search() it gets me anything that has the number 2 (22, 12, 2...) in it. I only want to get cells that are == "2" to show up.
Here is what I tried so far but it's not working.
$('#submitbtn').on('click', function() {
var dateselected = $("#contestdate").val();
$('#example').DataTable().column(1).search("^" + dateselected + "$", true, false).draw();
});
Thanks
This question has an accepted answers - jump to answer
Answers
You code snippet seems to work here:
http://live.datatables.net/sijogewu/1/edit
Maybe you can update my test case to show your issue.
Kevin
Hey @kthorngren,
Yes you are right, it works. Sorry about that, I think I forgot to update the code and then I also forgot to change the params to "true, false".
Thanks!
Actually... I think I'm still having trouble with this. Is this supposed to work even if I'm populating the datatable with a database? I'm getting "No matching records found"..
I updated the example to use Javascript data, which would be similar to using Ajax based data:
http://live.datatables.net/sijogewu/3/edit
In order to help we will need to see an example with your data replicating the issue. Please post a link to your page or update my test case with an example of your data.
Kevin
Anyone aware if regex search needs any sort of extra param or setting to be configured? I'm populating my datatable with a database (https://datatables.net/examples/server_side/) and the regex search is not working. I keep getting "No matching records found".
Your server script would need to support regex searching. The Datatables examples, if that is what you are using, does not have this support. One thing to consider is regex searching of a database might be slow. What are you using for your server scripts?
Kevin
@kthorngren,
Ok, thanks for clarifying that it needs to be setup on the server scripts.
Yes I'm using the simple Datatables example from the server_side link I mentioned in my previous message.
I'm expecting to have around a maximum of 4000 entries total. It's for a small project so I don't think it's enough to slow down that much and even if it does it's not the end of the world because that portion will be for internal use.
Is there an example somewhere for a server script that supports regex searching?
For my server scripts, I'm using the following php files.
(from the server_side link and yes I updated my sql user, pass, db...)
<?php
//DataTables example server-side processing script.
// DB table to use
$table = 'registrations2019';
// Table's primary key
$primaryKey = 'id';
// Array of database columns which should be read and sent back to DataTables.
// The
db
parameter represents the column name in the database, while thedt
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
array( 'db' => 'username', 'dt' => 0 ),
array( 'db' => 'contest_num', 'dt' => 1 ),
array( 'db' => 'timestamp', 'dt' => 2 ),
);
// SQL server connection information
$sql_details = array(
'user' => '...',
'pass' => '...',
'db' => '...',
'host' => 'localhost'
);
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* If you just want to use the basic configuration for DataTables with PHP
* server-side, there is no need to edit below this line.
*/
require( 'ssp.class.php' );
echo json_encode(
SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);
And the ssp.class.php file
https://github.com/DataTables/DataTablesSrc/blob/master/examples/server_side/scripts/ssp.class.php
Have you tried without server side processing to see if the speed is still ok? See this FAQ. It is recommended to use client side processing if possible so you don't have to worry about searching in your server script.
The developer recently answered this same question here:
https://datatables.net/forums/discussion/comment/155814/#Comment_155814
Kevin
Unfortunately, I need to have server side processing for this project.
About the answer from the dev, It doesn't clearly say what he did to solve the issue. My understanding is that it's not supported when using server side processing by default, you need to change the code so that the regex search will work.
I guess I'll keep searching/trying.
If anyone is looking for the solution to this, here it is.
https://datatables.net/forums/discussion/36652/server-side-column-filtering-with-regular-expressions-regex