简体   繁体   English

在 VueJS 中对我的 API 数据进行数字(0-9)排序

[英]Sorting my API data numerically (0-9) in VueJS

I have data mapped from an API (see below) which I am reaching fine but I am looking at sorting it numerically (0-9).我有从 API (见下文)映射的数据,我已经达到了很好,但我正在考虑对它进行数字排序(0-9)。 I'm having a hard time doing this with Vue.我很难用 Vue 做到这一点。 If I had my data static in the data(){...} , I can get it done a number of ways.如果我在data(){...}中有我的数据 static ,我可以通过多种方式完成它。 But not from an API because I can't point to the API for some reason whenever I try to point to it from a function in my methods.但不是来自 API,因为每当我尝试在我的方法中从 function 指向它时,出于某种原因我无法指向 API。 I have no idea what is going on and I'm hoping you guys have some direction.我不知道发生了什么,我希望你们有一些方向。

The template - Because of the nesting of the API, I am nesting loops as well.模板 - 由于 API 的嵌套,我也在嵌套循环。 (maybe there is a better way to also do this. I'm all ears). (也许还有更好的方法可以做到这一点。我全神贯注)。 allBatches is my Getter. allBatches是我的吸气剂。 I am serving the API through my State Manager (Vuex)我通过我的 State Manager (Vuex) 为 API 提供服务

<div>
  <div v-for="batches in allBatches" :key="batches.id">
     <div 
        v-for="dispatchstation in batches.dispatchstation" 
        :key="dispatchstation.id">
        <div v-for="apps in dispatchstation.applications" :key="apps.id">
          <div>{{apps}}</div>
        </div>
     </div>
  </div>
</div>

The data structure in the API - I intentionally left unrelated data out. API 中的数据结构——我故意遗漏了不相关的数据。 There are other layers in between.中间还有其他层。 But this shows the path I am looping and reaching out to.但这显示了我正在循环和接触的路径。

"batches": [
{
  "dispatchstation": [
    {
      "applications": [
        "384752387450",
        "456345634563",
        "345634563456",
        "567845362334",
        "567456745677",
        "456734562457",
        "789676545365",
        "456456445556",
        "224563456345",
        "456878656467",
        "053452345344",
        "045440545455",
        "045454545204",
        "000014546546",
        "032116876846",
        "546521302151",
        "035649874877",
        "986765151231",
        "653468463854",
        "653853121324",
        "000145456555"
      ]
    }
  ]
}

], ],

I've tried to do this with lodash using _.orderBy and use it a pipe.我尝试使用_.orderBy对 lodash 执行此操作,并将其用作 pipe。 No luck.没运气。 And I also tried this:我也试过这个:

data() {
  return {
    sortAsc: true,
    sortApps: "" // see explanation
  };
},
computed: {
  ...mapGetters(["allBatches"]),
  sortedData() {
    let result = this.sortApps;

    let ascDesc = this.sortAsc ? 1 : -1;
    return result.sort(
      (a, b) => ascDesc * a.applications.localeCompare(b.applications)
    );
  }
},

I used (tried) this method by giving sortApps the loop criteria dispatchstations.applications and loop v-for='apps in sortedData' .我通过给 sortApps 循环条件dispatchstations.applications和循环v-for='apps in sortedData'使用(尝试)这种方法。 I'm sure that is wrong.我确定那是错误的。 Vue is not really my forte. Vue 并不是我的强项。

I really don't have any preference on how this should be as long as the data renders sorted out numerically ASC.只要数据呈现以数字 ASC 排序,我真的没有任何偏好。

Any thoughts?有什么想法吗?

Thanks谢谢

EDIT编辑

Using Chase's answer, I am still getting the data through but it doesn't display.使用 Chase 的回答,我仍在获取数据,但没有显示。 I had to remove the negation ( ! ).我不得不删除否定( ! )。

Mutation and getters of State view from the vue chrome tool从 vue chrome 工具查看 State 的突变和吸气剂

来自 vue chrome 工具的状态视图突变

在此处输入图像描述

EDIT 2 - A simplified version of my store module编辑 2 - 我的商店模块的简化版本

  import axios from "axios";

  const state = {
    batches: [],
  };

  const getters = {
    allBatches: state => {
      return state.batches;
    },
  };

  const actions = {
    async fetchBatches({ commit }) {
      const response = await axios.get(`${window.location.protocol}//${window.location.hostname}:4000/batches`);

      commit("setBatches", response.data);
    },
  };

  const mutations = {
    setBatches: (state, batches) => (state.batches = batches),
  };

  export default {
    state,
    getters,
    actions,
    mutations
  };

Hopefully I'm not misunderstanding your question, but essentially I would recommend loading your data in the same way that you are currently and handling the sort in a computed method.希望我没有误解您的问题,但基本上我会建议您以与当前相同的方式加载数据并在计算方法中处理排序。

This is assuming that the length of batches and dispatchstation will always be 1.这是假设批次和调度站的长度始终为 1。

 new Vue({ el: "#app", data: { allBatches: null }, computed: { batchesSorted() { if (.this.allBatches) return {} const output = this.allBatches.batches[0].dispatchstation[0];applications. output,sort((a, b) => { return parseInt(a) - parseInt(b) }) return output } }. async created() { // Equivelent to...mapGetters(["allBatches"]) for the example this:allBatches = { "batches": [{ "dispatchstation": [{ "applications", [ "384752387450", "456345634563", "345634563456", "567845362334", "567456745677", "456734562457", "789676545365", "456456445556", "224563456345", "456878656467", "053452345344", "045440545455", "045454545204", "000014546546", "032116876846", "546521302151", "035649874877", "986765151231", "653468463854", "653853121324", "000145456555" ] }] }] } } })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <div v-for="(item, key) in batchesSorted":key="key"> {{ item }} </div> </div>

Let me know if I misunderstood anything or if you have any questions.让我知道我是否误解了任何内容或您有任何问题。

I hope that I understood your question, so you just need to order the data to render it and you don't need it as ordered in your store?我希望我理解你的问题,所以你只需要订购数据来呈现它,你不需要在你的商店订购它吗?

to display orderd data you can use this computed function, I hope it will help you要显示有序数据,您可以使用这个计算出来的 function,希望对您有所帮助


computed:{

      ...mapGetters(["allBatches"]),

      orderApplications(){

        let copieBatches = JSON.parse(JSON.stringify(this.allBatches));

        copieBatches.forEach(batches => {

          batches.dispatchstation.forEach(dispatchstation=>{

            dispatchstation.applications.sort()

          })

        });

        return copieBatches
      }
}

and your HTML will be like你的 HTML 会像

<div>
   <div v-for="batches in orderApplications">
      <div
         v-for="dispatchstation in batches.dispatchstation"
         :key="dispatchstation.id">
             <div v-for="apps in dispatchstation.applications">
                <div>{{apps}}</div>
              </div>
       </div>
   </div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM