jquery の sortable 関数を使用して、よくある質問リストを並べ替えています。言うまでもなく、私はこの概念に慣れていません。誰もがこれのバックエンドの良い例を持っています. フロントは正常に動作していますが、データベース内のシーケンスを更新することは別の話です。私のバックエンドはColdFusionです。
前もって感謝します
よくある質問を定義します。
<div id="faq">
<div id="q1">...</div>
<div id="q2">...</div>
(...)
<div id="q100">..</div>
</div>
よくある質問を並べ替え可能にする:
<script type="text/javascript">
$("#faq").sortable();
</script>
提出されたフォーム:
<form action="..." id="faq_form">
<input type="hidden" name="faqs" id="faqs" />
...
</form>
ソートされたシーケンスをフォームに追加
<script type="text/javascript>
$("#faq_form").submit(function() {
$("#faqs").val($("#faq").sortable('toArray'))
})
</script>
フォームが送信されると、フィールド「faqs」には、次のように#faqからコンマで区切られたIDが含まれます:q1、q3、q10、q11、q2、q100 .. ..
解析してDBに保存するだけです
Jquery UI Sortable の簡単な例を div で使用する方法を次に示します。
まず、HTML にライブラリを含めます。
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>` <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>``
ソート可能にするための HTML:
<div id="target">
<div style="cursor: move;" class="entity">
<div class="digit"><span>1</span><tab /> First Item </div>
</div>
<div style="cursor: move;" class="entity">
<div class="digit"><span>2</span> Second Item</div>
</div>
<div style="cursor: move;" class="entity">
<div class="digit"><span>3</span> Third Item</div>
</div>
<div style="cursor: move;" class="entity">
<div class="digit"><span>4</span> Fourth Item</div>
</div>
<div style="cursor: move;" class="entity">
<div class="digit"><span>5</span> Fifth Item</div>
</div>
</div>
ソート可能な関数は次のとおりです。
$(document).ready(function() {
$('#target').sortable({
items:'div.entity', //the div which we want to make sortable
scroll:true, //If set to true, the page
//scrolls when coming to an edge.
update:function(event,ui){ renumber(); } //This event is triggered when the user
//stopped sorting and the DOM position has changed.
});
});
renuber() は、Sortable 更新イベント ハンドラー コールバックから呼び出されます。
function renumber()
{
$('.digit span').each(function(index,element) {
$(element).html(index+1);
});
}