簡體   English   中英

jQuery UI可排序:確定項目的順序

[英]jQuery UI sortable: determining in what order the items are

以下是JavaScript的有趣用法:使用拖放重新排序項目。 我的頁面中的實現本身工作正常,但有沒有辦法確定用戶放置項目的順序?

我問,因為我想加載並保存cookie中的商品訂單。

2012年更新

完整的工作演示和來源


獲取元素的索引位置嘗試閱讀:

用於jquery的COOKIE插件:

JQUERY:

 $(function() {
    //coockie name
     var LI_POSITION = 'li_position';
       $('ul#sortable').sortable({
         //observe the update event...
            update: function(event, ui) {
              //create the array that hold the positions...
              var order = []; 
                //loop trought each li...
               $('#sortable li').each( function(e) {

               //add each li position to the array...     
               // the +1 is for make it start from 1 instead of 0
              order.push( $(this).attr('id')  + '=' + ( $(this).index() + 1 ) );
              });
              // join the array as single variable...
              var positions = order.join(';')
               //use the variable as you need!
              alert( positions );
             // $.cookie( LI_POSITION , positions , { expires: 10 });
            }
        });
     });​

HTML:

<ul id="sortable"> 
  <li id="id_1"> Item 1 </li> 
  <li id="id_2"> Item 2 </li> 
  <li id="id_3"> Item 3 </li> 
  <li id="id_4"> Item 4 </li> 
  <li id="id_5"> Item 5 </li> 
</ul>

PHP:

這只是一個例子,但你明白了:你可能想要使用數據庫而使用AJAX來獲取lis:

<?php  
//check if cookie is set..
if ( isset( $_COOKIE['li_position'] ) ) {
//explode the cockie by ";"...
$lis = explode( ';' , $_COOKIE['li_position'] );
// loop for each "id_#=#" ...
foreach ( $lis as $key => $val ) {
//explode each value found by "="...
$pos = explode( '=' , $val );
//format the result into li...
$li .= '<li id="'.$pos[0].'" >'.$pos[1].'</li>';
}
//display it
echo $li;
// use this for delete the cookie!
// setcookie( 'li_position' , null );
} else {
// no cookie available display default set of lis
 echo '
  <li id="id_1"> Fuji </li> 
  <li id="id_2"> Golden </li> 
  <li id="id_3"> Gala </li> 
  <li id="id_4"> William </li> 
  <li id="id_5"> Jordan </li> 
';
}
?>

使用toArray方法將可排序項的id序列化為字符串數組。

$( "#sortable" ).sortable('toArray');

HTML

<ul id="sortable"> 
  <li id="id1"> Item 1 </li> 
  <li id="id2"> Item 2 </li> 
  <li id="id3"> Item 3 </li> 
  <li id="id4"> Item 4 </li> 
  <li id="id5"> Item 5 </li> 
</ul>

JQUERY

 $("#sortable").sortable(
            {
                update: function () {
                var strItems = "";

                $("#sortable").children().each(function (i) {
                    var li = $(this);
                    strItems += li.attr("id") + ':' + i + ',';
                });

                updateSortOrderJS(strItems); <--- do something with this data

                }
            });

strItems看起來像(new-item-order:item-id)

0,49:1365:2,50:3,364:4366:5,39:6

然后你可以將它解析成更新函數

列出eachTask = new List(itemsList.Trim()。Split(new char [] {','}));

然后

String [] item = i.Split(new Char [] {':'});

這是我目前使用的:

<div id="sortable">
    <div id="2000"></div>
    <div id="1000"></div>
    <div id="3000"></div>
</div>

$('#sortable').sortable({
    update: function () { save_new_order() }
});

function save_new_order() {
    var a = [];
    $('#sortable').children().each(function (i) {
        a.push($(this).attr('id') + ':' + i);
    });
    var s = a.join(',');
    alert(s);
}

提醒值:

2000:0,1000:1,3000:2

我使用以下代碼來獲取項目的順序。

HTML

     <div id="sortable">
        <div >1</div>
        <div >2</div>
        <div >3</div>
        <div >4</div>
        <div >5</div>
        <div >6</div>
        </div>

JQuery的

     $(document).ready(function(){
        $("#sortable").sortable({
            stop : function(event, ui){
        $('#sortable > div').each(function(){
           alert($(this).html());
       });          
        }
    });
  $("#sortable").disableSelection();
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM