zepernick / Codeigniter-DataTables

CodeIgniter Library For Ajax Server Side DataTables 1.10 >
MIT License
94 stars 95 forks source link

No regex search? #11

Open fipsofant opened 8 years ago

fipsofant commented 8 years ago

Hey,

if I type on your example site/contact search "Doro", it will show me "Dorothy Young". If I type "Youn" I can't find anything. When I do the same on datatables.net examples, this kind of search works.

Why is it like this?

zepernick commented 8 years ago

It is using 2 different search providers if you will. Most of the examples on datatables.net are all doing client side searching. The entire table has been loaded through a JS array or from the DOM. This script is for chunking the table data through Ajax so that it goes back and requests data from the DB on the server for every search, sort, and page. The default search for the datatables.net is a anywhere match. The default search for this library is a left to right partial match. This type of matching is faster than searching anywhere in the string. However, you can still setup the library to have the same functionality by doing one of the following:

  1. Instruct your users to put the % wildcard in front of the search string when they want it.
  2. Use the setColumnSearchType method. I copied the snippet below from the README

setColumnSearchType(columnName, type) Set the matching type to be done for a given column. This will default to "after" if not specified columnName(string) - Name of the column matching what was passed in from the JavaScript in the data property type(string) - Type of search to perform. This will control the % wildcard on the like. valid values are: before, after, both, none

$this -> datatable -> setColumnSearchType('$.url', 'both');

There is not a method that turns on the column search type for the entire table. That would be a good one to add and I have made a note to add it to the library.

iprastha commented 8 years ago

Hi Zepernick,

I'm having the same issue as well, I've tried with the setColumnSearchType method already with 'both' set to all my columns with no result. After reading through the library code, I found that this method only applies with column search, not the global search via the single-textbox search built into the datatable.

So in library\Datatable.php , line 408:

$sqlOr .= $op . $c . ' LIKE \'' . $this->CI->db->escape_like_str($gSearchVal) . '%\'';

For this workaround, I changed to :

$sqlOr .= $op . $c . ' LIKE \'%' . $this->CI->db->escape_like_str($gSearchVal) . '%\'';

I'm not sure this is the best way to do it, but you might have a different suggestion / solution to this ?