Cannot set a checkbox unchecked once it has been checked against the data with Vue - vue.js

I am desparately trying to understand what's wrong there and can't figure it out!
I'll try to explain as best as I can, but the code is lengthy and I can't post it easily.
I have a component called "FrameHeader" that includes an input checkbox. This component is called in another component called "Frame", and the frames made from a v-for in another component ("FrameContainer").
In FrameHeader template, I have this:
<input :key="'frame-touchselectbox-'+frameId" type="checkbox"
v-model="touchSelected"
:class="{hidden: !isTouchSelectOn, selectCheckBox: true}"
/>
touchSelected is a computed property defined as such:
computed: {
touchSelected() {
console.log("checking frame touch selected for frame " + this.frameId + " ==> " + store.getters.isFrameTouchSelected(this.frameId));
return store.getters.isFrameTouchSelected(this.frameId);
},
},
where store.getters.isFrameTouchSelected(this.frameId); retrieves a boolean property called "touchSelected" in an object of the state:
The idea is that in the scenario I have, all "touchSelected" properties are first set to false (A), then only the one from one of the frame is set to true (B).
(A):
toggleTouchSelect(state, payload: {frameId: number; toggle: boolean}) {
const newCandidates: number[] = (payload.toggle) ? getAllSiblings(state.frameObjects, payload.frameId): [];
Object.keys(state.frameObjects).forEach((frameKey) => {
Vue.set(
state.frameObjects[parseInt(frameKey)],
"touchSelected",
false
);
Vue.set(
state.frameObjects[parseInt(frameKey)],
"isTouchSelectOn",
newCandidates.includes(parseInt(frameKey))
);
});
},
(B):
touchSelectFrame(state, payload: {frameId: number; isSelected: boolean}) {
Vue.set(
state.frameObjects[payload.frameId],
"touchSelected",
payload.isSelected
);
},
The data I get in the store is correct, I get false/true values where I want them.
However, the checkboxes are not correct. The first time I set one of the frame's property to "true", the corresponding checkbox gets checked. But when I get another frame's property to "true", the previous frame's checkbox doesn't get unchecked. Actually, I see it first being unchecked, then being checked again.
As I said, the data in the state is correct: even when that checkbox revert to checked, the underlying property in the data for that frame is "false".
BUT the weirdest thing is that if only change the checkbox input to a text input (changing the type of the input in the template), the text value is always correct even after the second time I set a frame's property to "true".
So...i'm totally puzzled and can't understand what's happening with those checkboxes.
Sorry for the vague explanation I hope it can still be understandable and that someone will shed a light on that :) Thanks a lot.

Computed props are by default getter-only (reference). That means your checkbox can read the value of touchSelected but can't change its value. You have to use a computed prop with a getter AND a setter. Assuming you have a mutation to change your frameId logic in Vuex:
computed: {
touchSelected: {
get(): {
return store.getters.isFrameTouchSelected(this.frameId);
}
set(newValue): {
store.commit('FRAME_MUTATION', newValue);
}
}
},

Related

VUE b-editable-table data model is returning old value upon editing

