Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js : passing item as parameter to computed prop in v-for, returning child array sorted ?

Have been googling around for quite a while to figure out how to do this..

I have a list of "Intents", which each of them got a list of "Entities", presented in a nested v-for.

Intents are already computed, but i need to also sort the "Entities" list on the fly, so therefore i thought that making that list also computed..

Error :

**TypeError: _vm.entityList is not a function**

This is my current approach :

< script >
  import uniq from 'lodash/uniq'
import orderby from 'lodash/orderby'
import Util from "@/components/util.js"

export default {
  data() {
    return {
      // ....
    }
  },
  computed: {
    nluData() {
      return orderby(this.$store.getters.nlujson.filter(item => {
        return item.intent.toLowerCase() === this.selectedIntent
      }), ['intent', 'text'], ['asc', 'asc'])
    },
    entityList(item) {
      return orderby(item.entities, ['entity', 'value'], ['asc', 'asc'])
    },
  },
  created() {
    this.$store.dispatch('getNluJson')
  },
  methods: {
    // ......    
  }


  </script>
// parent structure
<div v-for="(item, key, index) in nluData">
  // displaying nluData content through item.mydata // child structure
  <div v-for="ent in entityList(item)">
    // displaying entities data through computed prop. // item.entities is the array
  </div>
</div>

{
			"id": "J4a9dGEBFtvEmO3Beq31",
			"text": "This is Intent 1",
			"intent": "shelf_life",
			"entities": [
				{
					"start": "33",
					"end": "44",
					"value": "fridge",
					"entity": "ingredient_placement"
				},
				{
					"start": "10",
					"end": "20",
					"value": "duration",
					"entity": "shelf_life"
				},
				{
					"start": "25",
					"end": "30",
					"value": "spareribs",
					"entity": "ingredient"
				}
			]
		},
like image 292
Terje Nygård Avatar asked Feb 08 '18 10:02

Terje Nygård


1 Answers

You can pass parameters to a computed property with an anonymous function like so:

computed: {
  entityList() {
    return (item) =>
      orderby(item.entities, ['entity', 'value'], ['asc', 'asc']);
    },
  },
like image 182
erica mitchell Avatar answered Oct 10 '22 04:10

erica mitchell