2015-10-15 165 views
0

說我的購物車中有3個項目,我想刪除讓我們說3個項目中的2個,第一個項目刪除後就好了,其他項目不會被刪除。 經過一些測試,我看到在刪除產品之前數據是一個數組,並且在刪除產品後,數組轉換成了一個對象。從數組中刪除對象

例如: Basket {Klant: Object, Orderlines: Array[0]}

加入一些項之後:Basket {Klant: Object, Orderlines: Array[3]}

陣列中

是3個對象:

{"Klant":{},"Orderlines":[ 
{"id":"2793","number":1,"membershiptype":"New","lineid":"3521"}, 
{"id":"2802","number":1,"membershiptype":"New","lineid":"3522"}, 
{"id":"2803","number":1,"membershiptype":"New","lineid":"3523"} 
]} 

和刪除項之後:Basket {Klant: Object, Orderlines: Object}

和在該對象仍然是2個對象:

{"Klant":{},"Orderlines":{ 
"0":{"id":"2793","number":1,"membershiptype":"New","lineid":"3521"} 
"2":{"id":"2803","number":1,"membershiptype":"New","lineid":"3523"} 
}} 

我嘗試拼接和刪除數組,但結果相同。 我想要得到的結果是:

{"Klant":{},"Orderlines":[ 
{"id":"2793","number":1,"membershiptype":"New","lineid":"3521"}, 
{"id":"2803","number":1,"membershiptype":"New","lineid":"3523"} 
]} 

這裏是我的代碼:

function Basket() { 
    this.Klant = {}; 
    this.Orderlines = []; 
    Basketdata = null; 
    // check if we have storage and can use it. 
    if (typeof (Storage) !== "undefined") { 
     Basketdata = sessionStorage.getItem("Basket"); 
     total = sessionStorage.getItem("aantalitems"); 
     $('#aantalitems').html(total); 

     console.log(Basketdata); 
     if (Basketdata != null) { 
       mybasket = JSON.parse(Basketdata); 
       this.Orderlines = mybasket.Orderlines; 
       this.Klant = mybasket.Klant; 
     } 
    } 
    if (Basketdata == null) { 
     Basketdata = {}; 
    } 
    $('a.addcart').on('click', {this: this}, this.addArticle); 
    $('a.remove_cart_item').on('click', {this: this}, this.deleteArticle); 
    $('input.ChangeAmount').on('keyup change', {this: this}, this.changeAmount); 

} 

Basket.prototype = { 
    constructor: Basket, 

deleteArticle: function (event) { 
     var knop = $(event.currentTarget); 
     var mainartikel = $(knop).parents('tr.cart_item'); 
     var id = $(mainartikel).attr('id'); 
     var total = 0; 
     for (var i in event.data.this.Orderlines) { 
       var line = event.data.this.Orderlines[i]; 
       if (line.lineid === id) { 
        console.log(event.data.this.Orderlines[i]); 
        console.log(i); 
        event.data.this.Orderlines[i].delete = 1; 
       } else { 
        total += line.number; 
       } 
     } 
     sessionStorage.setItem("aantalitems", total); 
     $(mainartikel).fadeOut(function() { 
       $(this).remove(); 
     }); 
     sessionStorage.setItem("Basket", JSON.stringify(event.data.this)); 
     event.data.this.sendToServer(event.data.this); 



    }, 
sendToServer: function (object) { 
     $.ajax({ 
       type: "POST", 
       url: '/shop?addtoorder=1', 
       data: JSON.stringify(this), 
       dataType: 'json', 
       databasket: object 
     }).done(function (data) { 
       this.databasket.Orderlines = data.Orderlines; 
       for (var i in this.databasket.Orderlines) { 
        if (this.databasket.Orderlines[i].delete == 1) { 
         //here is the delete 
          //delete this.databasket.Orderlines[i]; 
          this.databasket.Orderlines.splice(i,1); 
        } 
        if (this.databasket.Orderlines[i].update == 1) { 
          this.databasket.Orderlines[i].update = 0; 
          location.reload(); 
        } 
       } 
       sessionStorage.setItem("Basket", JSON.stringify(this.databasket)); 
       $('#totalprice').html(data.total); 
       if (this.databasket.Orderlines.length == 0) { 
        this.Orderlines = []; 

        $('div.content').html('\ 
<div style="float: left;"><b><a href="../../../shop"><img src="../../lib/items/Checkout/template/go-home-icon.png" width="32" height="32" alt="go-home-icon" style="vertical-align: middle;"> Back to the shop</a></b></div>\n\ 
<br><br>No items in basket\n\ 
'); 
       } 
     }); 
    }}; 

什麼我試過到目前爲止:

deleteArticle 下,我試圖改變:event.data.this.Orderlines[i].delete = 1;

至:delete event.data.this.Orderlines[i]

這也結束了陣列改變到對象

sendToServer 下我試圖既

delete this.databasket.Orderlines[i]; 
this.databasket.Orderlines.splice(i,1); 

這也結束了陣列改變到對象

+0

你可以創建一個jsfiddle來測試它嗎? – nils

+0

@尼爾斯我會試試看,可能需要一些時間艱難 – KickRAzz

+0

刪除似乎並不是問題:http://jsfiddle.net/Lfudroe4/ 我認爲這與'data'有關,來自您的JSON響應的變量。另外,爲什麼在客戶端而不是服務器上刪除命令行? – nils

回答

0

嘗試:

delete yourObj['Orderlines'] 

var obj = {Basket: {Klant: Object, Orderlines: Array[0]}}; 
 
obj['Orderlines'] = [ 
 
{"id":"2793","number":1,"membershiptype":"New","lineid":"3521"}, 
 
{"id":"2802","number":1,"membershiptype":"New","lineid":"3522"}, 
 
{"id":"2803","number":1,"membershiptype":"New","lineid":"3523"} 
 
]; 
 
document.write('After adding to obj[\'Orderlines\']: ' + obj['Orderlines'] + '<br/>'); 
 
delete obj['Orderlines']; 
 
document.write('After deleting from obj[\'Orderlines\']: ' + obj['Orderlines']);