简体   繁体   English

如何使用jquery在购物车中显示有关放置物品的袋子的字段

[英]How to display a field in shopping cart regarding the bag in which item was dropped using jquery

I am very new to jQuery . 我对jQuery非常jQuery I am presently working on a project. 我目前正在做一个项目。 In this project there are 3 types of items and three bags such that the user should select anyone of the bags and try dropping items into it. 在该项目中,有3种物品和3个袋子,因此用户应选择任何一个袋子并尝试将物品放入其中。 If the user selects bag 1 then he can drop item1, item2, item3 if he selects bag 2 he can drop item2, item3 if he selects bag3 he can drop item3 only. 如果用户选择袋子1,则他可以放下item1,item2,item3(如果他选择袋子2),他可以放下item2,item3(如果他选择bag3),他只能放下item3。

Now the problem is I have added an additional field bag to display the type of bag in which the item was dropped (example "bag1" or "bag2" or "bag3"). 现在的问题是,我添加了一个额外的外地bag来显示放置物品的袋的类型(例如“ bag1”或“ bag2”或“ bag3”)。

But the problem is I am unable to display the bag field. 但是问题是我无法显示bag字段。 Do anyone out there let me know what I can do. 有谁在外面让我知道我能做什么。 I am struggling a lot for this. 我为此付出了很多努力。

Example on jsfiddle: http://jsfiddle.net/Vwu37/48/ jsfiddle上的示例: http : //jsfiddle.net/Vwu37/48/

HTML: HTML:

<tr>
    <th field="name" width=140>Name</th>
    <th field="bag" width=60>Bag</th>
    <th field="quantity" width=60 align="right">Quantity</th>
    <th field="price" width=60 align="right">Price</th>
    <th field="remove" width=60 align="right">Remove</th>
</tr>

Javascript: Javascript:

var data = {
    "total": 0,
    "rows": []
};
var totalCost = 0;
$(function () {
    $('#cartcontent1').datagrid({
        singleSelect: true
    });

    $('.bag').droppable({
        onDrop: function (e, source) {
            var name = $(source).find('p:eq(0)').html();
            var price = $(source).find('p:eq(1)').html();
            addProduct(name, parseFloat(price.split('$')[1]));
            $(source.draggable).remove();
            //$('.bag').droppable('enable');
            $(this).droppable('enable');
        }
    });


    $('.item').each(function (index, div) {
        var scope = $(this).attr('data-scope');
        //debugger;
        $(div).draggable({
            revert: true,
            proxy: 'clone',
            onStartDrag: function () {
                $('.bag').droppable('enable');
                $('.bag:not(.selected)').droppable('disable');
                $('.bag:not(.bag[data-scope*=' + scope + '])').droppable('disable');

                $(this).draggable('options').cursor = 'not-allowed';
                $(this).draggable('proxy').css('z-index', 10);
            },
            onStopDrag: function () {
                //$('.bag').droppable('enable');
                $(this).draggable('options').cursor = 'move';
            }
        });
    });



    $('.bag').click(function () {
        $('.selected').removeClass('selected');
        $(this).addClass('selected');
    });

});


function addProduct(name, price) {
    var totalQuantity = sumQuantity(data);

    if (totalQuantity < 10) {
        function add() {
            for (var i = 0; i < data.total; i++) {
                var row = data.rows[i];
                if (row.name == name) {
                    row.quantity += 1;
                    return;
                }
            }
            data.total += 1;
            data.rows.push({
                name: name,
                quantity: 1,
                price: price,
                remove: '<a href="#" class="remove" onclick="removeProduct(this, event)">X</a>'
            });
        }
        add();
        totalCost += price;
        $('#cartcontent1').datagrid('loadData', data);
        $('div.cart .total').html('Total: $' + totalCost);
    } else {
        alert('cannot have more than 10 items');
    }
}

function removeProduct(el, event) {
    var tr = $(el).closest('tr');
    var name = tr.find('td[field=name]').text();
    var price = tr.find('td[field=price]').text();
    var quantity = tr.find('td[field=quantity]').text();
    for (var i = 0; i < data.total; i++) {
        var row = data.rows[i];
        if (row.name == name) {
            data.rows.splice(i, 1);
            data.total--;
            break;
        }
    }
    totalCost -= price * quantity;
    $('#cartcontent1').datagrid('loadData', data);
    $('div.cart .total').html('Total: $' + totalCost);
}

function sumQuantity(data) {
    var sum = 0;
    for (var i = 0; i < data.total; i++) {
        sum += data.rows[i].quantity;
    }
    return sum;
}

I updated the bag html to include a bag id. 我更新了bag html以包含bag id。 In the onDrop function i added e.target.id to get the id of the bag. 在onDrop函数中,我添加了e.target.id以获取包的ID。

$('.bag').droppable({
    onDrop: function (e, source) {
        var name = $(source).find('p:eq(0)').html();
        var price = $(source).find('p:eq(1)').html();
        var bag = e.target.id;
        addProduct(name, parseFloat(price.split('$')[1]), bag);
        $(source.draggable).remove();
        //$('.bag').droppable('enable');
        $(this).droppable('enable');
    }
});

Pass this bag id to the addProduct function. 将此bag id传递给addProduct函数。 Se the example in http://jsfiddle.net/Vwu37/55/ Is this what you had in mind? 例如http://jsfiddle.net/Vwu37/55/中的示例,这是您想到的吗?

Edit: It looks like there is a problem when you add the same item to two different bags. 编辑:当您将相同的商品添加到两个不同的包装袋中时,似乎出现了问题。 I am not sure if you want to be able to add the same item to different bags. 我不确定您是否希望能够将同一物品添加到不同的包中。 If you want to do this you can change your addProduct function with if (row.name == name && row.bag == bag) 如果要执行此操作,可以使用if (row.name == name && row.bag == bag)更改addProduct函数。

  function addProduct(name, price, bag) {
      var totalQuantity = sumQuantity(data);

      if (totalQuantity < 10) {
          function add() {
              for (var i = 0; i < data.total; i++) {
                  var row = data.rows[i];
                  if (row.name == name && row.bag == bag) {
                      row.quantity += 1;
                      return;
                  }
              }
              data.total += 1;
              data.rows.push({
                  name: name,
                  bag: bag,
                  quantity: 1,
                  price: price,
                  remove: '<a href="#" class="remove" onclick="removeProduct(this, event)">X</a>'
              });
          }
          add();
          totalCost += price;
          $('#cartcontent1').datagrid('loadData', data);
          $('div.cart .total').html('Total: $' + totalCost);
      } else {
          alert('cannot have more than 10 items');
      }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM