ぺんぎんらぼ

お笑いとマンガ好きなしょぼしょぼWeb系エンジニアの日記です。たまに絵を描きます。

お笑いとマンガ好きなしょぼしょぼWeb系エンジニアの日記です

jQueryUIのsortable()メソッドの使い方

jQueryUIのsortable()メソッドの、以下3つのオプションとイベントの検証コードです。

  • connectWithオプション 別の並び替え要素のセレクタを指定することで、リスト項目が接続(連携)されます。
  • recieveイベント 接続リストが他のリストから並び替え項目を受け取った際にトリガされます。
  • removeイベント 並び替え項目がリストからドラッグされて別のリストに移動された際にトリガされます。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>sortable()検証</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>
<script type="text/javascript">
 
 
   $(function() {
       $("#firstArea").sortable({
           "revert" : true,
           "connectWith" : "#secondArea", // first→secondへの移動が可能になる
           "receive" : function(event, ui) {
               console.log("#firstArea receive イベント");
           },
           "remove" : function(){
               console.log("#firstArea remove イベント"); // first→secondへの移動完了時に発動
           }
       });
       $("#secondArea").sortable({
           "revert" : true,
           "connectWith" : "#firstArea", // second→firstへの移動が可能になる
           "receive" : function(event, ui) {
               console.log("#secondArea receive イベント");
           },
           "remove" : function(){
               console.log("#secondArea remove イベント"); // second→firstへの移動完了時に発動
           }
       });
   });
</script>
</head>
<body>
    <div id="firstArea" class="area" style="border: 1px dotted black; padding: 1em;">
        <div style="border: 1px solid black;">A</div>
        <div style="border: 1px solid black;">B</div>
        <div style="border: 1px solid black;">C</div>
    </div>
    <hr />
    <div id="secondArea" class="area">
        <div style="border: 1px solid blue;">D</div>
        <div style="border: 1px solid blue;">E</div>
        <div style="border: 1px solid blue;">F</div>
    </div>
    
</body>
</html>
````