将数据添加到数组中并删除其他内容。

huangapple go评论56阅读模式
英文:

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:

&lt;input type=&quot;checkbox&quot; :id=&quot;item.id&quot; :name=&quot;item.name&quot; :data-category=&quot;item.category.name&quot; style=&quot;font-size: 0.6em; transform: scale(1.6);&quot; @change=&quot;addToCart($event)&quot;/&gt;

and my function addToCart():

const addItemCart = ref([])
    const addToCart = (event) =&gt; {
        let itemId = event.target.id
        let itemName = event.target.name
        let category = event.target.getAttribute(&quot;data-category&quot;)

        var newItem = new Array();
        newItem = [{ id: itemId, name: itemName, category: category }];
             
        addItemCart.value = newItem
    }

i trayed with:

const addItemCart = ref([])
    const addToCart = (event) =&gt; {
        let itemId = event.target.id
        let itemName = event.target.name
        let category = event.target.getAttribute(&quot;data-category&quot;)

        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: &#39;96&#39;, name: &#39;DESINCRUSTADOR DCALUXE - HIDROCAL ELECTRONICO&#39;, category: &#39;ARTICULOS B&#39;}
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: &#39;281&#39;, name: &#39;DCALUXE PRO 2.0 DESINCRUSTADOR ELECTRONICO&#39;, category: &#39;ARTICULOS B&#39;}
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:

&lt;tr v-for=&quot;item in addItemCart&quot; :key=&quot;item&quot;&gt;
    &lt;td&gt;{{ item }}&lt;/td&gt;
    &lt;td&gt;{{ item.id }}&lt;/td&gt;
    &lt;td&gt;{{ item.name }}&lt;/td&gt;
    &lt;td&gt;{{ item.category }}&lt;/td&gt;
    &lt;td&gt;&lt;i class=&quot;fa fa-trash&quot; aria-hidden=&quot;true&quot; @click=&quot;removeItem($event)&quot;&gt;&lt;/i&gt;&lt;/td&gt;
&lt;/tr&gt;

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) =&gt; {
    let itemId = event.target.id
    let itemName = event.target.name
    let category = event.target.getAttribute(&quot;data-category&quot;)
          
    addItemCart.value.push({ id: itemId, name: itemName, category: category })
    //here we push the new item as an object into `addItemCart.value`
}

huangapple
  • 本文由 发表于 2023年6月26日 21:21:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557100.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定