I've built a Vue.js component, which fetches orders from a Woocommerce store. These orders contain the product variations from the products and are received as an object.
In the table, I need to format the object before it is displayed.
My code looks like this:
<template>
<div>
<vue-good-table
title=""
:columns="columns"
:rows="variationOrders"
:paginate="true"
:lineNumbers="true"/>
</div>
</template>
<script>
export default {
data: function() {
return {
variationOrders: [],
columns: [
{
label: 'Order#',
field: 'order_id',
filterable: true,
},
{
label: 'Customer',
field: 'customer_name',
//type: 'number',
html: false,
filterable: true,
},
{
label: 'QTY',
field: 'qty',
type: 'number',
//inputFormat: 'YYYYMMDD',
//outputFormat: 'MMM Do YY',
},
{
label: 'Product',
field: 'product_name',
//type: 'percentage',
html: false,
},
{
label: 'Variations',
field: this.formatVariations(self.variationOrders),
//type: 'percentage',
html: true,
},
{
label: 'Timeslot',
field: 'timeslot',
//type: 'percentage',
html: false,
},
{
label: 'Transportation',
field: 'store_id',
//type: 'percentage',
html: false,
},
],
}
},
methods: {
getTotals: function() {
var self = this;
var productId = document.getElementById('product-id').getAttribute('data-id');
axios.get('/api/v1/order_variations/' + productId)
.then(function (response) {
self.variationOrders = response.data.order_variations;
//console.log(response.data);
})
.catch(function(error) {
//
});
},
formatVariations: function(variationOrders) {
console.log(variationOrders);
},
},
mounted: function() {
this.getTotals();
// call the API every 30 seconds to fetch new orders
setInterval(function () {
this.getTotals();
}.bind(this), 5000);
}
}
</script>
On the Variations column I pass a function which is responsible for formatting the string. The function itself works fine, but I am unable to pass the the received object from the API to this function.
I get the following error message in the console:
If I pass
this.formatVariations(this.variationOrders)and console.log I getundefinedIf I pass
this.formatVariations(variationOrders)and console.log I get[Vue warn]: Error in data(): "ReferenceError: variationOrders is not defined"
I have a suspicion that at the time the function is called, that variable doesn't exist yet.
Is there anything I'm missing?
UPDATE 1
I've updated the code to the following. I'm closer but unfortunately the view isn't updating.
MY code looks like this:
<template>
<div>
<vue-good-table
title=""
:columns="formattedColumns"
:rows="variationOrders"
:paginate="true"
:lineNumbers="true"/>
</div>
</template>
<script>
export default {
data: function() {
return {
variationOrders: [],
columns: [
{
label: 'Order#',
field: 'order_id',
filterable: true,
},
{
label: 'Customer',
field: 'customer_name',
//type: 'number',
html: false,
filterable: true,
},
{
label: 'QTY',
field: 'qty',
type: 'number',
//inputFormat: 'YYYYMMDD',
//outputFormat: 'MMM Do YY',
},
{
label: 'Product',
field: 'product_name',
//type: 'percentage',
html: false,
},
{
label: 'Variations',
field: 'variation',
//type: 'percentage',
html: true,
},
{
label: 'Timeslot',
field: 'timeslot',
//type: 'percentage',
html: false,
},
{
label: 'Transportation',
field: 'store_id',
//type: 'percentage',
html: false,
},
],
}
},
methods: {
getTotals: function() {
var self = this;
var productId = document.getElementById('product-id').getAttribute('data-id');
axios.get('/api/v1/order_variations/' + productId)
.then(function (response) {
self.variationOrders = response.data.order_variations;
})
.catch(function(error) {
//
});
},
formatVariations: function(variationOrders) {
var variationsString = '';
variationOrders.forEach(function(item) {
var variations = JSON.parse(item.variation);
for(var i = 0; i < variations.length; i++) {
variationsString = variationsString + variations[i].key + ': ' + variations[i].value + '<br />';
}
});
return variationsString;
},
},
computed: {
formattedColumns(){
const formattedVariations = this.formatVariations(this.variationOrders);
console.log(formattedVariations);
return this.columns.map(c => {
if (c.label == "Variations") {
return {label: "Variations", field: formattedVariations , html: true}
}
return c;
})
}
},
mounted: function() {
this.getTotals();
// call the API every 30 seconds to fetch new orders
setInterval(function () {
this.getTotals();
}.bind(this), 5000);
},
}
</script>
Update 2
The formatVariations() function returns a sample set like this:
choose-your-cake: Naked<br />choose-sugar: Yes<br />choose-your-cake: Naked<br />choose-sugar: No<br />choose-your-cake: Naked<br />choose-sugar: No<br />choose-your-cake: Naked<br />choose-sugar: Yes<br />choose-your-cake: Naked<br />choose-sugar: Yes<br />choose-your-cake: Coated<br />choose-sugar: No<br />choose-your-cake: Naked<br />choose-sugar: Yes<br />choose-your-cake: Naked<br />choose-sugar: Yes<br />choose-your-cake: Coated<br />choose-sugar: Yes<br />choose-your-cake: Naked<br />choose-sugar: Yes<br />choose-your-cake: Naked<br />choose-sugar: Yes<br />choose-your-cake: Naked<br />choose-sugar: Yes<br />
Update 3
Here's one array item of what the API returns:
:
customer_name
:
(...)
order_id
:
(...)
product_name
:
(...)
qty
:
(...)
store_id
:
(...)
variation
:
"[{"id": 35, "key": "choose-your-cake", "value": "Naked"}, {"id": 36, "key": "choose-sugar", "value": "Yes"}]"