我对push数组有问题,如何在数据库中第二次插入时清空该数组。所以发生在我的问题中的情况是
第一次插入是可以的,如果再次按下提交,日志将在数组中的推入数据之前显示多次插入,那么在再次提交后是否可以清空数组?
第一个插入项等效于 2个项目。
所以当我以相同的顺序再次插入
我有变量
var orderNumber;
var orders = [];
var menu;
我这里有插入功能。
$(document).ajaxStop(function () {
$('#add_to_cart').on('click', function() {
var customer_id = $('#hidden_customer_id').val();
swal({
title: "Are you sure to submit this?",
text: "Once inserted, you will not be able to edit the transaction!",
icon: "warning",
buttons: true,
dangerMode: true,
})
.then((willInsert) => {
if (willInsert) {
$('.tbody_noun_chaining_order').children('tr').each(function() {
$row = $(this);
if ($row.hasClass('condimentParent')) {
// store a previous menu to the orders array if exists.
if (menu !== undefined) {
orders.push(menu);
}
menu = {
'total': $row.find('.total').text(),
'name': $row.find('.parent_item').text(),
'customer_id': customer_id,
'condiments': {
'Item': [],
'Qty': [],
'Total': []
}
};
} else if ($row.hasClass('editCondiments')) {
// row is a condiment, append elements to the previous "menu" variable
menu.condiments.Item.push($row.find('.child_item').text());
menu.condiments.Qty.push($row.find('.condiments_order_quantity').text());
menu.condiments.Total.push($row.find('.total').text());
}
});
if (menu) {
orders.push(menu);
}
storeOrder(orders);
$('table#noun_chaining_order').find('.tbody_noun_chaining_order').empty();
$('.append_customer_noun_order_price').text('0.00');
$('.total_amount').text('0.00');
$('.total_amount').text('0.00');
$('.rate_computation').text('0.00');
} else {
swal("Cancelled");
}
});
});
});
function storeOrder(data) {
var customer_id = $('#hidden_customer_id').val();
var place_customer = $('#place_customer').text();
$id = "";
$total_amount = $('.total_amount').text();
$append_customer_noun_order_price = $('.append_customer_noun_order_price').text();
$tax_rate = $('.rate_computation').text();
$delivery_rate = $('.del_rate').text();
$.ajax({
url:'/insert_customer_order_properties',
type:'POST',
data:{
'hidden_customer_id': customer_id,
'hidden_customer_address': place_customer,
},
success:function(data) {
$id = data[0].id;
$.ajax({
url:'/insert_customer_payment_details',
type:'POST',
data:{
'hidden_customer_id': customer_id,
'total_amount': $total_amount,
'customer_sub_total': $append_customer_noun_order_price,
'tax_rate': $tax_rate,
'id_last_inserted': $id
},
success:function(data) {
console.log(orders);
}
})
}
})
for (var num in orders) {
console.log(orders);
// simulate storing the order
$.ajax('/insert_wish_list_menu_order', {
type: 'POST',
// as the call is asynchronous, make sure to provide all required reference data along with the call.
context: orders[num].condiments,
data: {
// 'json': '{"orderNumber":' + (orderNumber++) + '}',
'append_customer_noun_order_price': orders[num].total,
'append_customer_noun_order': orders[num].name,
'customer_id': customer_id
},
success: function(orderNumber) {
$order_number = orderNumber[0].id; // meron na tayong kuha ng main item..
$.ajax({
url:'/insert_customer_order_details_properties',
type:'POST',
data:{
'order_number': $order_number,
'data_attribute_wish_order_id': $id,
},
success:function(data) {
console.log(data);
}
})
if (orderNumber !== undefined) {
$.ajax('/insert_wish_list_menu_belong_condiments', {
context: orderNumber,
type: 'POST',
data: {
'ParentId': orderNumber,
'Item': this.Item,
'Qty': this.Qty,
'Total': this.Total
// 'json': '{"condimentsCount": ' + this.Item.length + '}'
},
success: function(result) {
//$('#result').append('Stored ' + result.condimentsCount + ' condiments for order ' + this + '<br />');
console.log(result);
swal("Add to cart success,Redirect to proccessing page.", {
icon: "success",
});
},
})
}
}
})
}
}
答案 0 :(得分:0)
正如聊天中已经提到的那样,您的代码中目前缺少orders
和menu
变量的重置。
这将是您需要的一部分代码,将其插入if (willInsert) {
条件后面:
var orders= [];
var menu= undefined;
在您的for
循环中:
// store a previous menu to the orders array if exists.
if (menu) {
orders.push(menu);
}