我正在通过 Ajax 获取我的购物车的数据。
这个想法如下: - 我在购物车的每一行得到一行 - 每行我在数据库中检查用于填充选择对象的选项 - 如果该选项与我的行中的选项相同,则它被选中
我可以通过将选项 async 设置为 false 来实现这一点。但正如我在这里读到的,这不是首选方法。我应该如何调整我的架构?
//get items from cart
$.ajax({
type: "POST",
url: "db/getImagesCart.php"
}).done(function(data) {
//populate carts ul
var items = [];
obj = $.parseJSON(data.trim());
$.each(obj, function(id, value) {
//list formats of this item
$.ajax({
type: "POST",
url: "db/getFormats.php",
async: false
}).done(function(data){
var select_format = "<select style='text-align: center;'>";
formats = $.parseJSON(data.trim());
$.each(formats, function(id, test){
//add select tot variable + select if the same
if(test.format_id == value.idformat){
select_format += "<option value=" + test.format_id + " SELECTED data-price=" + test.format_price + ">" + test.format_name + "</option>";
} else {
select_format += "<option value=" + test.format_id + " data-price=" + test.format_price + ">" + test.format_name + "</option>";
}
});
select_format += "</select>";
items.push('<tr id="row' + value.item_id + '"><td class="remove"><a href="#" class="delete" id="' + value.item_id + '"><img src="img/delete.png" height="25px" style="border: 0;" /></a></td><td class="image"><a class="fancybox" rel="group" href="' + value.path + '"><img src="' + value.path_thumb + '" style="max-width: 100px; height: 75px;" /></a></td><td class="formaat">' + select_format + '</td><td class="totalprice">€ ' + value.price + '</td></tr>');
});
});
$("tbody").html(items.join(''));
$(".fancybox").fancybox({
openEffect: 'elastic',
closeEffect: 'elastic',
nextEffect: 'fade',
prevEffect: 'fade'
});
$(".delete").click(function(){
var cart_item = $(this).attr("id");
var variables = 'action=2' + '&cart_item_id=' + cart_item;
$.ajax({
type: "POST",
url: "db/cart.php",
data: variables
}).done(function(data){
if($.browser.msie){
$('#row' + cart_item).find('td').fadeOut('slow', function(){
$('#row' + cart_item).find('td').parent().remove();
});
} else {
$('#row' + cart_item).fadeOut('slow', function(){
$('#row' + cart_item).find('td').parent().remove();
});
}
calc_full();
})
});
$(".formaat").change(function(){
//recalculate prices
$('.cart tr:gt(0)').each(function(){
var theprice = $(this).find('.formaat select :selected').attr("data-price");
$(this).find('.totalprice').html("€ " + theprice);
//save to db
var cart_item = $(this).attr('id').slice(3);
var formaat = $(this).find('.formaat select :selected').val();
var variables = 'action=3' + '&format=' + formaat + '&cart_item_id=' + cart_item;
$.post("db/cart.php", variables, function(data){
});
calc_full();
});
})
calc_full();
});