Skip to content

Suggestion/Patch: filterByKeys - filter array by multiple keys #1094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
borgand opened this issue Jul 28, 2015 · 4 comments
Closed

Suggestion/Patch: filterByKeys - filter array by multiple keys #1094

borgand opened this issue Jul 28, 2015 · 4 comments

Comments

@borgand
Copy link

borgand commented Jul 28, 2015

A common requirement for a filterBy search in ... is to specify multiple keys to look, but not all of them (as that might produce false positives from fields irrelevant/invisible to the user).

Below is a crude modification of filterBy, dubbed filterByKeys. Usage example:

<li v-repeat="users | filterByKeys searchText in 'firstname' 'lastname'">{{username}}</li>

Would it be possible to add this filter to the core (or perhaps replace filterBy with it, as it should be backwards compatible)?

Currently, keeping this in a separate custom filters file requires duplicating the contains function and pulling in the same requirements as for array-filters.js itself.

var filterByKeys = function(){
    // Convert arguments to an actual Array
    var args = [].slice.call(arguments)
    var arr = args.shift()
    var search = args.shift()
    // cast to lowercase string
    search = ('' + search).toLowerCase()
    // strip optional 'in'
    if (args[0] == 'in'){
      delete args[0]
    }
    // if left with 0 keys, fall back to all keys
    var keys = args.length > 0 ? args : Object.keys(args);

    // filter input array
    return arr.filter(function (item) {
      var result = false
      keys.forEach(function(datakey){
        result = result || contains(Path.get(item, datakey), search)
      })
      return result;
    })
  }

PS. Possible issues might arise from the arguments conversion as that is a bit funky and I'm not 100% sure of browser compatibility

@yyx990803
Copy link
Member

This could be implemented as an enhancement to the built-in filterBy. We just need to detect when there's more than 1 search keys.

@ssssteve
Copy link

I've just run into this today, and I'd love to see this implemented with an optional array in the dataKey parameter:

filterBy filterKey in ['name', 'power']

...especially if we can declare it in advance and pass it to the Vue component.

@borgand
Copy link
Author

borgand commented Aug 13, 2015

The array argument is very good idea, expecially, when passed as data
parameter.

On K, 12. august 2015 18:30 Steve [email protected] wrote:

I've just run into this today, and I'd love to see this implemented with
an optional array in the dataKey parameter:

filterBy filterKey in ['name', 'power']

...especially if we can declare it in advance and pass it to the Vue
component.


Reply to this email directly or view it on GitHub
https://github.com/yyx990803/vue/issues/1094#issuecomment-130343396.

@j4n4r
Copy link

j4n4r commented Aug 18, 2015

This would be good improvement - waiting...

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants