Sortable list + ability to re-order each item by inputting rank #

前端 未结 2 976
深忆病人
深忆病人 2021-02-01 10:12

I\'ve searched and searched about how to do this, but to no avail.

Basically I have a pretty standard jQuery sortable list, using a gripper to allow users to rearrange t

2条回答
  •  清歌不尽
    2021-02-01 10:49

    jsfiddle:

    http://jsfiddle.net/pMcmL/6/

    HTML:

    • Item 1
    • Item 2
    • Item 3
    • Item 4
    • Item 5
    • Item 6
    • Item 7

    CSS:

    http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/themes/base/jquery-ui.css

    +

    li {
        margin: 1px;
        width: 130px;
        padding:2px;
        vertical-align:middle;
    }
    
    li span {
        color: gray;
        font-size: 1.1em;
        margin-right: 5px;
        margin-left: 5px;
        cursor: pointer;
        height:100%;
    }
    
    input[type="text"] {
        width: 32px;
        margin-right: 5px;
        border: 1px solid lightgay;
        color: blue;
        text-align: center;
    }
    

    Javascript:

    sort_ul = $('#sortable');                  // * sortable 
      itemsCount = $('#sortable li').length; // * total number of items function updateIndexes() { // * function to update $('#sortable li input').each( // items numbering function(i) { $(this).val(i + 1); }); } updateIndexes(); // * start by update items numbering sort_ul.sortable({handle: 'span', // * apply 'sortable' to
        stop: function(event, ui){ updateIndexes(); // * when sorting is completed, } // update items numbering }); $('#sortable li input').keyup( // * watch for keyup on inputs function(event) { if (event.keyCode == '13') { // * react only to ENTER press event.preventDefault(); // * stop the event here position = parseInt($(this).val());// * get user 'new position' li = $(this).parent(); // * store current
      • to move if (position >= 1 // * proceed only if && position <= itemsCount){ // 1<=position<=number of items li.effect('drop', function(){ // * hide
      • with 'drop' effect li.detach(); // * detach
      • from DOM if (position == itemsCount) sort_ul.append(li); // * if pos=last: append else // else: insert before position-1 li.insertBefore($('#sortable li:eq('+(position - 1)+')')); updateIndexes(); // * update items numbering li.effect('slide'); // * apply 'slide' effect when in }); // new position }else{ li.effect('highlight'); } // * if invalid position: highlight }}});

提交回复
热议问题