【问题标题】:VUEjs template computed array orderVUEjs 模板计算数组顺序
【发布时间】:2016-12-05 01:01:22
【问题描述】:

我有一个 VUE 模板,然后从 API 中获取一个数组,假设它是一个国家/地区列表。

现在根据我收到的 ID,我需要重新排序这个数组......

它可能看起来像这样:

Vue.component('select-list', {
template: '#select_list',
props: ['id', 'label', 'selected', 'list'],
computed: {
    // a computed getter
    computedList: function () {

        // I think I need to reorder the array (list) in here?

    }
}});

我的模板如下所示:

<template id="select_list">
<div class="nfw-row">
    <label :for="id">{{ label }}</label>
    <div>
        <select :name="id" :id="id" v-model="selected">
            <option value="">Please select...</option>
            <option :value="list_item" v-for="list_item in computedList">{{ list_item.name }}</option>
        </select>
    </div>
</div>
</template>

如果我需要订购我的清单,我是否走在正确的轨道上?假设条目 5、7、6 和 10 应该在我的数组顶部排序...

【问题讨论】:

  • 你在正确的轨道上,是的。究竟是什么问题?

标签: arrays sorting templates vue.js


【解决方案1】:

是的,这是执行此操作的方法。你可以使用类似 lodash 的东西:

computed: {
    computedList: function () {     
      return _.sortBy(this.list, [function(o) { return o.id; }]);
    }
}});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    • 2019-05-19
    • 2018-05-29
    相关资源
    最近更新 更多