简体   繁体   中英

JQuery UI Sortable - How to sort two types of items in one container

I want to sort two types of items (different class name) in same container. Each type of items should be sorted between itself without interfere the other items type.

I try this code: but it didn't work:

$('#myItemsContainer').sortable({
    items: '.sortType1',
    start: function(event, ui) {
    },
    change: function(event, ui) {
    },
    update: function(event, ui) {
    }
});

$('#myItemsContainer').sortable({
    items: '.sortType2',
    start: function(event, ui) {
    },
    change: function(event, ui) {
    },
    update: function(event, ui) {
    }
});

HTML:

<div id="myItemsContainer">

<div class="sortType1">Item Type 1</div>
<div class="sortType1">Item Type 1</div>
<div class="sortType1">Item Type 1</div>

<div class="sortType2">Item Type 2</div>
<div class="sortType2">Item Type 2</div>
<div class="sortType2">Item Type 2</div>

</div>

As mentioned, you must contain them in their own container. Here is an example.

 $(function() { $("#myItemsContainer .list:eq(0)").sortable({ items: "> .sortType1" }); $("#myItemsContainer .list:eq(1)").sortable({ items: "> .sortType2" }); });
 #myItemsContainer { list-style-type: none; margin: 0; padding: 3px; width: 60%; border: 1px solid #000; } #myItemsContainer div[class^='sortType'] { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px; border: 1px solid #CCC; }
 <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <div id="myItemsContainer"> <div class="list"> <div class="sortType1">Item Type 1</div> <div class="sortType1">Item Type 1</div> <div class="sortType1">Item Type 1</div> </div> <div class="list"> <div class="sortType2">Item Type 2</div> <div class="sortType2">Item Type 2</div> <div class="sortType2">Item Type 2</div> </div> </div>

You can achieve such behavior by updating items option of sortable widget. And, you can update it on mouseover on the items:

<div id="myItemsContainer">

    <div class="sortType1">Item Type 1</div>
    <div class="sortType1">Item Type 1</div>
    <div class="sortType1">Item Type 1</div>

    <div class="sortType2">Item Type 2</div>
    <div class="sortType2">Item Type 2</div>
    <div class="sortType2">Item Type 2</div>

</div>
$(function() {

    var elm = $('#myItemsContainer'); // cache for performance

    elm.sortable({
        items: '> .sortType1' // start with default selector
    }).on('mouseover', 'div', function(e) {

        if ($(e.currentTarget).hasClass('sortType1')) {
            // limit items to sortType1
            elm.sortable('option', 'items', '> .sortType1');
        } else {
            // limit items to sortType2
            elm.sortable('option', 'items', '> .sortType2');
        }
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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