I would like to guarantee that at least one checkboxes are checked and the price are correct calculated.
https://jsfiddle.net/snoke/1xrzy57u/1/
methods: {
calc: function (item) {
item.isChecked = !item.isChecked
this.total = 0;
for (i = 0; i < this.items.length; i++) {
if(this.items[i].isChecked === true) {
this.total += this.items[i].price;
}
}
// fullPackagePrice
if(this.items[0].isChecked === true && this.items[1].isChecked === true && this.items[2].isChecked === true) {
this.total = this.fullPackagePrice;
}
// Trying to guarantee that have at least one checkbox checked
if(this.items[0].isChecked === false && this.items[1].isChecked === false && this.items[2].isChecked === false) {
this.total = this.items[0].price;
this.items[0].isChecked = true;
}
}
}
A good fit for this would be using computed properties instead of a method.
Read more about these here: https://v2.vuejs.org/v2/guide/computed.html#Computed-Properties
A computed property observes all referenced data and when one piece changes, the function is re-run and re-evaluated.
What you could do is first create a allowCheckout computed property like this:
allowCheckout() {
return this.items[0].isChecked || this.items[1].isChecked || this.items[2].isChecked;
}
You will then use it within the button like this:
<button :disabled="allowCheckout"...
This will disable the button when no items are checked.
Next, you'll also want to create a second computed property for the total price
totalPrice() {
// Perform similar checking here to update this.total
}
Lastly, you'll want to change your checkboxes to no longer use v-on:change but to instead use v-model for the relevant parameter for each.
This way your checkbox status will be bound to the true/falseness of the variables.
If you still want to go with your method, you can implement at like shown in this updated fiddle and set a variable atLeastOneItemIsChecked like this:
this.atLeastOneItemIsChecked = this.items.find(item => item.isChecked) !== undefined
Do not force the user to always check a checkbox. Instead, display a hint and disable the button using :disable and tailwind css resulting in this:
Related
I want always show compopent if logic one time is true.
I try add and set new var , but get "Unexpected side effect in "canShowAlways" computed property".
How i can do it in vue ?
<mycomp v-if="canShowAlways" />
data: function(){
return {
a: 0,
b: 4,
c: 1
d: 2,
isAlwaysShow: false
}
}
computed: {
canShowAlways() {
if(this.isAlwaysShow){
return true;
}
var isLast = this.a && this.b || this.c && this.d;
if(isLast){
this.isAlwaysShow = true;
return true;
}
return false;
},
In general, you should not edit other data in computed property.
This line this.isAlwaysShow = true; is most likely what causes the error that you see in your code.
If you really wanted to stick to the code you have above, then a quick solution would be to call a method that would change the value of this.isAlwaysShow.
Otherwise, you would set a watch as mentioned here.
There is most likely a better way to handle what you are trying to do but more information would need to be provided.
I am a Python developer and I have been working on Vuejs app.
If have function that is equivalent of a() in python that takes iterables. and if all items in iterable are true than all([...]) returns true
methods: {
all: function(iterable) {
for (var index = 0; index < iterable.length; ++index) {
if (!iterable[index]) return false;
}
return true;
}
}
and here is how I validate.
if (this.all([
this.age,
this.gender,
this.contactNumber,
this.townCity,
this.department.name,
this.attendType
])
) {
window.location = "#slip"
this.displayState = 'block';
}
else{
alert("Please fill all required fields.");
}
but this is not working.
Even if I fill all the mandatory fields I have the values in all the this.* attributes still I get alert saying "Please fill all required fields."
In JS, empty string values will return false on your IF statement.
You should check this point first.
(Maybe you have to check this topic)
Other thing : Make sure your "departement.name" variable is assigned. Vuejs is not reactive with sub-properties in objects.
Reactivity In Depth (Vue.js)
I want to hide an element based in a condition,
This is what i do:
First i use a v-if with computed property, but is not working, because when HMR reload the page, the button is hidden. And when i logout and login and refresh the localSorage with other condition, the button is still hidden or vice versa, the button is shown where should not.
Why?
This is my code:
computed:{
RegistrarUsuario(){
var userData = JSON.parse(localStorage.getItem("usuario"));
var acciones = userData.info.Acciones
for(var i = 0; i < acciones.length; i++){
if(acciones[i].accion === 'RegistrarUsuario'){
return false;
}
else{
return true;
}
}
}
},
<v-btn v-if="RegistrarUsuario" slot="activator" dark>Agregar</v-btn>
You're only looking at the first element of your acciones array (you return true or false on the first iteration). I suspect what you want is to return false if any of the elements matches. To do this you could use Array.some():
RegistrarUsuario(){
var userData = JSON.parse(localStorage.getItem("usuario"));
var acciones = userData.info.Acciones
return !acciones.some(a => a.accion === 'RegistrarUsuario');
}
I have a component that is going to have data named isVendorOrAgent that should be false or true based on a property that the component gets. When I put this condition on the data section of the component, it doesn't work and always returns false but when I put that in created() and change the isVendorOrAgent in created(), it works.
How can I make this work?
This is the condition that is not working:
data : () => {
return {
isVendorOrAgent : (this.entityName == "vendor" || this.entityName == "agent") ? true : false;
}
}
but this works when the default is false:
created(){
if(this.entityName == "vendor" || this.entityName == "agent"){
this.isVendorOrAgent = true;
}
}
Try this code sample:
data () {
return {
isVendorOrAgent: Boolean(this.entityName === "vendor" || this.entityName === "agent")
}
}
What is different?
Now data is not an arrow function, because arrow functions do not change the context, so this won't be what it should be inside the component
We now store a Boolean value
We are now using strict equality ===, which is just a good habit
You may also take a look at computed properties: https://v2.vuejs.org/v2/guide/computed.html
Your problem could be solved like this:
computed () {
return {
isVendorOrAgent: Boolean(this.entityName === "vendor" || this.entityName === "agent")
}
}
The second way is preferable if you need this property to be reactive.
Here is what I am trying to do:
I have two DataTables on the same page with different data. One is 'sell_orders' and the other is 'buy_orders'. I want to filter the data in each table separately based on checkboxes at the top of each table. So far I have gotten that to work using the following code:
$("#sell_vis_cit").change(function() {
var checked = this.checked;
var allowFilter = ['sell-orders'];
if (!checked) {
$.fn.dataTable.ext.search.push (
function(settings, data, dataIndex) {
// check if current table is part of the allow list
if ( $.inArray( settings.nTable.getAttribute('id'), allowFilter ) == -1 ) {
// if not table should be ignored
return true;
}
return $(sell_table.row(dataIndex).node()).attr('sell-data-sec') != 'x';
}
);
sell_table.draw();
} else {
$.fn.dataTable.ext.search.pop();
sell_table.draw();
}
});
$("#buy_vis_cit").change(function() {
var checked = this.checked;
var allowFilter = ['buy-orders'];
if (!checked) {
$.fn.dataTable.ext.search.push (
function(settings, data, dataIndex) {
// check if current table is part of the allow list
if ( $.inArray( settings.nTable.getAttribute('id'), allowFilter ) == -1 ) {
// if not table should be ignored
return true;
}
return $(buy_table.row(dataIndex).node()).attr('buy-data-sec') != 'x';
}
);
buy_table.draw();
} else {
$.fn.dataTable.ext.search.pop();
buy_table.draw();
}
});
The problem I am having is when it comes time to remove the filter. If filters have been applied to each table, the removal of the filter using the pop() function becomes unreliable because there is no way to verify that it is removing the filter from the right table.
So my question is: is there a way to verify that pop() is running on the right table like I did with push()? Alternatively, is there a better way to achieve my goal?
Why push() and pop() in the first place? It seems to me you have some static filters which is turned on and off by checkboxes. You could declare a filter once globally and do the "math" inside the filter :
$.fn.dataTable.ext.search.push(function(settings, data, dataIndex) {
if ((settings.sTableId == 'sell-orders' && $("#sell_vis_cit").is(':checked')) ||
(settings.sTableId == 'buy-orders' && $("#buy_vis_cit").is(':checked'))) {
//filter code
} else {
return true
}
})
and then simply activate the filters in the click handlers :
$("#sell_vis_cit, #buy_vis_cit").change(function() {
buy_table.draw();
sell_table.draw();
})