【问题标题】:Vue.js Sorting using a underscore sortByVue.js 使用下划线 sortBy 进行排序
【发布时间】:2017-01-04 15:49:07
【问题描述】:

尝试在 Vue 组件上实现 sortBy 方法。这是我到目前为止所拥有的。

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);
        }
    },
}

我可以看到 sortBy 函数正在被命中,并且参数 sortKey 正在从模板正确发送,但问题似乎在于 _.sortBy 函数对 @987654323 没有任何影响@ 大批。

任何想法为什么它不起作用。
我已经尝试使用 Underscores 示例数组的 _.sortBy 方法,它工作正常,但我认为当数组添加到 Vue 组件上的数据时,可能会阻止此函数正常工作。

【问题讨论】:

    标签: sorting underscore.js vue-component vuejs2


    【解决方案1】:

    我看到_.sortBy 工作,看到小提琴here

    代码如下:

    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');
          }
        }
    })
    

    从你的 repo 中添加更多代码,如果你仍然看到这还没有解决。

    【讨论】:

    • 谢谢 - 但看起来上面的代码与我使用的代码完全相同,但有一个例外。我对 Vue 组件而不是 Vue 对象本身的数据进行排序。我原以为这可能与数据数组有关 - 但我已经为组件提供了上面的数组,并在 _.sortBy 函数之前和之后检查了数组,但数组没有变化
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-09
    • 2013-09-23
    • 2023-04-04
    相关资源
    最近更新 更多