Updating a filtered multi-row Vuex backed form results in "do not mutate vuex store state" - vuex

I have a multi row form in Nuxt/Vuex that I am successfully CRUDing rows. I can create, updated, and delete rows. Sandbox here.
https://codesandbox.io/s/multirow-fields-hkzql
I have another form where I am filtering the rows based on a value in the row. I can create and delete rows but not update them. Sandbox here.
https://codesandbox.io/s/testing-filtered-multirow-fields-24cxb
When I edit a field in the filtered table I get the [vuex] do not mutate vuex store state outside mutation handlers error.
// store/Table.js snippet
export const getters = {
getField,
filteredRows: state => {
return state.rows.filter(row => row.key > 1);
}
};
<!-- component/MultiRow.vue snippet -->
<tr v-for="row in filteredRows" :key="row.key">
<td>
<input v-model="row.key">
</td>
<td>
<input v-model="row.value">
</td>
<td>
<button class="btn" #click="removeRow(row)">Remove row</button>
</td>
</tr>
Is there a solution in vuex-map-fields? Is there a solution without it?

Your are using Vuex in strict mode, so you cannot update the property of an object from the state outside a mutation, as explained here: https://vuex.vuejs.org/guide/forms.html
To solve your issue, firstcreate a new mutation method:
updateRow(state, row) {
const index = state.rows.findIndex(i => i.key === row.key);
if (index > -1) {
state.rows[index] = row;
}
}
Then replace your v-model by a :value + #inputin order to update the "row" value with this new mutation method:
<tbody>
<tr v-for="row in filteredRows" :key="row.key">
<td>
<input :value="row.key" #input="updateRow(row)">
</td>
<td>
<input :value="row.key" #input="updateRow(row)">
</td>
<td>
<button class="btn" #click="removeRow(row)">Remove row</button>
</td>
</tr>
</tbody>
And update your method list:
methods: {
...mapMutations("Table", ["addRow", "removeRow", "updateRow"])
}

Related

Trying to not show v-model change until I want to show it

Okay so I have a table and the table has a number display and an Input for the user to increase the number up and down.
<tr v-for="(obj, index) in OBJECTARRAY" :key="obj.key">
<td>{{OBJOTHERNUMBER- OBJNUMBER}}</td>
<td>
<input v-model="OBJNUMBER" type="number">
</td>
</tr>
I don't want this change to show dynamically with a v-model but with a button click that updates the change?
What you could do is use 2 variables.
DiscountPriceTemp
DiscountPrice
And when you click on the button, you update DiscountPrice with DiscountPriceTemp.
<tbody>
<tr v-for="(obj, index) in OBJECTARRAY" :key="obj.key">
<td>{{ obj.SalePrice - obj.DiscountPrice }}</td>
<td>
<input v-model="obj.DiscountPriceTemp" type="number">
</td>
</tr>
</tbody>
export default {
methods: {
onButtonClick (obj) {
obj.DiscountPrice = obj.DiscountPriceTemp
}
}
}
You can use a ref to access the input value and set the discountPrice when the update button is clicked. In Vue it's best practice to not access the document directly. Not sure if you want a button for each row or a bulk update so I've included both. Here's the codepen if you want to see it in action https://codepen.io/madison_at_vrume/pen/mdWNrMy
<div>
<tbody>
<tr v-for="(obj, index) in prices" :key="obj.key">
<td>{{ obj.salePrice - obj.discountPrice }}</td>
<td>
<input
:ref="`discountInput-${index}"
:value="obj.discountPrice"
type="number"
>
</td>
<td>
<button #click="updatePrice(index)">Update Price</button>
</td>
</tr>
</tbody>
<button #click="updateAllPrices()">Update Prices</button>
</div>
methods: {
updatePrice(index) {
const newDiscountPrice = this.$refs[`discountInput-${index}`].value;
this.prices[index].discountPrice = newDiscountPrice;
},
updateAllPrices() {
this.prices.forEach((obj, index) => {
this.updatePrice(index);
})
},
}

The v-model in the view is not updated until after reloading the page for the first time

