英文:
add data to array delete other content
问题
I'm trying to add an element to an array without deleting other content, and it's very trivial... Array.push()
My problem is that I'm using ref([])
and I read that I can use ref.value.push = newItem
, but my problem is that it overrides my previous content. I need to allow the user to select checkboxes with information, and this information should be added to my table. To achieve this, I'm doing the following:
<input type="checkbox" :id="item.id" :name="item.name" :data-category="item.category.name" style="font-size: 0.6em; transform: scale(1.6);" @change="addToCart($event)"/>
And my addToCart()
function:
const addItemCart = ref([])
const addToCart = (event) => {
let itemId = event.target.id
let itemName = event.target.name
let category = event.target.getAttribute("data-category")
var newItem = [{ id: itemId, name: itemName, category: category }];
addItemCart.value = newItem
}
I tried with:
const addItemCart = ref([])
const addToCart = (event) => {
let itemId = event.target.id
let itemName = event.target.name
let category = event.target.getAttribute("data-category")
var newItem = [{ id: itemId, name: itemName, category: category }];
addItemCart.value.push = newItem
console.log(addItemCart.value)
}
This returns:
Proxy(Array) {push: Array(1)}
[[Handler]]: Object
[[Target]]: Array(0)
push: Array(1)
0: {id: '96', name: 'DESINCRUSTADOR DCALUXE - HIDROCAL ELECTRONICO', category: 'ARTICULOS B'}
length: 1
[[Prototype]]: Array(0)
length: 0
[[Prototype]]: Array(0)
[[IsRevoked]]: false
Proxy(Array) {push: Array(1)}
[[Handler]]: Object
[[Target]]: Array(0)
push: Array(1)
0: {id: '281', name: 'DCALUXE PRO 2.0 DESINCRUSTADOR ELECTRONICO', category: 'ARTICULOS B'}
length: 1
[[Prototype]]: Array(0)
length: 0
[[Prototype]]: Array(0)
[[IsRevoked]]: false
It's not pushing data into my array, and I can't display my data in my table. I have this code for the loop:
<tr v-for="item in addItemCart" :key="item">
<td>{{ item }}</td>
<td>{{ item.id }}</td>
<td>{{ item.name }}</td>
<td>{{ item.category }}</td>
<td><i class="fa fa-trash" aria-hidden="true" @click="removeItem($event)"></i></td>
</tr>
My question is, how can I add items to my array without deleting my other content?
英文:
i´m traying to add element into array without delete other content, it´s very trivial... Array.push()
my problem it´s i´m using ref([])
and i´m reading that i can use ref.value.push = newItem
but my problem it´s that override my previous content... i need to do that user select checkbox with information, and this information be added to my table. To build this, i´m doing:
<input type="checkbox" :id="item.id" :name="item.name" :data-category="item.category.name" style="font-size: 0.6em; transform: scale(1.6);" @change="addToCart($event)"/>
and my function addToCart()
:
const addItemCart = ref([])
const addToCart = (event) => {
let itemId = event.target.id
let itemName = event.target.name
let category = event.target.getAttribute("data-category")
var newItem = new Array();
newItem = [{ id: itemId, name: itemName, category: category }];
addItemCart.value = newItem
}
i trayed with:
const addItemCart = ref([])
const addToCart = (event) => {
let itemId = event.target.id
let itemName = event.target.name
let category = event.target.getAttribute("data-category")
var newItem = new Array();
newItem = [{ id: itemId, name: itemName, category: category }];
addItemCart.value.push = newItem
console.log(addItemCart.value)
}
This return:
Proxy(Array) {push: Array(1)}
[[Handler]]:Object
[[Target]]: Array(0)
push: Array(1)
0: {id: '96', name: 'DESINCRUSTADOR DCALUXE - HIDROCAL ELECTRONICO', category: 'ARTICULOS B'}
length: 1
[[Prototype]]: Array(0)
length: 0
[[Prototype]]: Array(0)
[[IsRevoked]]: false
app.js:20948
Proxy(Array) {push: Array(1)}
[[Handler]]: Object
[[Target]]: Array(0)
push: Array(1)
0: {id: '281', name: 'DCALUXE PRO 2.0 DESINCRUSTADOR ELECTRONICO', category: 'ARTICULOS B'}
length: 1
[[Prototype]]: Array(0)
length: 0
[[Prototype]]: Array(0)
[[IsRevoked]]: false
Not push data in my array and i can´t show my data in my table, i have this code to do loop:
<tr v-for="item in addItemCart" :key="item">
<td>{{ item }}</td>
<td>{{ item.id }}</td>
<td>{{ item.name }}</td>
<td>{{ item.category }}</td>
<td><i class="fa fa-trash" aria-hidden="true" @click="removeItem($event)"></i></td>
</tr>
My question it´s, how i can to do add items in my array bu not deleted my other content.
Thanks for readme and sorry for my bad english
答案1
得分: 0
尝试将 push 作为一个函数使用,并将新对象作为参数传递给 push:
const addItemCart = ref([])
const addToCart = (event) => {
let itemId = event.target.id
let itemName = event.target.name
let category = event.target.getAttribute("data-category")
addItemCart.value.push({ id: itemId, name: itemName, category: category })
// 在这里我们将新项目作为对象推送到 `addItemCart.value`
}
英文:
try using push as a function, and passing the new object to push as a parameter:
const addItemCart = ref([])
const addToCart = (event) => {
let itemId = event.target.id
let itemName = event.target.name
let category = event.target.getAttribute("data-category")
addItemCart.value.push({ id: itemId, name: itemName, category: category })
//here we push the new item as an object into `addItemCart.value`
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论