DeuxHuitHuit / quicksearch

A jQuery plugin for searching through DOM Elements quickly
https://www.npmjs.org/package/jquery.quicksearch
Other
135 stars 35 forks source link

Searching Data Attributes? #26

Open christianmagill opened 8 years ago

christianmagill commented 8 years ago

Is there any way to search data attributes of the given elements?

A custom function option for generating searchable text from elements would be very useful for this and other cases.

christianmagill commented 8 years ago

After digging around the source code, I settled on this solution.

testQuery: function (query, txt, _row) { txt = $(_row).attr('data-text'); txt = txt.toLowerCase(); for (var i = 0; i < query.length; i += 1) { if (txt.indexOf(query[i]) === -1) { return false; } } return true; }

nitriques commented 8 years ago

Nice imperfect solution!

A custom function option for generating searchable text from elements would be very useful for this and other cases.

I like this a lot so I'll reopen this issue. Thanks!

christianmagill commented 8 years ago

Great! I look forward to a simpler solution.

zaus commented 8 years ago

I think you're talking about offering a callback like cacheUsing(possibleResult) that lets you override the functionality applied at https://github.com/DeuxHuitHuit/quicksearch/blob/master/src/jquery.quicksearch.js#L273 and here

So instead of:

var temp = self.strip_html(this.innerHTML);

it would be

var temp = self.strip_html(options.cacheUsing(this));

By default you'd have:

defaults: {
    ...
    , cacheUsing: function(possibleResult) { return possibleResult.innerHTML; }

And override for your scenario

, cacheUsing: function(possibleResult) { return $(possibleResult).data('whatever'); }