Reputation: 4763
I checked these:
Jquery draggable/droppable and sortable combined
jQuery UI: sortable and draggable + sortable and droppable
JQuery Draggable + Droppable + Sortable
So, answers are none of these.
Theory
Problem
What have you tried ?
$(function() {
$( ".fetchedfromdb li" ).draggable({
appendTo: "body",
helper: "clone",
drag: function(){
$(".sortintodb ol").droppable({
activeClass: "ui-state-default",
hoverClass: "ui-state-hover",
accept: ":not(.ui-sortable-helper)",
drop: function(event, ui) {
$( this ).find( ".placeholder" ).remove();
$( "<li></li>" ).text( ui.draggable.text() ).appendTo( this );
}
}).sortable({
sort: function() {
$( this ).removeClass( "ui-state-default" );
}
});
}
});
});
My COdeIgniter part:
<?php
echo form_open('/data/process');
echo form_label('yep') . form_textarea('remarks');
foreach($dataFetched as $data => $field) {
echo "<h2>$data</h2> \n <ul class='fetchedfromdb'>";
foreach($field as $f):
$fieldFetch = $data.'_1_1';
echo '<li>'.$f->$fieldFetch.'</li>';
echo "<br />";
endforeach;
echo '</ul>';
echo '<div style="background-color: #c3c3c3; height:100px">';
echo "<ol class=\"sortintodb\" id=\"$data\">";
echo '<li class="placeholder">Drop here</li>
</ol>
</div><hr />';
}
echo form_submit('submit','Submit');
echo form_close();
?>
via CSS I have made sure that every output till parent foreach ends comes under same section
Any ideas about how can this be implemented?
Any help, much appreciated. Thank you :)
'Visual' representation of what we are looking at...
Upvotes: 4
Views: 8382
Reputation: 3848
Please check this updated fiddle and let me know if any extra behavior required
$(function() {
$('.sortable').sortable().disableSelection();
$( ".draggable li" ).draggable({
revert: true
});
$('.droppable').droppable({
drop: function(e, ui) {
var el = $('<li></li>').text($(ui.draggable).text()).appendTo($(this));
if($(ui.draggable).parent().find('li').size() == 1)
$(ui.draggable).parent().addClass('empty');
else
$(ui.draggable).parent().removeClass('empty');
$(ui.draggable).remove();
if($(this).find('li').size() == 1)
$(this).addClass('empty');
else
$(this).removeClass('empty');
if($(this).is('.sortable')) {
$(this).sortable( "refresh" );
$(this).sortable( "refreshPositions" );
} else {
$(el).draggable({
revert: true
});
}
},
accept: function(draggable) {
return $(draggable).parent().data('section') == $(this).data('section') && !$(draggable).parent().is($(this));
}
});
});
Upvotes: 3
Reputation: 1061
Here is Working demo. Jsfiddle Demo
HTML
<ul class='fetchedfromdb' id="da1"> //parent element
<li id="1">Data1</li>
<br />
<li id="2">Data2</li>
<br />
<li id="3">Data3</li>
<br />
<div style="background-color: #c3c3c3; height:100px">
<ol class="sortintodb" id="e201">
<li class="placeholder" id="9">Drop here</li>
</ol>
</div>
</ul>//ends
<hr />
<h2>e202</h2>
<ul class='fetchedfromdb'>
<li id="5">Data1</li>
<br />
<li id="6">Data2</li>
<br />
<li id="7">Data4</li>
<br />
<div style="background-color: #c3c3c3; height:100px">
<ol class="sortintodb" id="e202">
<li class="placeholder" id="8">Drop here</li>
</ol>
</div>
</ul>
<hr />
Script
$(".fetchedfromdb li").draggable({
containment: 'parent',
helper: "clone",
connectToSortable: '.sortable',
});
$(".sortintodb").droppable({
activeClass: "ui-state-default",
hoverClass: "ui-state-hover",
accept: ":not(.ui-sortable-helper)",
drop: function(event, ui) {
var self = $(this);
//if you don't want same "data" in placeholder more than once
self.find(".sortintodb").remove();
var dropId = ui.draggable.attr('id');
if (self.find("[id=" + dropId + "]").length) return;
$("<li></li>", {
"text": ui.draggable.text(),
"id": dropId
}).appendTo(this);
},
});
$('.sortintodb').sortable({
placeholder: "ui-state-highlight",
});
Upvotes: 4
Reputation: 4024
so i am guessing you want to have some code that updates the database on what you have ordered/dropped?
in your jquery .drop & .sort functions have some code that runs a jquery ajax to post the data to the controller. the controller can then get your model to re-order the data to mimic what is shown on the screen.
below is code i used to do this as an example. so in general terms it gets some data, sends it to the server, and tidies up the local screen copy. my ajax_post method is pretty complicated, but its function is to send data to the server, and process the replies.
drop: function( event, ui ) {
var row = ui.draggable.context.id;
var data = {id:row.substr(row.lastIndexOf('_')+1),mr_id:o.mr_id,type:o.type}
ajax_post('patients/row_remove',data);
$('#'+row).remove();
setup_row_banding(o.container + ' .med_row');
} // end drop
Upvotes: 0