0

Trying to implement a sortBy method on a Vue component. Here's what I have so far.

var studentTableComp =  {
    template: '#studentTable',
    data: function () {
        return {
            students: data,
            show: true,
            columns: [
                'id',
                'candidateType',
                'complete'
            ]
        }
    },
    methods: {
        sortBy: function (sortKey, e) {
            e.preventDefault();
            _.sortBy(this.students.students, sortKey);
        }
    },
}

And I can see that the sortBy function is being hit, and that the parameters sortKey are being sent correctly from the template, but the problem seems to be with the _.sortBy function doesn't have any effect on the this.students.students array.

Any ideas why it doesn't working.
I've tried the _.sortBy method using Underscores example array, and it works fine, but I think something happens to the array when it is added to the data on the Vue component that might be preventing this function from working correctly.

1 Answer 1

0

I see _.sortBy working, see fiddle here.

Here is the code:

var demo = new Vue({
    el: '#demo',
    data: function(){
        return {
        students: [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}]
      };
    },
    methods: {
      sorted: function() {
        return _.sortBy(this.students, 'name');
      }
    }
})

Add more code from your repo, if you see still this is not resolved.

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

1 Comment

Thanks - but it looks like the above is the exact same code that I'm using with one exception. I have sorting data that is on a Vue Component rather than the Vue object itself. I had thought it might be something to do with the data array - but I've given the component the array you have above and checked the array before and after the _.sortBy function but there is no change to the array

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.