2012-08-09 58 views
0

我對unorderd列表(ul)使用jquery draggble函數,並且在項目更改並且設置頁面的順序被加載後遇到了項目順序的問題。假設我們有下面的代碼:獲取項目的順序jquery可排序並設置順序給他們

<html> 
    <head> 
    <script src="http://code.jquery.com/jquery-latest.js"></script> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js> </script> 
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script> 
    <script> 
     $(document).ready(function(){ 

      $(".block").sortable({ 
      axis: 'y', 
      update: function(event, ui) {// order changed 
       var order = jQuery(this).sortable('toArray'); 
       // send this order to index.php and store it in db 

      } 
     }); 
     }); 
    </script> 

    </head> 
    <body> 
     <ul class="block" type="none"> 
      <li id = "one">1</li> 
      <li id = "two">2</li> 
      <li id = "three">3</li> 
      <li id = "four">4</li> 
      <li id = "five">5</li> 
     </ul> 
    </body> 

和第二個文件是

<html> 
    <head> 
    <script> 
     $('.secondblock').sortable({axis:'y'}); 
     // get order from index.php and set it to .secondblock 

    </script> 
    </head> 
    <body> 
    <ul class="secondblock" type="none"> 
      <li id = "one">1</li> 
      <li id = "two">2</li> 
      <li id = "three">3</li> 
      <li id = "four">4</li> 
      <li id = "five">5</li> 
     </ul> 
    </body> 
</html> 

是否有任何可能的解決方案?

+1

你只需要一種方法來進行排序,對吧? http://stackoverflow.com/questions/929519/dynamically-arranging-divs-using-jquery/929607#929607 – 2012-08-09 03:56:06

+1

這是記者從昨天你的問題幾乎如出一轍:http://stackoverflow.com/問題/ 11870024 /設置訂單到jQuery的排序。如果你想做些微小的改動,最好編輯一下。 – Faust 2012-08-09 04:40:27

+0

我很抱歉,但我認爲這個問題並不清楚,我剛剛添加了更復雜的解釋... – 2012-08-09 05:22:16

回答

4

如果你不想用我的評論的鏈接上的解決方案,你可以使用這個,簡單:

function reorder(orderArray, elementContainer) 
{ 
    $.each(orderArray, function(key, val){ 
     elementContainer.append($("#"+val)); 
    }); 
} 

orderArray:含在所有ID陣列爲了你想(正是排序( 「指定者」)返回)

elementContainer:是jQuery對象WHI ch包含您的可排序項目。

工作示例這裏:
http://jsfiddle.net/ecP5k/1/