Question
I'm using the sortable function in jquery to sequence a faq list. Needless to say, i'm new to this concept. Anybody have any good examples of the backend for this. I have the front working fine, but updating the sequence in the database is another story. My backend is ColdFusion btw.
Thanks in advance
Solution
Define the faq:
<div id="faq">
<div id="q1">...</div>
<div id="q2">...</div>
(...)
<div id="q100">..</div>
</div>
Make faq sortable:
<script type="text/javascript">
$("#faq").sortable();
</script>
Form submitted:
<form action="..." id="faq_form">
<input type="hidden" name="faqs" id="faqs" />
...
</form>
Add sorted sequence to form
<script type="text/javascript>
$("#faq_form").submit(function() {
$("#faqs").val($("#faq").sortable('toArray'))
})
</script>
When form is submitted, field "faqs" will contain comma separated id's from #faq like this: q1,q3,q10,q11,q2,q100...
Just parse it and save to DB
OTHER TIPS
Here is simple example of Jquery UI Sortable,how it can be used with div's.
First include libraries in your 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 for making sortable:
<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>
Here is the sortable function:
$(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() is called from the Sortable update event handler callback:
function renumber()
{
$('.digit span').each(function(index,element) {
$(element).html(index+1);
});
}