【问题标题】:jquery selectable swap list itemsjquery 可选择的交换列表项
【发布时间】:2012-12-10 04:17:14
【问题描述】:

我已经看到一个关于在两个下拉列表之间交换值/项目的问题,How to swap values in select lists with jquery?

我的问题是它可以在 jquery ui-selectable 列表之间完成。您单击列表 1 中的一个元素和列表 2 中的另一个元素,然后单击交换按钮会将每个项目/ div 的内容从一个转移到另一个?

HTML:

<button>Swap Values</button>
<div style="display:inline-block">
<div id="selectable" class="mySel">
    <div value=Item1>Item1</div>
    <div value=Item2>Item2</div>
    <div value=Item3>Item3</div>
    <div value=Item4>Item4</div>
</div>

<div id="selectable2" class="mySel2">
    <div value=Item1>Item1</div>
    <div value=Item2>Item2</div>
    <div value=Item3>Item3</div>
    <div value=Item4>Item4</div>
    <div value=Item1>Item5</div>
    <div value=Item2>Item6</div>
    <div value=Item3>Item7</div>
    <div value=Item4>Item8</div>
</div>
</div>

CSS:

*{padding:6px; font-size:1.1em}

.mySel, .mySel2{
height: 300px;
width: 200px;
overflow: auto;
border: solid 2px black;
margin-bottom: 20px;
float:left;
margin:10px;
}

.mySel > DIV, .mySel2 > DIV{
height: 50px;
border: solid 1px red;
}

#selectable .ui-selecting { background: #FECA40; }
#selectable .ui-selected { background: #F39814; color: white; }
#selectable2 .ui-selecting { background: #FECA40; }
#selectable2 .ui-selected { background: #F39814; color: white; }

JQUERY 可选:

$("#selectable, #selectable2").selectable();

JQUERY 可排序:

$(".mySel, .mySel2").sortable({
    tolerance: 'pointer',
    connectWith: '.mySel, .mySel2',
    helper: 'original',
    scroll: true
});

JQUERY 交换值尝试代码:

$('button').click(function() {
    var new1 = [];

    $('.mySel, .mySel2').each(function(i) {
        new1[i] = $(this).val();
    });
    new1.reverse();
    $('.mySel, .mySel2').each(function(i) {
        $(this).val(new1[i]);
    });
});

【问题讨论】:

    标签: jquery jquery-ui jquery-ui-sortable swap selectable


    【解决方案1】:

    这将在没有 sortable() 方法的情况下工作,它似乎会干扰 selectable() 方法。

    $('button').click(function(){
       $('#selectable .ui-selected').removeClass('ui-selected').appendTo('#selectable2');
       $('#selectable2 .ui-selected').removeClass('ui-selected').appendTo('#selectable');
    });
    

    编辑:只要 sortable() 第二个运行,它将与 sortable 方法一起使用。 *编辑:回应后续问题*

    其他问题

    如何判断一个值为“Item1”的元素在移动之前是否已经存在于list2中?

    这里有一些有趣的事情要做。首先是相反列表中的相同术语是否被交换。如果是,则交换可以继续,因为两个列表将继续只有一个值。其次,如果发现重复,则脚本不能完成交换。在交换发生之前,需要检查两组列表是否重复。

    我通过结合一些复杂的 jQuery 选择器和一个单独的评估函数来解决这个问题,该函数将返回真或假,具体取决于是否存在重复项。我在下面的代码中添加了 cmets 来解释它是如何工作的。

    function testFor($o, $p) { //two collections of Items we are comparing
            var retVal = true; //By default, this script returns true, a mismatched pair results in a return: false;
            $o.each(function() { //Look at each element in the first collection
                var thisDivValue = $(this).attr('value'); //Grab it's value=""
                if ($p.parent().find('div[value=' + thisDivValue + ']').not('.ui-selected').length > 0) { 
                //Search in the opposite collection for an item with a matching value that is NOT selected for swapping, if you find it, return false.
                    retVal = false;
                    return false;
                }
                else {return true;}
            });
            return retVal; //True or false depending on what was set above.
        }
    function showError() {
        alert('DUPLICATE FOUND');
    }
    $("#selectable, #selectable2").selectable();
    $(".mySel, .mySel2").sortable({
        tolerance: 'pointer',
        connectWith: '.mySel, .mySel2',
        helper: 'original',
        scroll: true
    });
    $('button').click(function() {
        var v = $('#selectable2>div');
        var w = $('#selectable>div');
        var x = $('#selectable .ui-selected');
        var y = $('#selectable2 .ui-selected');
        if ((w.length - x.length + y.length) > 4) {return false;}
        if ((v.length - y.length + x.length) > 8) {return false;}
        if (testFor(x, y) && testFor(y, x)) { //Both of these tests must return TRUE in order for the swap to happen
            $('#selectable .ui-selected').removeClass('ui-selected').appendTo('#selectable2');
            $('#selectable2 .ui-selected').removeClass('ui-selected').appendTo('#selectable');
        }
        else {showError();}
    });​
    

    jsFiddle

    【讨论】:

    • 是的,这就是我要找的,谢谢。不过有几个问题。 1)是否可以让每个列表具有最大允许项目数。 List1 一个最大值。 4,list2 最多。 8. 这样每个列表都必须选择一个要交换的项目,而现在一个项目可以自己交换。 2)左列(list1,#selectable)能否有同一项不能在一起的限制。例如,左列不能将 item1 和 item1 放在一起吗?
    • 1) 您可以使用jQuery 为#selectable 和#selectable2 的每个子div 创建一个数组,并将它们的长度属性与.ui-selected div 的每个相应集合的长度进行比较。示例:var w = $('#selectable&gt;div').length;var x = $('#selectable .ui-selected').length;var y = $('#selectable2 .ui-selected').length;,然后在发生交换时使用if 语句计算总数:if ((w - x + y) &gt; 4) {error}
    • 2) 您可以使用 jQuery 的 .attr('value') 方法将左侧列中的现有值与右侧列中的预期交换值进行比较。如果找到匹配项,则显示错误消息。
    • 好的,再次感谢您的帮助。第一部分(1)我在你的帮助下想通了。第二部分 (2) 我已经部分完成了,但不知道如何调用列表 1 中的所有 div 值?我试过var sel1 = $('#selectable &gt; div').attr('value').children(),但它似乎只得到了第一个值,即-value=Item1。项目 2、3 和 4 仍然可以从列表 2 移动到列表 1?我还做了var sel2 = $(#selectable2 .ui-selected).attr('value'),然后在两个== 之间做了一个if 语句作为参考。
    • 我将添加另一个答案,下面有更多空间进行解释。
    猜你喜欢
    • 2017-01-30
    • 2022-01-16
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 2015-08-23
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多