0

Got following .json:

[{
"STATUS": "0500",
"POSID": "..."
},
{
"STATUS": "1500",
"POSID": "..."
},
{
"STATUS": "0500",
"POSID": "..."
}]

Counted the values via countBy from lodash:

  countData (data) {
    return _.countBy(_.map(this.data, function (d) {
      return d.status
    }))
  }

A table is generated where my status numbers and counts are displayed:

 <tr v-for="(value, key) in countData">
   <td>{{ key }}</td>
   <td>{{ value }}</td>
 </tr>

Now I want to sort that via value.

How can I accomplish that?

2 Answers 2

1

Looks like countData is already a computed property, so if you need to keep it, you can refer to it from another computed property (as this.countData) which should provide the sorted version of the results for presentation.

For example,

computed: {
  countData () { ... },
  sortedCountData () {
    // this.countData looks something like: {'0500': 1, ...}
    return Object.keys(this.countData)
      .sort((i, j) => this.countData[j] - this.countData[i])
      .map(key => ({ key, value: this.countData[key] }));
  },
}

Then you should update your markup so the v-for sources from sortedCountData instead.

If you don't need to keep countData, you may as well do the sorting there.

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

2 Comments

Is it right that i have to name the tds: <td>{{ value.key }}</td> <td>{{ value.value }}</td> ?
Yes, that's right—since sortedCountData is a proper array and no longer a object (as returned by the lodash countBy call), each item v-for iterates over is also an object, so you'll need to index into the key and value properties.
0

you can sort the array before rendering. OR alternatively you can use css to do this.

<tbody style="display:flex;flex-direction:column"> 
// <tbody> or <table> if parent is <table> tag 
  <tr v-for="(value, key) in countData" style="order:{{value}}">
  <td>{{key}}</td>
  <td>{{value}}</td>
  </tr>
</tbody>

you need to give fixed width on td in case it gets distorted

1 Comment

- style="order:{{ value }}": Interpolation inside attributes has been removed. Use v-bind or the colon shorthand instead. For example, instead of <div style="{{ val }}">, use <div :style="val">. -> But wont work either.

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.