I am very new to the VUE framework. The b-editable-table model is not returning the current value but the old value.
Here's the bootstrap code.
<b-editable-table disableDefaultEdit
:rowUpdate="rowUpdate"
:editMode="'row'"
bordered
class="editable-table"
v-model="items"
:fields="fields"
striped>
<template #cell(edit)="data">
<div v-if="data.isEdit">
<BIconX class="edit-icon" #click="handleSubmit(data, false)"></BIconX>
<BIconCheck class="edit-icon"
#click="handleSubmit(data, true)"></BIconCheck>
</div>
<BIconPencil v-else
class="edit-icon"
#click="handleEdit(data, true)"></BIconPencil>
</template>
<template #cell(delete)="data">
<BIconTrash class="remove-icon"
#click="handleDelete(data)"></BIconTrash>
</template>
</b-editable-table>
Here's the Javascript Method handleSubmit:
The current value of the productPrice 11.63 but when I console data.item.productPrice, it returns me 11.25 which is an old value.
This is a result of console.log showing objects as 'live' data vs individual data properties that will log their value at the time of the actual console.log statement. This is further explained in the MDN documentation for console.log.
Don't use console.log(obj), use console.log(JSON.parse(JSON.stringify(obj))).
This way you are sure you are seeing the value of obj at the moment you log it. Otherwise, many browsers provide a live view that constantly updates as values change. This may not be what you want.
Technically your first console.log should be showing the old productPrice value for some (extremely) short amount of time, same as your other console logs, but then the log updates itself to show the new value almost instantaneously.
To get the updated value of a table cell
Because of the way b-editable-table is designed, we can't get the updated cell value from an event handler on a cell v-slot that isn't the cell being updated. One way to do what you want is to listen to the input-change event emitted from the table itself. This passes a data variable that contains the updated values of whatever row was just updated. From this you can assign the updated row data to a new data property on your component. This property will have the information you need when a user clicks the icon in your #cell(edit) slot
<b-editable-table
bordered
class="editable-table"
v-model="items"
:fields="fields"
#input-change="handleInput"
>
data() {
return {
fields: [ ... ],
items: [ ... ],
updatedRow: {} // <-- new property to hold updated row data
},
methods: {
handleInput(data) {
if (Object.keys(this.updatedRow).length === 0) {
this.updatedRow = {
...this.items[data.index],
[data.field.key]: data.value,
};
} else {
this.updatedRow = { ...this.updatedRow, [data.field.key]: data.value };
}
},
// method called by `#cell(edit)` slot
handleSubmit(data, update) {
this.rowUpdate = {
edit: false,
id: data.id,
action: update ? "update" : "cancel",
};
console.log("this.updatedRow.productPrice", this.updatedRow.productPrice); // <-- shows latest productPrice
this.updatedRow = {}
},
}
An explanation for:
this.updatedRow = {...this.items[data.index], [data.field.key]: data.value};
This line exists because the data object passed in by the event contains only pieces of the updated value that we still must manually construct into a row object. This is the format of the passed in data object (unimportant properties removed):
{
index: 0
field: {
key: "productPrice"
label: "Product Price"
...
}
value: "11.63"
...
}
So we have these properties:
index (the row updated)
field.key (the cell updated)
value (the new cell value)
At this point, we still have nothing giving us the full updated row object.
Our v-model this.items is still not updated! We have to construct this updated row object ourselves. The code that does this uses object destructing.
{ ...this.items[data.index] } gives us all the current (old) key-values of this.items[row]
adding [data.field.key]: data.value as another property will update the relevant key-value pair that was just updated by the user.
together, this.updatedRow = {...this.items[data.index], [data.field.key]: data.value}; gives us the row from this.items but with the absolute latest values from the row that was just updated by the user.

Vue-Native checkbox change value

I want to be able to change the value of a checkbox by clicking on it. recentContacts are loading just fine, and specifying initial checked values in the computed function works well. The :on-press seems to change the value but does not reflect in the UI.
Please Help
Template
<nb-list>
<nb-list-item v-for="contact in recentContacts" v-bind:key="contact.uid">
<nb-checkbox :on-press="() => contact.checked =! contact.checked" :checked="contact.checked"></nb-checkbox>
<nb-text>{{contact.firstName}} {{contact.lastName}}</nb-text>
</nb-list-item>
</nb-list>
Code
export default {
computed: {
recentContacts() {
return store.state.admin.userData.recentContacts.map(rc => {
rc.checked = false;
return rc;
});
}
},
}
EDIT:
I am guessing because VUEX is imutable. I've got this to work by having recentContacts inside of the data attribute instead of computed just not how I want to do things.

Input field not reacting to data changes after being written to by a user

While creating a Vue.js application I have become stuck at a weird problem. I want to be able to manipulate an input field (think increment and decrement buttons and erasing a zero value on focus, so the user doesn't have to) and up until a user writes to the input field, everything is fine. After that, however, further changes in the data are no longer represented in the input field.
As I was sure I could not be the only one with this particular problem, I searched extensively, but had no luck. What baffles me the most is that everything works until the field is written to, since I can not really imagine why this would remove the data binding.
The following code should show the same behavior. It is an input field component, which is initialized with a zero value. On focus the zero gets removed. This works, until a user manually writes to the field after which zero values will no longer be removed, even though the focus method fires, the if-condition is met and the data in the amount-variable is changed.
Vue.component('item', {
data: function () {
return {
amount: 0
}
},
render: function (createElement) {
var self = this;
return createElement('input', {
attrs: {
//bind data to field
value: self.amount,
type: 'number'
},
on: {
//update data on input
input: function (event) {
self.amount = event.target.value;
},
//remove a zero value on focus for user convenience
focus: function (event) {
if (self.amount == 0 || self.amount == "0") {
self.amount = '';
}
}
}
})
}
})
I think you need to use domProps instead of attrs to make it reactive. But I would suggest you use vue's template syntax or if you insist on using the render function I would also suggest you to use JSX.

How to prevent #change event when changing v-model value

I'm building an auto-complete menu in Vue.js backed by Firebase (using vue-fire). The aim is to start typing a user's display name and having match records show up in the list of divs below.
The template looks like this:
<b-form-input id="toUser"
type="text"
v-model="selectedTo"
#change="searcher">
</b-form-input>
<div v-on:click="selectToUser(user)" class="userSearchDropDownResult" v-for="user in searchResult" v-if="showSearcherDropdown">{{ user.name }}</div>
Upon clicking a potential match the intention is to set the value of the field and clear away the list of matches.
Here is the code portion of the component:
computed: {
/* method borrowed from Reddit user imGnarly: https://www.reddit.com/r/vuejs/comments/63w65c/client_side_autocomplete_search_with_vuejs/ */
searcher() {
let self = this;
let holder = [];
let rx = new RegExp(this.selectedTo, 'i');
this.users.forEach(function (val, key) {
if (rx.test(val.name) || rx.test(val.email)) {
let obj = {}
obj = val;
holder.push(obj);
} else {
self.searchResult = 'No matches found';
}
})
this.searchResult = holder;
return this.selectedTo;
},
showSearcherDropdown() {
if(this.searchResult == null) return false;
if(this.selectedTo === '') return false;
return true;
}
},
methods: {
selectToUser: function( user ) {
this.newMessage.to = user['.key'];
this.selectedTo = user.name;
this.searchResult = null;
}
}
Typeahead works well, on each change to the input field the searcher() function is called and populates the searchResult with the correct values. The v-for works and a list of divs is shown.
Upon clicking a div, I call selectToUser( user ). This correctly reports details from the user object to the console.
However, on first click I get an exception in the console and the divs don't clear away (I expect them to disappear because I'm setting searchResults to null).
[Vue warn]: Error in event handler for "change": "TypeError: fns.apply is not a function"
found in
---> <BFormInput>
<BFormGroup>
<BTab>
TypeError: fns.apply is not a function
at VueComponent.invoker (vue.esm.js?efeb:2004)
at VueComponent.Vue.$emit (vue.esm.js?efeb:2515)
at VueComponent.onChange (form-input.js?1465:138)
at boundFn (vue.esm.js?efeb:190)
at invoker (vue.esm.js?efeb:2004)
at HTMLInputElement.fn._withTask.fn._withTask (vue.esm.js?efeb:1802)
If I click the div a second time then there's no error, the input value is set and the divs disappear.
So I suspect that writing a value to this.selectedTo (which is also the v-model object for the element is triggering a #change event. On the second click the value of doesn't actually change because it's already set, so no call to searcher() and no error.
I've noticed this also happens if the element loses focus.
Question: how to prevent an #change event when changing v-model value via a method?
(other info: according to package.json I'm on vue 2.5.2)
On:
<b-form-input id="toUser"
type="text"
v-model="selectedTo"
#change="searcher">
The "searcher" should be a method. A method that will be called whenever that b-component issues a change event.
But looking at your code, it is not a method, but a computed:
computed: {
searcher() {
...
},
showSearcherDropdown() {
...
}
},
methods: {
selectToUser: function( user ) {
...
}
}
So when the change event happens, it tries to call something that is not a method (or, in other words, it tries to call a method that doesn't exist). That's why you get the error.
Now, since what you actually want is to update searcher whenever this.selectedTo changes, to get that, it is actually not needed to have that #change handler. This is due to the code of computed: { searcher() { already depending on this.selectedTo. Whenever this.selectedTo changes, Vue will calculate searcher again.
Solution: simply remove #change="searcher" from b-form. Everything else will work.
#acdcjunior, thanks for your answer.
Of course just removing the reference to searcher() just means no action is taken upon field value change so the field won’t work at all.
Moving the searcher() function into methods: {} instead of computed: {} means that it will be called on an input event and not a change even (another mystery but not one for today). A subtle difference that takes away the typeahead feature I’m aiming at.
However, it did make me remember that the result of computed: {} functions are cached and will be re-computed when any parameters change. In this case I realised that the searcher() function is dependent upon the this.selectedTo variable. So when the selectToUser() function sets this.selectedTo it triggers another call to searcher().
Fixed now. In case anyone has a similar problem in the future, I resolved this by turning to old fashioned semaphore by adding another variable.
var userMadeSelection: false
Now, searcher() begins with a check for this scenario:
computed: {
searcher() {
if(this.userMadeSelection) {
this.userMadeSelection = false;
return this.selectedTo;
}
…
and then in selectToUser():
this.userMadeSelection = true;

element not updated when data changes

I have a vaadin-checkbox:
<vaadin-checkbox id=[[item.id]] disabled="true" checked="[[item.checked]]">[[item.description]]</vaadin-checkbox>
I defined my properties:
static get properties() {
return {
items: {
type: Array,
notify: true,
value: function() {
return [];
}
}
};
}
When I now change the data by pressing some button:
_selectItem(event) {
const item = event.model.item;
if (item.checked === true) {
this.$.grid.deselectItem(item);
} else {
this.$.grid.selectItem(item);
}
item.checked = !item.checked;
}
The state of the checkbox is still checked="true". Why isnt the checkbox getting updated? The same thing when I change the description of the item:
_selectItem(event) {
event.model.item.description = 'test';
}
The test description is never appearing. The checkbox is never getting updated.
The reason why the checkbox does not get updated by the button click handler is in the Polymer 2 data system. Polymer does not detect the change and does not update the template accordingly.
In order to make the change in a way that Polymer would detect it you have two options:
Use this.set(`items.${event.model.index}.checked`, !item.checked) if you can reliably assume that the index used by dom-repeat always matches that elements's index in the items array (it is not the case if you use sorting or filtering features of dom-repeat). See an example here https://jsfiddle.net/vlukashov/epd0dn2j/
If you do not know the index of the updated item in the items array, you can also use the Polymer.MutableData mixin and notify Polymer that something has changed inside the items array without specifying the index of the changed item. This is done by calling this.notifyPath('items') after making a change. However, this requires that your element extends the Polymer.MutableData mixin, and that dom-repeat has the mutable-data attribute set. See an example here: https://jsfiddle.net/vlukashov/epd0dn2j/24/
More information on this in the Polymer 2 docs.