joel
joel

Reputation:

Jquery Multiple Sortable List

Can someone please explain how to take the code below.

Heres a screen shot

alt text http://img196.imageshack.us/img196/9514/picture4omk.png

this is my JS

$(document).ready(function(){ 

    $(function() {
        $("#sortable1, #sortable2").sortable(

            { connectWith: '.connectedSortable', 
            opacity: 0.6, 
            cursor: 'move', 
            update: function() {
            var order = $(this).sortable("serialize"); 
            $.post("home/updateBOX", order, function(theResponse){
                $("#contentRight").html(theResponse);
            });                                                              
        }                                 
        });
    });

});

This is my current View

<div id="contentLeft">
    Category 1
    <ul id="sortable1" class="connectedSortable">
    <?php foreach($getcat1->result() as $box) :?>

            <li id="recordsArray_<?php echo $box->boxID ?>"><?php echo $box->boxID . ". " . $box->boxID . $box->boxText ?></li>
        <?php endforeach; ?>
    </ul>
    Category 2
    <ul id="sortable2" class="connectedSortable">
    <?php foreach($getcat2->result() as $box) :?>

        <li id="recordsArray_<?php echo $box->boxID ?>"><?php echo $box->boxID . ". " . $box->boxID . $box->boxText ?></li>
        <?php endforeach; ?>
    </ul>
</div>

This is my current Controller

function index()
    {

        // Boxes
        $this->db->order_by('boxListingID','ASC');      
        $this->db->where('boxListingCat',1);
        $data['getcat1'] = $this->db->get('boxes');

        $this->db->order_by('boxListingID','ASC');
        $this->db->where('boxListingCat',2);
        $data['getcat2'] = $this->db->get('boxes');


        // Initialize
        $this->layout->set('nav', $this->class);
        $this->layout->load('layout','home/home_view',$data);
    }   

    function updateBOX()
    {
        if (empty($_POST))  { return false; }
        $updateRecordsArray = $_POST['recordsArray'];
        $listingCounter = 1;
        foreach ($updateRecordsArray as $listingCounter=>$recordIDValue) {
            $this->db->set('boxListingID',$listingCounter+1)->where('boxID',$recordIDValue)->update('boxes');
        }

    }
}

Please Help!

I have been trying very hard to make the following code work so that when you are dragging a li from one UL to another it will detect that it's in a new UL and save that data. I don't know where to start

I will be very very appreciative for any help.

Upvotes: 0

Views: 2920

Answers (3)

SoluableNonagon
SoluableNonagon

Reputation: 11755

http://jsfiddle.net/Waw4V/2/

If you're looking to make multiple sortables, it is best done by attaching classes to each kind of list. The above link uses divs, but you can apply it to lists.

Lets say you have a list of questions:

<div id='questionList'>
    <div class='question'>
        <div>Question Details</div>
        <div class='answerForQuestion'>
            <div class='answer'>Answer 3
                <span>Details</span>
            </div>
            <div class='answer'>Answer 2</div>
            <div class='answer'>Answer 4</div>
        </div>
    </div>
    <div class='question'>
        <div>Question Details</div>
        <div class='answerForQuestion'>
            <div class='answer'>Answer 2</div>
            <div class='answer'>Answer 7</div>
            <div class='answer'>Answer 11</div>
        </div>
    </div>
</div>

Here's what you do in jQuery:

$('#questionList').sortable(); // this makes one each major category sortable
$('.answerForQuestion').sortable({  // this makes sub categories sortable
    connectWith: ['.answerForQuestion']
    change: // this fires everytime the item you drag is moved, put in your logic here, such as an ajax call to update your database
});

Upvotes: 0

miCRoSCoPiC_eaRthLinG
miCRoSCoPiC_eaRthLinG

Reputation: 2960

Yup! Draggable and Droppable should do the trick... You can use the drop() method of droppable to update the list state for both.

Upvotes: 0

Robert Harvey
Robert Harvey

Reputation: 180777

What about using the draggable and droppable plugins in jQuery UI?

The Draggable plugin makes selected elements draggable by mouse.

the Droppable plugin provides a drop target for draggables.

Upvotes: 4

Related Questions