views:

1493

answers:

2
+4  Q: 

jQuery sortables

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

+11  A: 

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

Tomasz Tybulewicz
To anyone who's a newbie like me, dont forget to put your Jquery code in a function ie $(function() { //your code }); This might be wrong / obvious, but i couldnt get the above to work until i did it.
Rob Y
A: 

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"&gt;&lt;/script&gt;`&lt;script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"&gt;&lt;/script&gt;``

Html for making sortable:

<div id="target">
    <div style="cursor: move;" class="entity">
        <div class="digit"><span>1</span><tab />&nbsp; First Item </div>            
    </div>      
    <div style="cursor: move;" class="entity">
        <div class="digit"><span>2</span>&nbsp; Second Item</div>           
    </div>      
    <div style="cursor: move;" class="entity">
        <div class="digit"><span>3</span>&nbsp; Third Item</div>            
    </div>
    <div style="cursor: move;" class="entity">
        <div class="digit"><span>4</span>&nbsp; Fourth Item</div>           
    </div>
    <div style="cursor: move;" class="entity">
        <div class="digit"><span>5</span>&nbsp; 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);
    });
}
Mohammad Faheem