Because when updating the value of a variable in the method, it is not updated in the v-model of the view ?.
If I reload the page the method v-on: change ="reCalcTotal(index, art.stock_solicited)" works correctly, and updates the subtotal value every time "art.stock_solicited" changes.
But until I reload the page for the first time, the method does not modify the v-model in the view.
What alternative do I have to solve it?
HTML
<tr v-for="(art, index) in arGeneratedOrders" v-if="art.stock_solicited > 0">
<td>
$#{{ art.subtotal.toFixed(1) }}
<input type="text" v-model="art.subtotal">
</td>
<td>
<input type="number" v-model="art.stock_solicited" v-on:change="reCalcTotal(index, art.stock_solicited)">
</td>
</tr>
<tr>
<td>
Total: $#{{total.toFixed(2)}}
</td>
</tr>
js
data: {
arGeneratedOrders:''
},
computed: {
total(){
let total=0;
this.arGeneratedOrders.forEach(item => {
total += item.subtotal;
});
return total
}
},
methods: {
reCalcTotal: function(index, newStockSolicited) {
this.arGeneratedOrders[index].stock_solicited=newStockSolicited;
this.arGeneratedOrders[index].subtotal=(this.arGeneratedOrders[index].stock_solicited)*this.arGeneratedOrders[index].purchase_price,
localStorage.setItem('lsGeneratedOrders', JSON.stringify(this.arGeneratedOrders));
this.total;
alert('Index:'+index+' - New:'+newStockSolicited+' - ='+this.arGeneratedOrders[index].subtotal); //This alert shows the updated variables, but the values do not change in the view.
}
}

vue.js does not correctly rerender compared to the vue instance object

I have a small issue with my vue template. The code is the following :
<template>
<div class="panel panel-default"
v-bind:id="'panel_'+noeud.id">
<div class="panel-heading">{{noeud.name}}</div>
<div class="panel-body">
<table class="table">
<thead>
<tr>
<th>Noeud</th>
<th>Poid</th>
</tr>
</thead>
<tbody>
<tr
v-for="noeud_poids in weightSorted"
v-if="noeud_poids.macro_zonning_noeud_id_2 != noeud.id"
is="macrozonningproximitenoeudpoids"
:noeud_poids="noeud_poids"
:noeud="noeud"
:noeuds="noeuds"
:delete_callback="delete_final"
:change_callback="update_line">
</tr>
<tr>
<td>
<select v-model="new_noeud">
<option value=""></option>
<option v-for="one_noeud in noeuds "
v-bind:value="one_noeud.id">{{one_noeud.name}}</option>
</select>
</td>
<td>
<input type="text" v-model="new_weight">
</td>
<td>
<input type="button" class="btn btn-primary" #click="addNoeudProximite" value="Ajouter"/>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</template>
<script>
export default {
props: ['pnoeud', 'pnoeuds'],
data: function(){
return {
points: 0,
points_restants: 100,
new_weight:0,
new_noeud:0,
noeud:this.pnoeud,
noeuds:this.pnoeuds,
weightSorted:this.pnoeud.weightSorted
}
},
mounted() {
},
methods:{
delete_final(macro_zonning_noeud_id_2){
axios.delete("/macrozonning/proximite/",{
params:{
macro_zonning_noeud_id_2:macro_zonning_noeud_id_2,
macro_zonning_noeud_id_1:this.noeud.id
}
}).then((res) => {
Vue.delete(this.weightSorted, String(macro_zonning_noeud_id_2));
})
},
update_line(nb_points){
this.points_restants = this.points_restants - nb_points;
this.points = this.points + nb_points;
},
addNoeudProximite(){
axios.put('/macrozonning/proximite/', {
'macro_zonning_noeud_id_1': this.noeud.id,
'macro_zonning_noeud_id_2': this.new_noeud,
'weight': this.new_weight
}).then((res) => {
Vue.set(this.weightSorted, String(this.new_noeud), res.data);
});
}
}
}
</script>
When the function delete_final is executed on the last item of my list, the view is correctly rerendered as the last item of my list is removed. But when I try to remove the first item of my list then the view rerenders but the the last item has been removed. When I check the Vue object in devtools, it does not reflect the new view, but it reflects the action taken (my first item has been removed).
If you have any idea where this problem comes from it would be awesome.
Thanks a lot community
Use a key attribute on the element you are rendering with v-for so that vue can exactly identify VNodes when diffing the new list of nodes against the old list. See key attribute
<tr> v-for="noeud_poids in weightSorted" :key="noeud_poids.id" </tr>

