首页 > 解决方案 > 为什么“_.orderBy”函数不对结果进行排序?

问题描述

我想做数据排序,但我不知道为什么它不起作用。我已经为此苦苦挣扎了几个小时,但我仍然不知道出了什么问题。有人能告诉我为什么“_.orderBy”函数不对结果进行排序吗?

<template>
<div>
    <table>
        <thead>
            <tr>
                <th v-for="(column, index) in columns" :key="index">
                    <a href="#" @click="sortBy(index)">{{ column }}</a>
                </th>
            </tr>
        </thead>
        <tbody>
            <tr v-for="user in orderedUsers" :key="user.id">
                <td>{{ user.name }}</td>
                <td>{{ user.age }}</td>
            </tR>
        </tbody>
    </table>
</div>

<script>
    export default {
        data() {
            return {
                users: [
                    {name: 'Andrew', age: 100},
                    {name: 'Bella', age: 200},
                    {name: 'Cylerion', age: 300},
                ],
                columns: {
                    name: 'User Name',
                    age: 'User Age'
                },
                sortKey: 'name',
                reverse: false
            }
        },
        computed: {
            orderedUsers: function () {
                return _.orderBy(this.users, this.sortKey, this.reverse)
            }
        },
        methods: {
            sortBy: function(sortKey) {
                this.reverse = (this.sortKey == sortKey) ? ! this.reverse : false;

                this.sortKey = sortKey;
            },
        }
    }
</script>

我的图书馆:Lodash 4.6.0。

标签: sortinglodash

解决方案


推荐阅读