Can't add new item using v-model in Vue JS - vue.js

I am learning Vue.
Now, I am trying to add data with the price and finally, it calculates total price:
Here is the HTML
<div id="app">
<form #submit.prevent="addItem">
<table border="1" cellpadding="10" width="300">
<tr>
<td colspan="2"><strong>Add New Item</strong></td>
</tr>
<tr>
<td>
<input type="text" name="" v-model="newItem" placeholder="Item Name">
</td>
<td>
<input type="number" name="" v-model="newItemPrice" placeholder="Item Price">
</td>
</tr>
</table>
</form>
<br>
<table border="1" cellpadding="10" width="400">
<tr>
<th>Item Name</th>
<th>Item Price</th>
</tr>
<tr v-for="(item, index) in items" :key="index">
<td>{{ item.name }}</td>
<td><input type="number" name="" v-model="item.price"></td>
<td><button #click="removeItem(index)">X</button></td>
</tr>
<tr>
<td>Total</td>
<td><strong>{{ total }}</strong></td>
</tr>
</table>
</div>
Here is the Vue Instance:
new Vue({
el : '#app',
data : {
items: [
{ name: 'Rice', price : 12.60 },
{ name: 'Oil', price : 22.00 },
{ name: 'Mango', price : 32.50 },
{ name: 'Orange', price : 42.00 },
],
newItem : '',
newItemPrice : '',
},
computed: {
total() {
var total = 0;
this.items.forEach( item => {
total += parseFloat( item.price );
})
return total;
}
},
methods: {
addItem() {
this.items.push({
name: this.newItem,
price: 0
});
},
removeItem( index ) {
this.items.splice( index, 1 )
}
}
});
You can see it's by default showing item name and price. I want to add new item using the v-model called newItem But It's not adding the new item to the table
BUT
If I remove the Item Price column I mean this line:
<td>
<input type="number" name="" v-model="newItemPrice" placeholder="Item Price">
</td>
then it's adding the new item perfectly :(
can you tell me what's wrong here?

See two issues with the fiddle:
There is no way to submit the form data
When pushing the price field was not added to the object
After fixing both of them it works well in this fiddle.