Child component not updating when data chaining - why does :key need to be the value that changes?

I had a table row that was like this:
<tr v-for="(pricing, idx) in pricings"">
<td>{{pricing.description}}</td>
<td>$ {{pricing.unconfirmed_price_formatted}}
</tr>
I wanted to migrate this table row to a component. However, when I change the underlying data (this.pricings), the child component doesn't update. I am calling it like this:
<pricing-row v-for="(pricing, idx) in pricings" :key="pricing.id + pricing.description" :pricing=pricing v-on:updateAfterSave="savedData" v-on:showModal="showAddEditModal"></pricing-row>
The strange thing is that the underlying array is changing - just this component is not properly updating.
It's also clear that if we use as a key the value that changes (unconfirmed_price_formatted in this case), it does update.
I'm a bit baffled by this behavior. Any ideas on what is wrong?
edit 1
here is the component:
<template>
<tr>
<td>{{localPricing.description}}</td>
<td v-if="localPricing.price">$ {{localPricing.price_formatted}} {{localPricing.units_display}}</td>
<td v-else> </td>
<td v-if="localPricing.unconfirmed_price">$ {{localPricing.unconfirmed_price_formatted}} {{localPricing.unconfirmed_units_display}}</td>
<td v-else> </td>
<td v-if="localPricing.state != 'deleted'">
<!-- button class="btn btn-outline-secondary" #click="willShowModalWithBattery(pricing)">edit</button -->
<button class="btn btn-outline-secondary" #click="showModal(pricing)">edit</button>
</td>
<td v-else> </td>
</tr>
</template>
<script>
export default {
props: ['pricing'],
data: function(){
return {
localPricing: this.pricing
}
},
methods:{
showModal: function(pricing){
this.$emit('showModal', pricing);
}
}
}
</script>

How to handle variable number of process variables in Camunda

I'm new to Camunda and didn't find any tutorial or reference explaining how to achieve the following:
When starting a process I want the user to add as many items as he likes to an invoice. On the next user task all those items and their quantity should be printed to somebody approving the data.
I don't understand yet how to get this 1:n relationship between a process and its variables working. Do I need to start subprocesses for each item? Or do I have to use a custom Java object? If so, how can I map form elements to such an object from within the Tasklist?
I got it working with the help of the links provided by Thorben.
The trick is to use JSON process variables to store more complex data structures. I initialize such lists in my "Start Event". This can be done either in a form or in my case in a Listener:
execution.setVariable("items", Variables.objectValue(Arrays.asList(dummyItem)).serializationDataFormat("application/json").create());
Note that I added a dummyItem, as an empty list would lose its type information during serialization.
Next in my custom form I load this list and can add/remove items. Using the camForm callbacks one can persist the list.
<form role="form" name="form">
<script cam-script type="text/form-script">
/*<![CDATA[*/
$scope.items = [];
$scope.addItem = function() {
$scope.items.push({name: '', count: 0, price: 0.0});
};
$scope.removeItem = function(index) {
$scope.items.splice(index, 1);
};
camForm.on('form-loaded', function() {
camForm.variableManager.fetchVariable('items');
});
// variables-fetched is not working with "saved" forms, so we need to use variables-restored, which gets called after variables-fetched
camForm.on('variables-restored', function() {
$scope.items = camForm.variableManager.variableValue('items');
});
camForm.on('store', function() {
camForm.variableManager.variableValue('items', $scope.items);
});
/*]]>*/
</script>
<table class="table">
<thead>
<tr><th>Name</th><th>Count</th><th>Price</th><th></th></tr>
</thead>
<tbody>
<tr ng-repeat="i in items">
<td><input type="text" ng-model="i.name"/></td>
<td><input type="number" ng-model="i.count"/></td>
<td><input type="number" ng-model="i.price"/></td>
<td>
<button class="btn btn-default" ng-click="removeItem($index)">
<span class="glyphicon glyphicon-minus"></span>
</button>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="4">
<button class="btn btn-default" ng-click="addItem()">
<span class="glyphicon glyphicon-plus"></span>
</button>
</td>
</tr>
</tfoot>
</table>
</form>
Two things aren't working yet:
Field validation, e.g. for number fields
The dirty flag used on the "save" button doesn't get updated, when adding/removing rows