Datatables Filter - Exact search and smart search

Datatables Filter - Exact search and smart search

sidmanutdsidmanutd Posts: 3Questions: 2Answers: 0

want to persist with the smart search ( search as i type ) that datatables has. The problem is, one of my columns shows values like: person and personnel. So with million records, it is hard for me to dig down to "person" with the column just showing "personnel" when i type "person".

I do not want only exact match. That would make me type the whole name as in : "person-xyz-123" in some cases.

Is there a way for me to specify say "person" in quotes and tell datatables that i just want to do exact search when i type in quotes and still persist with my normal search ?

Answers

  • ignignoktignignokt Posts: 146Questions: 4Answers: 39
    edited October 2014

    With millions of records, I am assuming you are using server-side processing? If so, in the filter function you could probably do something like:

    if($request['search']['value'][0] === "|")
    

    And change it so when the search starts with a pipe, it will perform either a LIKE search without the wildcard (%) before and after the search term, or you can just make it WHERE column = search or whatever fits your needs. Then just remember to take the pipe out of the string in your query.

    I'm sure there are other ways, and possibly a better way; but that should work.

  • sidmanutdsidmanutd Posts: 3Questions: 2Answers: 0

    This is how i solved my problem. Below is the helper function i created:

    
      def filter_helper(data)
        return "#{data[:name]} = :search", search: "#{params[data[:dt_name]].gsub!(/^\"|\"?$/, '') }" if "#{params[data[:dt_name]]}".chars.first == "\""
        return "#{data[:name]} like :search", search: "%#{params[data[:dt_name]]}%" if "#{params[data[:dt_name]]}".chars.first != "\""
      end
    
    
This discussion has been closed.