开发者

How prevent second or further more update functions when using connectedWith property in jqueryui sortable?

开发者 https://www.devze.com 2023-03-16 20:56 出处:网络
$(document).ready(function() { $(\'.sortable\').sortable( { 开发者_如何学Go connectWith:\'.sortable\',
$(document).ready(function()
{
    $('.sortable').sortable(
    {
        开发者_如何学Go connectWith:'.sortable',
         update: function()
         {
              alert('sorted');
         }
    });
});
<ul class='sortable'>
   <li>A</li>
   <li>B</li>
   <li>C</li>
</ul>
<ul class='sortable'>
   <li>1</li>
   <li>2</li>
   <li>3</li>
</ul>

Ok, this alert two times when I sort with ULs. I need one time alert even sorted with UL or Li.


Seems like everything OK: when you sort LI's inside single UL then you receive one alert. But if you move LI from one UL to another then you receive two alerts, because each sortable is updated. First loses one LI and second receives it: http://jsfiddle.net/CoolEsh/Kuwzq/1/

If you need alert only when one of the lists updated then do next:

$(document).ready(function()
{
    $('.sortable1').sortable(
    {
         connectWith:'.sortable2'
    });

    $('.sortable2').sortable(
    {
         connectWith:'.sortable1',
         update: function()
         {
              alert('sorted');
         }
    });
});
<ul class='sortable1'>
   <li>A</li>
   <li>B</li>
   <li>C</li>
</ul>
<ul class='sortable2'>
   <li>1</li>
   <li>2</li>
   <li>3</li>
</ul>


I have found a way to do it.

$updated = false;
$('.sortable').sortable(
{
    connectWith: '.sortable',
    stop: function()
    {
        if($updated)
        {
                alert('Updated!');
        }
        $updated = false;
    },
    update: function(e, ui)
    {
        $updated = true;
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消