Jump to content

-1 down vote favorite I am using the dragdrop functionality on a page which also updates the database.Drag and drop is working fine But i am havin


piyush23424

Recommended Posts

I am using the dragdrop functionality on a page which also updates the database.Drag and drop is working fine But i am having difficulty to determine which events to use to update the database. Here is my code

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Sortable - Portlets</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.1/jquery-ui.js"></script>
<!--<link rel="stylesheet" href="/resources/demos/style.css">-->
<style>
body {
min-width: 520px;
}
.interview-questions-column {
width: 400px;
float: left;
padding-bottom: 100px;
}

.question-bank {
width: 500px;
float: left;
padding-bottom: 100px;
}
.portlet {
margin: 0 1em 1em 0;
padding: 0.3em;
}
.portlet-header {
padding: 0.2em 0.3em;
margin-bottom: 0.5em;
position: relative;
}
.portlet-toggle {
position: absolute;
top: 50%;
right: 0;
margin-top: -8px;
}
.portlet-content {
padding: 0.4em;
}
.portlet-placeholder {
border: 1px dotted black;
margin: 0 1em 1em 0;
height: 50px;
}



.sortable {
border: 1px solid #eee;
width: 95%;
min-height: 20px;
list-style-type: none;
margin: 0;
padding: 5px 0 0 0;
float: left;
margin-right: 10px;
}
.sortable li {
margin: 0 5px 5px 5px;
padding: 5px;
font-size: 1.2em;
width: 95%;
}

.bank-quertion-sotrable {
    border: 1px solid #eee;
width: 150px;
min-height: 20px;
list-style-type: none;
margin: 0;
padding: 5px 0 0 0;
float: left;
margin-right: 10px;

}

.bank-quertion-sotrable li{
    margin: 0 5px 5px 5px;
padding: 5px;
font-size: 1.2em;
width: 200px;

}
.ui-state-default { height: 1.5em; line-height: 1.2em; }


</style>
<script>
$(function() {
$( ".interview-questions-column" ).sortable({
connectWith: ".interview-questions-column",
handle: ".portlet-header",
cancel: ".portlet-toggle",
placeholder: "portlet-placeholder ui-corner-all"
});

$( ".portlet" )
.addClass( "ui-widget ui-widget-content ui-helper-clearfix ui-corner-all" )
.find( ".portlet-header" )
.addClass( "ui-widget-header ui-corner-all" )
.prepend( "<span class='ui-icon ui-icon-minusthick portlet-toggle'></span>");

$( ".portlet-toggle" ).click(function() {
var icon = $( this );
icon.toggleClass( "ui-icon-minusthick ui-icon-plusthick" );
icon.closest( ".portlet" ).find( ".portlet-content" ).toggle();
});
});


$(function() {
    $( ".sortable" ).sortable({
         placeholder: "ui-state-default",
         connectWith: ".connectedSortable",
         /*receive: function(event, ui) {
           // only perform for sub drop downs
              if($(this).hasClass('question_bank'))
               {
                   // if the item doesn't have the matching sub class
                   if (!$(this).hasClass($(ui.item).attr('class')))
                   {
                       // cancel the sortable
                       $(ui.sender).sortable('cancel');
                   }
               }
                alert('receive');
                alert(ui.sender.attr("dataid"));
                alert(ui.sender.toSource());
            //  alert("[" + this.id + "] received [" + ui.item.attr("id") + "] from [" + ui.sender.attr("id") + "]");

        },*/

        update: function (event, ui) {
            //serial = $('#sortableF').sortable('serialize');
            var data = $(this).sortable('serialize');
            alert(data);
                 //alert('update sortable');
                 //alert("[" + this.id + "] received [" + ui.item.attr("id") + "] from [" + ui.sender.attr("id") + "]");
        // POST to server using $.post or $.ajax
        /*$.ajax({
            data: 'test',
            type: 'POST',
            url: '/your/url/here'
        });*/
    }




    }).disableSelection();
});


$(function() {
$( ".draggable" ).draggable({
connectToSortable: ".sortable",
helper: "clone",
/*revert: function(valid) {
        if(valid) {

            alert("drop is valid");
            //Dropped in a valid location
        }
        else {
            alert("drop is invalid");
            //Dropped in an invalid location
        }
       // return !valid;
      return false;
    }*/
stop: function( event, ui ) {
    // alert(ui);
    // alert("[" + this.id + "] received [" + ui.item.attr("id") + "] from [" + ui.sender.attr("id") + "]");

    }   

});


});
    $( "ul, li" ).disableSelection();
