3

Vuejs 2.0 deprecated the built in filters (filterby, orderby, limitby) when using v-for. I'm trying to recreate the functionality of the filterby and orderby filters.

I have both filters working separately, but when I try to combine them together I'm running into issues.

Relevant template section:

<tr class="vu-row" v-for="item in tableFilter">
  <td v-for="head in columns">{{item[head[".key"]]}}</td>
</tr>

Relevant script sections:

import { mapGetters } from 'vuex'
import db from './db'

export default {
  firebase: {
    sections: db.ref('HE'),
    columns: db.ref('Columns')
  },
  computed: {
    ...mapGetters({
      query: 'queryGet',
      sortkey: 'sortkeyGet',
      sortorders: 'sortOrdersGet'
    }),
    tableFilter: function () {
      return this.orderBy(function () {
        return this.findBy(this.sections, this.query, 'Designation')
      }, this.sortorders[this.sortkey], this.sortkey)
    }
  },
  methods: {
    findBy: function (list, value, column) {
      return list.filter(function (item) {
        return item[column].includes(value)
      })
    },
    orderBy: function (list, order, column) {
      return list.sort(function (a, b) {
        return order * (a[column] - b[column])
      })
    }
  },
........(more irrelevant code)
}

The sections data consists of an array of objects that I'm trying to loop through and filter.

The issue is with the tableFilter computed property. I don't know how to properly chain the 2 findBy and orderBy functions. Each filter works on its own when I call them individually.

Any help would be appreciated. Thanks

Edit

For reference I created these fiddles. Here is a fiddle with working search (search only searches the first column, and it is case sensitive), and here is one with working sort when clicking on table headers (although sorting by the first column isn't working for some reason).

Here is a fiddle where I try to chain the 2 functions. I can't seem to get it to run.

4
  • How did you try to chain them? Commented Nov 29, 2016 at 7:05
  • Also note that "[...] filters are primarily designed for text transformation purposes. For more complex data transforms in other directives, you should use Computed properties instead." (Source) Commented Nov 29, 2016 at 7:40
  • Sorry, I should have been more clear - I am using a computed property. The tableFilter computed property in my above code is where I try to chain my 2 methods (findBy and orderBy) Commented Nov 29, 2016 at 13:06
  • Hi! Thanks for explanations and illustration. However, considering such functions have been deprecated, did you find what the devs now consider as the way to do such functions, using v-for only? Commented Apr 16, 2018 at 9:10

1 Answer 1

1

Fixed it. The mass miss typo and some other small stuff. Sorting numbers, of course not sorting strings.

check code in JSFiddle https://jsfiddle.net/62u2e4bj/

Hope it helps!

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, to be clear, I am using a computed property. The tableFilter computed property in my above code is where I try to chain my 2 functions (findBy and orderBy). I guess my problem is I don't know how to properly combine the 2 functions.
Ah, yes. Can you create JSBin/Fiddle for that? I will then try to help
Ok, this took longer than it should have because my actual app uses single file components. Here is a fiddle with working search, and here is one with working sort when clicking on table headers. So I'm looking chain these two functions someonw. Obviously, the way I tried in my original post doesn't work.
Let's start from the begining. First of all - you have misstype in sortOrders. Keep camelCase always. That's first to fix.
You got it, thanks. I swear I tried that solution at some point, but I must have done something wrong. Also, in case anyone else is wondering why sorting the first column doesn't work, I just needed to replace this line in the orderBy function: return order * (a[column] - b[column]) with this: return order * (a[column] < b[column])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.