jQuery UI shortable 按 desc 顺序设置

jQuery UI shortable set by desc order

我这里有一个列表,我想按照 DESC 顺序做空。 像 10,9,8,7,6,5,4,3,2,1 我的目标是更改 data-id。但这里是从最低值开始的。我想从上限开始

jQuery('.photos').sortable({
    stop: function(event, ui){
        let myIncrementor = GetLowestDataIdValue(); //Custom value returned from function
        $(".ui-sortable li").each(function(i, el){
               $(el).attr('data-id',myIncrementor);
               myIncrementor += 1; //increment it
        });
    }
});

function GetLowestDataIdValue(ui){
    let lowestVal = -1;
    $('.photos.ui-sortable').find('li').each(function(i, el){
         if(parseInt($(el).attr('data-id')) < lowestVal || lowestVal === -1){
              // alert(lowestVal);
               lowestVal = parseInt($(el).attr('data-id'));
         }
    });

    return lowestVal;
}
#sortable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
#sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px; cursor:move; }
#sortable li span { position: absolute; margin-left: -1.3em; }
#sortable li.fixed{cursor:default; color:#959595; opacity:0.5;}
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
<ul class="photos ui-sortable">
    <li class="photo" data-id="21">pawal</li>
    <li class="photo" data-id="22">sigma</li>
    <li class="photo" data-id="23">notea</li>
    <li class="photo" data-id="24">missouri</li>
    <li class="photo" data-id="25">Ooops</li>
    <li class="photo" data-id="26">Ratee</li>
</ul>

jsfiddle

我解决了我的问题。 这里我用了

myIncrementor -= 1;

解决的代码在这里

jQuery('.photos').sortable({
    stop: function(event, ui){
        let myIncrementor = GetLowestDataIdValue(); //Custom value returned from function
        $(".ui-sortable li").each(function(i, el){
               $(el).attr('data-id',myIncrementor);
               myIncrementor -= 1; //increment it
        });
    }
});

function GetLowestDataIdValue(ui){
    let lowestVal = +1;
    $('.photos.ui-sortable').find('li').each(function(i, el){
         if(parseInt($(el).attr('data-id')) > lowestVal || lowestVal === +1){
              // alert(lowestVal);
               lowestVal = parseInt($(el).attr('data-id'));
         }
    });

    return lowestVal;
}
#sortable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
#sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px; cursor:move; }
#sortable li span { position: absolute; margin-left: -1.3em; }
#sortable li.fixed{cursor:default; color:#959595; opacity:0.5;}
<ul class="photos ui-sortable">
<li class="photo" data-id="26">Ratee</li>
<li class="photo" data-id="25">Ooops</li>
 <li class="photo" data-id="24">missouri</li>
 <li class="photo" data-id="23">notea</li>
  <li class="photo" data-id="22">sigma</li>
    <li class="photo" data-id="21">pawal</li>
   
    
   
    
    
</ul>

您可以使用 this question 中的解决方案按降序排序,只需更改回调中元素的顺序(从 b.dataset.id 中减去 a.dataset.id

$('.ui-sortable li').sort(function(a,b) {
     return parseInt(b.dataset.id) - parseInt(a.dataset.id);
}).appendTo('.photos');
#sortable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
#sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px; cursor:move; }
#sortable li span { position: absolute; margin-left: -1.3em; }
#sortable li.fixed{cursor:default; color:#959595; opacity:0.5;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
<ul class="photos ui-sortable">
    <li class="photo" data-id="21">pawal</li>
    <li class="photo" data-id="22">sigma</li>
    <li class="photo" data-id="23">notea</li>
    <li class="photo" data-id="24">missouri</li>
    <li class="photo" data-id="25">Ooops</li>
    <li class="photo" data-id="26">Ratee</li>
</ul>