This happens because of browser implementation. As mentioned in W3C Specs:
When there is only one single-line text input field in a form, the user agent should accept Enter in that field as a request to submit the form.
But in case of multiple elements, the enter keypress does not trigger the form submit and thus you get this behaviour.
To resolve this you can simply use #keyup.enter.prevent="addItem" to listen to the enter keypress on each input and call the addItem() function like:
new Vue({
el: '#app',
data: {
items: [{name:"Rice",price:12.6},{name:"Oil",price:22},{name:"Mango",price:32.5},{name:"Orange",price:42}],
newItem: '',
newItemPrice: null,
},
computed: {
total() {
var total = 0;
this.items.forEach(item => {
total += parseFloat(item.price);
})
return total;
}
},
methods: {
addItem() {
this.items.push({
name: this.newItem,
price: 0
});
},
removeItem(index) {
this.items.splice(index, 1)
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet">
<div id="app">
<form #submit.prevent="addItem">
<table border="1" cellpadding="10" width="300">
<tr>
<td colspan="2"><strong>Add New Item</strong></td>
</tr>
<tr>
<td>
<input type="text" name="" v-model="newItem" placeholder="Item Name"
#keyup.enter.prevent="addItem">
</td>
<td>
<input type="number" name="" v-model="newItemPrice" placeholder="Item Price"
#keyup.enter.prevent="addItem">
</td>
</tr>
</table>
</form>
<br>
<table border="1" cellpadding="10" width="400">
<tr>
<th>Item Name</th>
<th>Item Price</th>
</tr>
<tr v-for="(item, index) in items" :key="index">
<td>{{ item.name }}</td>
<td><input type="number" name="" v-model="item.price"></td>
<td><button #click="removeItem(index)">X</button></td>
</tr>
<tr>
<td>Total</td>
<td><strong>{{ total }}</strong></td>
</tr>
</table>
</div>

You should put a new line in your form, my suggestion is to put just above the close form tag </form>:
<input type="submit" value="add">
Another fix to do is in your methods addItem()
addItem() {
this.items.push({
name: this.newItem,
price: this.newItemPrice
});
}
Where it is the number 0 you should provide the this.newItemPrice to it work properly.

Related

I'm having a render error in my nuxt page with a property that cannot be read

I'm trying to add rows to a table dynamically with a method in nuxt.js, but the prop that I have defined doesn't work...
I receive this error from the console:
Property or method "invoice_customer" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.
I'm trying with this template:
<template>
<div>
<h3>Customers</h3>
<input type="text" v-model="search" placeholder="Search for names..." />
<table v-if="customers.length > 0">
<tr>
<th>name</th>
<th>address</th>
</tr>
<tr v-for="customer in customers" :key="customer.id">
<td>{{ customer.name }}</td>
<td>{{ customer.address }}</td>
</tr>
</table>
<div class="center">
<p>Customer</p>
<select v-if="customers.length > 0">
<option v-for="customer in customers" :key="customer.id">
{{ customer.name }}
</option>
</select>
</div>
<table v-if="invoice_customers.length > 0">
<tr>
<th>Id</th>
<th>Amount</th>
<th>Expire Date</th>
<th>Note</th>
</tr>
<tr
v-for="invoice_customers in invoice_customer"
:key="invoice_customers.id"
>
<td>
<input
type="number"
name=""
id=""
v-model="invoice_customer.amount"
/>
</td>
<td>
<b-form-datepicker
id="example-datepicker"
v-model="invoice_customer.expire_date"
class="mb-2"
></b-form-datepicker>
</td>
<td>
<input type="text" name="" id="" v-model="invoice_customer.note" />
</td>
</tr>
</table>
<b-button variant="outline-success" #click="addNewInstallment">+ Add New</b-button>
</div>
</template>
and this is my script:
<script>
export default {
name: "IndexPage",
data() {
return {
selected: "",
customers: [],
search: "",
invoice_customers: [{
id: 0,
amount: 0,
expire_date: "",
note: "",
}],
};
},
props: {
},
async mounted() {
const response = await this.$axios.$get("/api/customers");
this.customers = response.data;
return this.customers.filter((p) => {
// return true if the product should be visible
// in this example we just check if the search string
// is a substring of the product name (case insensitive)
return p.name.toLowerCase().indexOf(this.search.toLowerCase()) != -1;
});
},
methods: {
addNewInstallment() {
this.invoice_customers.push({
id: 0,
amount: 0,
expire_date: "",
note: "",
});
}
}
}
</script>
invoice customer is defined in v-for cycle
You inverted the order in your v-for, it should be
v-for="invoice_customer in invoice_customers"

Getting a NaN when typing a value

I'm trying to calculate a value from 2 input boxes and then get the total of those input boxes. I'm then trying to get the all my amounts and total them and add them to the subtotal but the issue I'm having is that when I type in a number in the first box my output is NaN instead of 0 and I would like for it to show me a 0 instead.
Here is my code
<template>
<div class="content-header">
<div class="container">
<div class="row">
<div class="col-sm-12">
<div class="card">
<div class="card-body">
<div class="row">
<div class="col-lg-12">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Unit</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr v-for="product in products">
<td>{{ product['name'] }}</td>
<td>
<input type="text" class="form-control" v-model="unit[product['unit']]" #change="calculateCost(product['name'])">
</td>
<td>
<input type="text" class="form-control" v-model="price[product['price']]" #change="calculateCost(product['name'])">
</td>
<td>
{{ cost[product['name']] }}
</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td>
Subtotal: {{ subTotal }}
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default{
props: [],
data(){
return {
products: [],
unit: {},
price: {},
cost: []
}
},
computed:{
subTotal(){
if(this.cost!== null)
{
if(Object.keys(this.cost).length !== 0){
return Object.keys(this.cost).reduce((carry, item) => {
carry+= Number(this.cost[item])
return carry;
}, Number(0))
}else{
return 0;
}
}
}
},
methods: {
getProducts(){
axios.get(`/api/product/all`).then(response => {
this.products = response.data.products;
});
},
calculateCost(item){
this.cost[item] = Number(this.unit[item]) * Number(this.price[item]);
},
},
mounted() {
this.getProducts();
}
}
</script>
Almost all type of inputs return a string. You can use
<input type="number" class="form-control" v-model="unit[product['unit']]" #change="calculateCost(product['name'])">
or
<input type="text" class="form-control" v-model.number="unit[product['unit']]" #change="calculateCost(product['name'])">
The problem is the v-model for unit and price are set to the different keys than the one given to calculateCost(), which causes the lookups to fail and results in NaN:
<input v-model="unit[product['unit']]" #change="calculateCost(product['name'])"> ❌
^^^^^^ ^^^^^^
<input v-model="price[product['price']]" #change="calculateCost(product['name'])"> ❌
^^^^^^^ ^^^^^^
<input v-model="unit[product['name']]" #change="calculateCost(product['name'])"> ✅
<input v-model="price[product['name']]" #change="calculateCost(product['name'])"> ✅
Setting the keys to product['name'] ensures the correct lookup for unit and price in calculateCost(). Since the user could enter invalid values (non-numeric strings) or omit a value, it's possible to get NaN, so it would be a good idea to add a NaN-check in calculateCost():
calculateCost(item) {
const unit = Number(this.unit[item]);
const price = Number(this.price[item]);
if (Number.isNaN(unit) || Number.isNaN(price)) return;
this.cost[item] = unit * price;
},
Also, you probably want the cost to react to user input, so switch from #change to #input:
<input v-model="unit[product['name']]" #input="calculateCost(product['name'])">
<input v-model="price[product['name']]" #input="calculateCost(product['name'])">
demo

set checkbox checked if value same vuejs

I'm displaying a list of menus in a table and looping with v-for, i want to set checkbox is checked if "id" from data menus same with "id_menu" from grupMenu, anyone has the same problem, please share it how i can solved, I'm realy new in Vue, thank's for helping,
new Vue({
el: "#app",
data: {
menus: [{
id: 1,
name_menu: "Setting",
parent: 0
},
{
id: 2,
name_menu: "Users",
parent: 1
},
{
id: 3,
name_menu: "Menu",
parent: 1
},
{
id: 4,
name_menu: "Role",
parent: 1
},
],
grupMenu: [{
id: 1,
id_user_group: 1,
id_menu: 1,
role: 1
},
{
id: 2,
id_user_group: 1,
id_menu: 2,
role: 0
},
]
},
methods: {
}
})
.text-center {
text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<h2>Todos:</h2>
<table border="1">
<thead>
<tr>
<th rowspan="2" style="vertical-align:middle;text-align:center">Menu</th>
<th colspan="3" style="text-align:center">Previlege</th>
</tr>
<tr class="">
<th class="text-center">Insert</th>
</tr>
</thead>
<tbody>
<tr v-for="menu in menus" :key="menu.id">
<td v-if="menu.parent == 0" style="color:blue">
<input type="checkbox">
<!-- <input type="checkbox" v-model="checked"> -->
<b class="ml-2">{{menu.name_menu}}</b>
</td>
<td v-else>
<input type="checkbox" class="ml-5"> -- {{menu.name_menu}}
</td>
<td class="text-center">
<input type="checkbox">
</td>
<!-- </div> -->
</tr>
</tbody>
</table>
</div>
here in fiddle
If I understand correctly, you want the checkbox checked for each entry in menu whose id appears as a menu_id in grupMenu.
So write a method like this:
isInGrupMenu(id) {
return this.grupMenu.some((item) => item.id_menu === id);
}
and bind it to the checked attribute of each checkbox:
<input type="checkbox" :checked="isInGrupMenu(menu.id)">

Vuejs: add the same form multiple times

I'm using laravel and vuejs.
One of the things I want to be able to do, is to allow the user to create muliple address when they click the button.
Here the sample code:
<table>
<tr>
<th>POSTAL CODE</th>
<td><input type="text" name="postal_code[]" v-model="" /></td>
</tr>
<tr>
<th>City</th>
<td>
<select name="city[]" v-model="">
<option>ABC</option>
</select>
</td>
</tr>
<tr>
<th>District</th>
<td><input type="text" name="District[]" v-model="" /></td>
</tr>
<tr>
<th>Street</th>
<td><input type="text" name="Street[]" v-model="" /></td>
</tr>
<tr>
<th>Name/th>
<td>
last_name<input type="text" name="last_name[]" v-model="" />
first_name<input type="text" name="first_name[]" v-model="" />
</td>
</tr>
</table>
<button v-on:click="add-form">ADD</button>
EDIT Sorry because my question was not clear enough.
I want to duplicate the whole table, not only one input, and everytime user click the button, i want to show one more table (the old table with old input has not be changed)
I found the solution.
Thank you so much!
All you need to do is to create an empty array of addresses and then push the input into this array on button click. Here's an example:
Jsfiddle: https://jsfiddle.net/zyh4jvuf/
<div id="app">
<input type="text" v-model="addressInput">
<button v-on:click="addAddress">add</button>
<!-- Displaying addresses -->
<div v-for="address in addresses">
{{address}}
</div>
</div>
new Vue({
el: '#app',
data () {
return {
addressInput: '',
addresses: []
}
},
methods: {
addAddress () {
this.addresses.push(this.addressInput)
// clear the input
this.addressInput = ''
}
}
})
Do you want to "duplicate" the whole table or just a single input? I guess the second want. In that case, i would rather push the info to an array like this:
<template>
<div class="formApp">
<input type="text" name="steet" v-model="street">
<button #click="addStreet">add</button>
<div v-for="str in streetArray">
<div #click="removeStreet($event)">{{ str }}</div>
</div>
</div>
</template>
<script>
export default {
name: 'formApp',
data() {
return {
streetArray: [],
street: '',
}
},
methods: {
addStreet() {
this.streetArray.push(this.street);
this.street = '';
},
removeStreet(event) {
var index = this.streetArray.indexOf(event.target.innerText);
index > -1 ? this.streetArray.splice(index, 1):'';
}
}
}
</script>
The addStreet will add a new street everytime you click the button and then it will clear the input. Then, if the user wants to delete the street, it will have to click over the street name. That way of deleting things is not user friendly at all so you could add a icon to each street name to declare that possibility.
Also, it was just an hint for you because there can be errors while writing streets names and the user needs a way to delete them. Having a bunch of inputs for wrong streets or not being able to delete the ones you added would be a total chaos. Anyways, there you go.
You can try the following code
new Vue({
el: '#example-3',
data:{
title:"Lists",
item:{},
items:[]
},
methods: {
addform: function () {
this.items.push(this.item);
this.item={};
}
}
})
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<div id="example-3">
<table>
<tr>
<th>POSTAL CODE</th>
<td><input type="text" name="postal_code" v-model="item.postal_code" /></td>
</tr>
<tr>
<th>City</th>
<td>
<select name="city" v-model="item.city">
<option>ABC</option>
</select>
</td>
</tr>
<tr>
<th>District</th>
<td><input type="text" name="District" v-model="item.District" /></td>
</tr>
<tr>
<th>Street</th>
<td><input type="text" name="Street" v-model="item.Street" /></td>
</tr>
<tr>
<th>Name</th>
<td>
last_name<input type="text" name="last_name" v-model="item.last_name" /><br/>
first_name<input type="text" name="first_name" v-model="item.first_name" />
</td>
</tr>
</table>
<button v-on:click="addform">ADD</button>
<br/>
<h2>{{title}}</h2>
<table border="1" v-if="items.length>0">
<tr v-for="(value,index) in items">
<td>{{value.postal_code}}</td>
<td>{{value.city}}</td>
<td>{{value.District}}</td>
<td>{{value.last_name}}</td>
<td>{{value.first_name}}</td>
</tr>
</table>
</div>

Dynamic v-model with v-for

I have a v-for loop that is going to spit out multiple rows of inputs that I would like to save each separate row to an array object dynamically.
v-for:
<table class="table m-0">
<tbody>
<tr v-for="fund in defaultFunds">
<td>
{{fund.name}}
<b-input v-model="newEntries[fund.id]['id']"
:value="fund.id"
name="entryFund"
type="text"
class="d-none"
:key="fund.id" />
</td>
<td>
<b-input v-model="newEntries[fund.id]['amount']"
name="newFundAmount"
id="newFundAmount"
type="text"
placeholder="Amount"
:key="fund.id"/>
</td>
</tr>
</tbody>
</table>
Desired array (using example of entering 2 rows):
newEntries: [
{ id: '1', amount: '50.00' },
{ id: '2', amount: '123.45' }
],
I load newEntries as an empty array by default. I don't know how to get the kind of array object I want with v-for. With the above code I end up with this:
newEntries: [null, '50.00', '123.45']
What am I doing wrong?
Do you want something like this:
https://jsfiddle.net/e6L5seec/
<div id="app">
newEntries: {{ newEntries }}
<table>
<tr v-for="(fund, index) in defaultFunds">
<td>{{ fund.name }}</td>
<td>
<input v-model="newEntries[index].id"
name="entryFund"
:value="fund.id"
type="text" />
</td>
<td>
<input v-model="newEntries[index].amount"
name="entryFund"
type="text" />
</td>
</tr>
</table>
</div>
new Vue({
el: "#app",
data: function() {
return {
defaultFunds: [
{
id: 0,
name: 'fund 0'
},
{
id: 1,
name: 'fund 1'
}
],
newEntries: [{}, {}]
}
},
methods: {
}
});