简体   繁体   English

从拖动的元素获取HTML5自定义数据属性

[英]Get HTML5 custom data attributes from dragged element

I have two lists: one just for getting elements (store list) and drag them into the other list (shopping cart). 我有两个列表:一个仅用于获取元素(商店列表)并将它们拖到另一个列表(购物车)中。

The shopping cart list stores one elemenet of each type dropped there, and if more than one elemenet of the same type is dragged there, I want the Twitter Bootstrap badge to be incremented, and avoid a new duplicate entry to be stored. 购物车列表中存储了每种放置的一个elemenet,如果将多个相同类型的elemenet拖到那里,我希望Twitter Bootstrap徽章增加,并避免存储新的重复条目。

HTML: HTML:

<div class="panel panel-primary">
    <div class="panel-heading">Fruit store</div>
    <div class="panel-body">
        <ul id="fruit-list" class="list-group">
            <li href="#" class="list-group-item" data-type="apple">Apple</li>
            <li href="#" class="list-group-item" data-type="pear">Pear</li>
            <li href="#" class="list-group-item" data-type="banana">Banana</li>
            <li href="#" class="list-group-item" data-type="watermellon">Watermellon</li>
        </ul>
    </div>
</div>
<div class="panel panel-primary">
    <div class="panel-heading">Shopping cart</div>
    <div class="panel-body">
        <ul id="cart-list" class="list-group">
            <li href="#" class="list-group-item" data-type="banana">Banana
                <div class="pull-right">
                    <span id="badge" class="badge">5</span>
                </div>
            </li>
            <li href="#" class="list-group-item" data-type="pear">Pear
                <div class="pull-right">
                    <span id="badge" class="badge">2</span>
                </div>
            </li>
        </ul>
    </div>
</div>

JS: JS:

// Create fruit store list
var list_element = document.getElementById("fruit-list");
var fruit_list = new Sortable(list_element, {
    group: {
        name: "fruit_group",
        pull: 'clone',
        put: false
    },
    sort: false,
    ghostClass: "droppable-area",
});

// Create shopping cart list

    var cart_list_element = document.getElementById("cart-list");
    var cart_list = new Sortable(cart_list_element, {
        group: {
            name: "fruit_group",
            pull: true,
            put: true
        },
        // Element is dropped into the list from another list
        onAdd: function (event) {
            console.log(event.item)
            // First check if there is an equal fruit at the list already. In that case, increment badge number
            var draggedElement = event.item;
            var type = draggedElement.data('type');

            // Iterate existing elements
            $("#cart-list li").each(function () {
                var existingType = $(this).data('type');

                if (existingType == type){
                    var numberOfFruits = event.item.closest('span').innerHTML;
                    console.log(numberOfFruits + ' ' + type + ' already at the list, updating badges');
                    // Increment number of this fruit
                    (event.item.closest('span').innerHTML)++;
                    return false; //break statement
                } else {
                    console.log('New fruit inserted to cart list');
                }
            });
        },
    });

It is not working, and the problem according to Firebug is the read of the HTML5 custom data field data-type . 它不起作用,并且Firebug认为问题在于读取HTML5自定义数据字段data-type I need to compare this data-type field to those already at the list to detect duplicates (this is a small executable example of a bigger application, where I need to compare more than one custom data field, so comparing the text of the list entry is not enough for me; it has to be the custom data field). 我需要将此data-type字段与列表中已经存在的data-type字段进行比较以检测重复项(这是一个较大的应用程序的小型可执行示例,在这里我需要比较多个自定义数据字段,因此需要比较列表条目的文本对我来说还不够;它必须是自定义数据字段)。

As you can see, I'm trying to read the dropped item getting if from the onAdd function event (and according to FireBug , the item contained into event is the dropped item with its custom data field, so this has to be ok), and I'm trying to retrieve the data field with .data() . 如您所见,我试图读取从onAdd函数事件中获取的被删除项(根据FireBug ,事件中包含的项是具有其自定义数据字段的被删除项,因此必须可以),我正在尝试使用.data()检索数据字段。 I have read that .data() does create a copy of the value, so it is a better idea to use .attr() when modifing the value, but this example is quite dummy and does not require to modify the custom data field, so it should be enough. 我已经读到.data()确实会创建值的副本,因此在修改值时使用.attr()是一个更好的主意,但是此示例是相当虚构的,不需要修改自定义数据字段,所以应该足够了

Here is my JsFiddle . 这是我的JsFiddle Any idea what's wrong? 知道有什么问题吗?

我找到了答案:我正在使用Jquery,应该使用普通的JavaScript:

var type = event.item.getAttribute("data-type");

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

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