</script>
</head>
<body>
<div class="interview-questions-column">

<div class="portlet">
<div class="portlet-header">Feeds</div>
    <div class="portlet-content">
        <ul id="sortableF" class="connectedSortable sortable">
            <li class="ui-state-default" id="item-1">Item 1</li>
            <li class="ui-state-default" id="item-2">Item 2</li>
            <li class="ui-state-default" id="item-3">Item 3</li>
            <li class="ui-state-default" id="item-4">Item 4</li>
            <li class="ui-state-default" id="item-5">Item 5</li>
        </ul>
    </div>
</div>


    <div class="portlet">

    <div class="portlet-header">News</div>
    <div class="portlet-content">  
        <ul id="sortableS" class="connectedSortable sortable">
            <li class="ui-state-default" id="item-6">Item 1</li>
            <li class="ui-state-default" id="item-7">Item 2</li>
            <li class="ui-state-default" id="item-8">Item 3</li>
            <li class="ui-state-default" id="item-9">Item 4</li>
            <li class="ui-state-default" id="item-10">Item 5</li>
        </ul>
    </div>

    </div>




    <div class="portlet">
    <div class="portlet-header">Shopping</div>
    <div class="portlet-content">

     <ul id="sortableS" class="connectedSortable sortable">
            <li class="ui-state-default" id="item-11">Item 1</li>
            <li class="ui-state-default" id="item-12">Item 2</li>
            <li class="ui-state-default" id="item-13">Item 3</li>
            <li class="ui-state-default" id="item-14">Item 4</li>
            <li class="ui-state-default" id="item-15">Item 5</li>
        </ul>

    </div>
    </div>




    <div class="portlet">
    <div class="portlet-header">Links</div>
    <div class="portlet-content">

     <ul id="sortableS" class="connectedSortable sortable">
            <li class="ui-state-default" id="item-16">Item 1</li>
            <li class="ui-state-default" id="item-17">Item 2</li>
            <li class="ui-state-default" id="item-18">Item 3</li>
            <li class="ui-state-default" id="item-19">Item 4</li>
            <li class="ui-state-default" id="item-20">Item 5</li>
        </ul>

    </div>
    </div>



    <div class="portlet">
    <div class="portlet-header">Images</div>
    <div class="portlet-content">

      <ul id="sortableS" class="connectedSortable sortable">
            <li class="ui-state-default" id="item-21">Item 1</li>
            <li class="ui-state-default" id="item-22">Item 2</li>
            <li class="ui-state-default" id="item-23">Item 3</li>
            <li class="ui-state-default" id="item-24">Item 4</li>
            <li class="ui-state-default" id="item-25">Item 5</li>
        </ul>

    </div>
    </div>

</div>

<div class="question-bank">

<div class="portlet">
    <div class="portlet-header">Question bank</div>
        <div class="portlet-content">
           <ul id="question_back" class="connectedSortable bank-quertion-sotrable">
                <li class="ui-state-default draggable" id="item-26">Item 1</li>
                <li class="ui-state-default draggable" id="item-27">Item 2</li>
                <li class="ui-state-default draggable" id="item-28">Item 3</li>
                <li class="ui-state-default draggable" id="item-29">Item 4</li>
                <li class="ui-state-default draggable" id="item-30">Item 5</li>
            </ul>
         </div>
    </div>

</div>


</body>
</html>

Basically above code will form few boxes with items(which are actually questions) in it. There is one box on the right side with heading "Question Bank". From the "Question Bank" we can drag the questions to other boxes but can't do the reverse(this is also working). Also we can sort the questions within the box or drag the questions from one box to another.

Box heading are the categories and items are questions associated to that categories.

What i need is :

  1. Event that updates the database when we drag the questions from "Question Bank" to any box on the left side. Please note Questions in the "Question Bank" are pre created coming from database.
  2. Event that updates the database when we sort the questions within the box.
  3. Event that updates the database when we drag the question from one box to another box. it should also do the sorting for that two boxes.
  4. Event that updates the database when we sort the boxes.

Here is the JS Fiddle link jsfiddle.net/6o30rrzx Question Bank is comming on the bottom of the page instead of on the right side

 

any advice and suggestions will be greatly appreciated Thank you for reading this post.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.