How to drag and drop table rows to swap them using JavaScript? - javascript

I am trying to swap table rows using JQuery UI. It works well but I just could not get the row number during the drag and drop event. I need to know the row number for my following work, could someone help me?
eg.
$('#table1 tbody').sortable({
revert: true,
start: function(event, ui) {
// get row number, such as (row) 1
},
stop: function(event, ui) {
// get row number, such as (row) 3
}});
In fact, I do not need to sort table rows (just swap), if there are any possible solutions, please let me know...

Wrap the tr elements inside tbody
Add a helper clone when dragging a tr. Only way you can make the drag.
When the dragging starts, a clone is made (because of helper: "clone") and the c variable will hold reference to the clone and the tr being dragged. When the dragging stops, if it is outside a droppable, the clone is destroyed. If it is inside the draggable, we destroy the clone and the tr (so it is removed from the list and can't be dragged again)

Using stop event you can get row number of drag-gable rows.
stop: function(ui, event){
var id = event.item.index();
alert(id);
}
Try this
JsFiddle:
http://jsfiddle.net/Vn5SZ/31/

First of all no need to get the row number on start and stop since it will be same .
and to know the row number just give data attribute to your rows as:
data-rownumber=1
than try this
$('#table1 tbody').sortable({
revert: true,
stop: function(event, ui) {
var row=ui.draggable.attr('data-rownumber');
//this will be the dragged row id
}});

Related

jQuery Sortable connectWith but preserve initial order on second list

I'm using jQuery sortable with connectWith to move items from list A to list B. List A has several items (100+). List B only has 15 items (numbered 1-15). I want list B to be sortable but when dragging items from List A, items want to go to the top with a quick drag. Ie if if I grab an item in list A and that items is below list B (before sorting) it wants to be placed in position 1.
I want to maintain the original order in this case. So if I drag something without sorting/positioning, it defaults to the end of the list.
I feel like I need to work with 'start' and 'drop', since start immediately causes the list to shift down when dragging.
Here is my JS
$( "#sortable1, #sortable2" ).sortable({
connectWith: ".connectedSortable",
cursor: 'move',
update: function(event, ui) {
var order = $("#sortable2").sortable("toArray");
$('#selected').val(order.join(","));
},
receive: function(event, ui) {
if ($("#sortable2").sortable("toArray").length > 15) {
$(ui.sender).sortable('cancel');
swal("You can only select 15!", "", "error");
}
}
}).disableSelection();
I was able to get this to work with:
helper: 'clone',

Updating binding with jQuery sortable?

I have a sortable list of fixtures that is populated from an observableArray using the foreach binding. One of the parameters in each fixture in the array is position.
That position reflects the order that the fixtures are sorted in. By default when the fixtures are inserted using foreach, the position numbers are in the correct order, but when you sort the list by dragging the items, the position number is updated using jQuery:
$( "#picks" ).sortable({
revert: true,
placeholder: "placeholder",
containment: 'parent',
axis: "y",
handle: '.dragHandle',
update: function() {
for (var i = 1; i <= $('#picks li').length; i++) {
$('#picks li:nth-child('+i+')').find('span.num').text(i);
}
}
});
So, the position numbers do change, but since the numbers are updated without referencing the observable, the observable itself isn't updated. My question is, how can I update the observable using the update function tied to the jQuery sortable.
I do not want make the number an input, so using an input with a value binding was not an option!
Demo Fiddle
You need a binding handler for your sortable. If you mess with the DOM outside of a binding handler, you have problems like this. The author even provides links to some demo fiddles at the bottom of the github page.

jquery ui sortable cannot drop cell into newly visible div

Simplest to see in a jsfiddle
Links to jsfiddle must be accompanied by code, therefore I am copying the js here:
$('.row').sortable({
placeholder: 'cell placeholder',
forcePlaceholderSize: true,
items: '.cell',
connectWith: '.row',
dropOnEmpty: true,
distance: 0.5,
tolerance: "pointer",
start: function() {
$('.row').css('min-height','30px');
},
stop: function(event, ui){
$('.row').css('min-height','');
}
});
the problem is, if I have two or more rows and one of them is being made visible after the cell is clicked on to being the drag, then the empty row cannot be dropped into unless the call is first hovered over another non-empty possible destination.
To see this in practice, click on the cell "iop" and try and drag it down into the thrid row. It doesn't work. BUT, if you click on the cell "iop", drag it up into the first row and then - without releasing it - back down into the third row, it does!
I presume that this has something to do with when sortable builds its internal list of possible targets. But, is there any way to make the empty row visible only once the dragging has started, and still have it targetable?
Refreshing sortable seems to do it:
$('.row').sortable('refresh');
https://jsfiddle.net/kfdmn9nm/

jQuery Draggable cannot highlight for copy and paste

I make use of jQuery UI's Draggable functionality to make it so you can click and drag a table row. Unfortunately when I want to highlight the text to copy the data in one of the table cells, it starts dragging the row away. So I used the "distance" option like so:
$('.test').draggable({
revert: 'invalid',
helper: 'clone',
distance: 150,
start: function(event, ui) {
}
});
However my problem is that it still won't let me "highlight" the table row, even if it is prior to "dragging" it. :(
Oh btw, I'm just using a regular HTML table, IE:
<table>
<tr class="test"><td>Data</td></tr>
<tr class="test"><td>Data</td></tr>
</table>
Another Q&A on this topic has a solution and it explains the way to run disableSelection on all the elements within your items that are selectable to make it possible to select text for copy and paste and other uses. So for your problem, you could do:
$('td').disableSelection();

jQuery UI - Combining Selectable with Draggable

As the question states, I'm trying to combine jQuery UI's Selectable and Draggable functions into a working drag-n-drop interface with the user being able to select multiple divs and move those divs into jQuery Droppables.
Here is the code I have to allow the user to select divs across a row in a table:
$('tr').selectable({
filter: 'div',
selected: function(event, ui) {
$(ui.selected).draggable({
containment: 'document',
axis: 'y',
snap: true,
snapMode: 'inner',
revert: 'invalid',
opacity: 0.8,
drag: function(e, ui) {
$('.ui-selected').css({
top : ui.position.top //Y-Axis Only
});
$('.ui-selected').addClass('ui-draggable-dragging');
},
stop: function() {
$('.ui-selected').removeClass('ui-selected ui-draggable');
}
});
}
});
As you can see, I've tried adding the known classes that jQuery UI adds to elements being dragged/selected/etc, but the only element that seems to act as an actual draggable is the one that the user has clicked on (is actually dragging).
My desire is to have the user drag the entire selected group, and have them all act as draggables (ie: revert on invalid, snap to droppable, etc)
Does anyone have any idea how to do this or where to go from here?
Also, here is a jsfiddle to demonstrate exactly what I mean (and where I'm currently at on this). You might have to resize the results view so the table cells are all the same width.
Simulate the behavior.
when selecting multiple item just mark them as selected manually (e.g. checkbox, add custom style, etc)
when dragging use draggable option helper to define custom clone helper : function(){
return "<p>custom item</p>"
},
this will hide individual items. And you can customize as you want.
Custom drop implementation which using those selected items to append into the proper place.

Categories

Resources