通过desc顺序设置的jQuery UI shortable

时间:2018-11-16 14:16:42

标签: javascript jquery jquery-ui short

我这里有一个列表,我想按照DESC的顺序做空。 像10,9,8,7,6,5,4,3,2,1 我的目标是要更改的数据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

2 个答案:

答案 0 :(得分:1)

您可以使用this question中的解决方案以降序排列,只需更改回调中的元素顺序(将a.dataset.id中的b.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>

答案 1 :(得分:0)

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

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame()
df ['Xstart'] = [1,2,3,4,5]
df ['Xend'] = [6,8,9,10,12]
df ['Ystart'] = [0,1,2,3,4]
df ['Yend'] = [6,8,9,10,12]

fig = plt.figure()
ax = fig.add_subplot(111)
for i in range (len(df)):
    ax.plot(
        (df.iloc[i]['Xstart'],df.iloc[i]['Xend']),
        (df.iloc[i]['Ystart'],df.iloc[i]['Yend'])
    )
plt.show()

解决的代码在这里

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;}