0

I would really appreciate some help here. My JSON knowledge is limited, and I can't solve it.

I need to delete an item from a shopping cart quick view that I create via JSON. The front end seems to work, but it doesn't because it doesn't remove the item from the JSON object.

The JSON object looks like.

[
    {
        "id": 216687,
        "productId": 9604505,
        "catalogId": 306758,
        "name": "xxxxxxxxx1",
        "description": "",
        "quantity": 1,
        "totalPrice": "38,00",
        "smallImage": "/images/products/large/xxxxx.jpg",
    },
    {
        "id": 216687,
        "productId": 9604503,
        "catalogId": 306756,
        "name": "xxxxxxxxx1",
        "description": "",
        "quantity": 1,
        "totalPrice": "38,00",
        "smallImage": "/images/products/large/xxxxx.jpg",
    }
]

jQuery function:

//Deleting Items
$(document).on('click', '.shopping-cart .delete i', function(){
    var $target = $(this).parent().parent();
    var $positions = $('.shopping-cart .item');
    $target.hide(300, function(){
        $.when($target.remove()).then( function(){
            if($positions.length === 1) {
                $('.shopping-cart .items-list').remove();
                $('.shopping-cart .title').text('Shopping cart is empty!');
            }
        });
    });
});

I think that I am not deleting the item properly.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tyra Pululi
  • 436
  • 1
  • 7
  • 19
  • Is the Json stored in a variable somewhere? – Mike Loffland Mar 21 '15 at 06:08
  • What are you using the JSON for? To render some content on the page? To update the server? It's not clear what the relationship is between your jquery code and the json. – technophobia Mar 21 '15 at 06:09
  • @Mike I am very bad with json, I get the object with this.items since globals.items returns null. – Tyra Pululi Mar 21 '15 at 07:05
  • @technophobia, to render content on the page but i need it too to modify content in the server when an item is deleted – Tyra Pululi Mar 21 '15 at 07:05
  • @jogesh_pi thats the first post I found what it didnt help much – Tyra Pululi Mar 21 '15 at 07:11
  • So are you saying you get that data from the server in JSON format, and now you want to remove an item from that data on the server? You will need to have some way of telling the server to delete the data then. Do you control the code that sends the data from the server? – GregL Mar 21 '15 at 13:14
  • sorry for not answering before, been really bussy tfinishing something. – Tyra Pululi Mar 23 '15 at 16:56
  • @GregL yes, thats exactly what I have in mind, and I am not sure if I can control that data. – Tyra Pululi Mar 23 '15 at 16:57

1 Answers1

0

I don't see where you're even trying to delete the item from the array. My jQuery is a little rusty, but I only see DOM manipulation going on.

You have to find the item in the list and remove it.

   // code elsewhere
    var id = 216687;

    RemoveItem(id);

    // function to remove item
    function RemoveItem(id) {    
        for(var i = 0; i < data.length; i++) {
            // don't know what your criteria for removal is, but you can match any property
            if (data[i].id == id) {
                // removes the element when the match is found based on your criteria for removal
                var smallerArray = data.splice(i, 1);
            }
        }
    }
Yatrix
  • 13,361
  • 16
  • 48
  • 78
  • HI and thanks, I removed the json attempts since everything is pretty messy. So you are right with this code i am not even trying to remove anything. So I need to get the id of the item? and delete that id from the object? – Tyra Pululi Mar 21 '15 at 06:34
  • @TyraPululi that's a design decision I can't answer for you. You could go by id, productId, etc. That's up to you. Whatever you want to use, you'll have to have accessible in the DOM somewhere. If the id's are all unique, you can make those id's the id element representative of the item in the cart and then grab the id that way. Something like `RemoveItem($cartItem.attr('id'));` – Yatrix Mar 21 '15 at 06:41
  • I am still trying to figure it out – Tyra Pululi Mar 21 '15 at 07:32