【问题标题】:How to make a dynamically created element accept droppable elements如何使动态创建的元素接受可放置元素
【发布时间】:2013-05-09 04:39:20
【问题描述】:

我有以下代码,我在其中使用 ajax 结果创建动态元素。

function getBillInformation(Status,billingDate,User){
    var parent = "";
    var status = "";

    switch(Status){
        case "OPEN":
            parent = $('#tab-1');
            status = "O";
            break;
        case "SETTLED":
            parent = $('#tab-2');
            status = "S";
            break;
        case "CANCEL":
            parent = $('#tab-3');
            status = "C";
            break;
    }

    var billList = parent.find('#bill-list').first();
    var bill = parent.find('.bill').first();
    var billItemList = parent.find('.bill-item-list').first();
    var billItem = parent.find('.bill-item').first();
    var billPanel = parent.find('#bill-panel').first();

    var jsonObject = "billStatus=" + status + "&billingDate=" + billingDate + "&user=" + User;

    ajaxCall("/getBillingInformation","POST",jsonObject,function(response){
        billPanel.empty();

        billItemList.find('.bill-item').each(function(){
            $(this).remove();
        })

        for(var i = 0; i < response.bills.length; i++){
            var clone = bill.clone();
            clone.attr('id', response.bills[i].billId);
            clone.find('.bill-number').html(response.bills[i].billNo);
            clone.find('.amount').html(response.bills[i].billTotal + " USD");

            clone.find('.room-number').html(response.bills[i].roomNo);
            clone.find('.table-name').html(response.bills[i].tableNo);

            if(response.bills[i].itemsList.length != 0){
                for(var j = 0; j < response.bills[i].itemsList.length; j++){
                    var billItemClone = billItem.clone();
                    billItemClone.find('.bill-item-name-right').html(response.bills[i].itemsList[j].amount);
                    billItemClone.find('.bill-item-amount').html(response.bills[i].itemsList[j].qty);
                    var total = (response.bills[i].itemsList[j].amount) * (response.bills[i].itemsList[j].qty);
                    billItemClone.find('.bill-item-price').html(total + ".00 USD");
                    billItemClone.draggable({
                        revert : true,
                        zIndex: 1,
                        containment: "window",
                        opacity: 0.5,
                        cursor: "move",
                        helper: function() { return $(this).clone().appendTo('body').show(); }
                    });
                    clone.find('.bill-item-list').prepend(billItemClone);
                }
            }

            billPanel.prepend(clone);
        }
    });

}

现在我需要使bill-item-list 元素可放置。!我编写了代码来接受可放置的元素。这是代码。

$('.bill-item-list').droppable({
                    accept: '.item',
                    drop: function( event, ui ) {
                        var clone = bill.clone();
                        var droppable = $(this);
                        var jsonObject = 'itemId=' + ui.draggable.attr('id') + '&billId=' + $(this).parent().attr('id');
                        $.ajax({
                            url: "/addItemToBill",
                            type: "POST",
                            data: jsonObject,
                            dataType: "json",
                            beforeSend: function(x) {
                                if(x && x.overrideMimeType) {
                                    x.overrideMimeType("application/json;charset=UTF-8");
                                }
                            },
                            success: function(result){
                                if(result.status = true){
                                    clone.find('.bill-item-name-left').html(ui.draggable.find('.item-name').children('h4').html());
                                    clone.find('.bill-item-name-right').html(ui.draggable.find('.item-price').children('h4').html());
                                    clone.find('.bill-item-price').html(ui.draggable.find('.item-price').children('h4').html() + ".00 USD" );
                                    clone.find('.bill-item-amount').html("1");
                                    if(droppable.height() > 300){
                                        droppable.css('overflow-y','scroll');
                                        droppable.css('height', '320px')
                                    }
                                    clone.draggable({
                                        revert : true,
                                        zIndex: 1,
                                        containment: "window",
                                        opacity: 0.5,
                                        cursor: "move",
                                        helper: function() { return $(this).clone().appendTo('body').show(); }
                                    });
                                    clone.insertBefore(droppable.find('.item-drop-point')).hide().fadeIn("slow");
                                }

                            }
                        });
                    }
                });

问题是我试图在创建元素时使其可放置!但是当一个项目被丢弃时,会有一个 ajax 调用!所以任何人都可以给我一个想法,让这个动态元素可放置。

【问题讨论】:

    标签: javascript jquery jquery-ui


    【解决方案1】:

    你需要分离函数'drop'甚至处理程序,然后对于动态元素你需要再次绑定droppable插件。

    注意:您需要取消绑定之前应用的插件。

    例如:

    function drop_handler(){
        ----
        $('.bill-item-list').unbind('droppable');
        events()      // once again apply your droppable plugin
    }
    
    function events(){
        $('.bill-item-list').droppable({
            accept: '.item',
            drop: drop_handler
        })
    }
    

    【讨论】:

    • 我还没有使实时元素可放置!所以我需要解绑!
    • 在 Ajax 调用之后,您想为新创建的项目应用可放置插件,那么您需要取消绑定以前应用的可放置插件,因为 $(.classname) 将为您带来以前可用的所有元素,因此需要取消绑定否则不需要。
    • @Dimal Chandrasiri 如果我的建议对您有所帮助。请为我的帖子投票。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 2013-09-18
    • 2013-08-18
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    相关资源
    最近更新 更多