2

Here's the Fiddle

I have a table with draggable rows that are multi-selectable, but I want to drag them en masse to another table and drop them there - not to be appended as additional elements to the other table, but to do some stuff with the information, i.e. form submission.

My example is originally based on another demo I found here: multi drag demo

Here is the HTML code for the basic example of this problem.

<table class="DraggableThings">
  <tr draggable='true'><td >Row 1</td></tr>
  <tr draggable='true'><td >Row 2</td></tr>
  <tr draggable='true'><td >Row 3 </td></tr>
</table>

<table id='menu_table'>
  <tbody>
    <tr>
        <td class='droppableItem' >accomplished</td>
    </tr>
  </tbody>
</table>

Here is the JQuery code

$('.droppableItem')
   .bind('dragenter dragover', false)
   .bind('drop', function(event){
       accomplished($(this),event);
});
$('.DraggableThings tr').bind('click', function () {
   $(this).toggleClass("selected");
});
$('.DraggableThings tr').bind('dragstart', function(event){
  var mytext = event.target.innerText;
  event.originalEvent.dataTransfer.setData('txt', mytext );
});
$('.DraggableThings tr').drag("init", function () {
    return $('.selected');
  }).drag(function (ev, dd) {
    $(this).css({
        top: dd.offsetY,
        left: dd.offsetX
    });
});


function accomplished(thing,event) {
  event.dataTransfer = event.originalEvent.dataTransfer;
  var data = event.dataTransfer.getData('txt');
  log(data + " made it to accomplishments");
}

css

.selected {
  background-color: #ECB;
}

Here's the Fiddle

Hope someone knows the answer Thank you

slashdottir
  • 7,835
  • 7
  • 55
  • 71
  • So let's go with what you were saying - we grab some of the rows and throw them into the drop area. They're not appended (so it's as if they never moved), but we can get the objects (by id, class, etc.) that were moved there and use that information. What information do you need from those moved objects and what role does the form play? – WindsofTime Feb 16 '14 at 23:38
  • Well, the project is a 'todo' list type thing and it would be efficient to submit multiple accomplishments at once to the server rather than one at a time which is too slow. The UI is already crazy complex with lots of draggability. You can currently drag individual items to various icons which have different features such as "delete" or "postpone" or "accomplished". Once you do that, it posts back to the server the info and updates your personal calendar and stores the information in your drive folder. etc. – slashdottir Feb 16 '14 at 23:45
  • So the portion of what to do once you know where it was dropped is covered (or you get what you need to do) and the issue at hand is figuring out how to reach that point of being able to drop multiple items into one of the areas and get a message like "1, 2, and 3 accomplished." – WindsofTime Feb 17 '14 at 00:36
  • precisely! thank you – slashdottir Feb 17 '14 at 01:52
  • Working on it right now. I just have to fix up how the message is printed. – WindsofTime Feb 17 '14 at 02:48

1 Answers1

3

Here's a JSFiddle demonstration, but I suggest fixing up the table rows because dragging multiple rows sometimes confuses one table row for another because they're not spaced out that much, so it can't tell to which table row you're trying to place the elements. I made them bigger for testing purposes.

You could do something along the lines of clicking rather than dragging to guarantee you add it to the right category.

It's the same code as in your other question, except we switched it up to action(ui.helper.children(), event); so that we're passing the elements we selected (and their respective text) and the event, which returns the table row's inner text (i.e. accomplished, postponed, and deleted).

The rewritten action code is:

function action(a,b) {
    for(var i = 0; i < a.length; i++)
        log(a[i].innerText + " made it to " + b.target.innerText);
}

We post to the log in a loop, so that we get all the elements we selected separately. b.target.innerText gets the category (again, i.e. accomplished, postponed, and deleted). Not looping through it means we're getting the concatenation of all the elements (e.g. "Row1Row2").

WindsofTime
  • 611
  • 4
  • 11
  • Sweet, looks like it's basically working except for the text part. Amazing, thank you – slashdottir Feb 17 '14 at 04:50
  • Which text part? Or do you mean the size of the table rows being too small so that it may be intercepted by the wrong table row? – WindsofTime Feb 17 '14 at 05:39
  • when I run the fiddle and drag two rows at once to the accomplished area it says: "undefined made it to undefined undefined made it to undefined" – slashdottir Feb 17 '14 at 11:39
  • Really? The JSFiddle runs fine on my end: [Screenshot](http://prntscr.com/2tabxx) – WindsofTime Feb 17 '14 at 12:01
  • Okay I just tried it on Firefox and Chrome. It works on Chrome, but I get the undefined error on Firefox – slashdottir Feb 17 '14 at 12:17
  • It works on Firefox if I change the action method this way: function action(children,ev) { for(var i = 0; i < children.length; i++) { var childi = children[i]; var t = ev.target; var tv2 = $(t).text(); log($(childi).text() + " made it to " + tv2); } } – slashdottir Feb 17 '14 at 12:31
  • not gonna sweat ie9 as this is intended for android anyway – slashdottir Feb 17 '14 at 12:34
  • Glad to hear everything works out. Did you give the other browsers a try as well or is Chrome/Firefox all you really need? Well, if it's going to be developed for Android, it will be slightly different. – WindsofTime Feb 17 '14 at 13:26
  • Well, this is just a learning thing for me. Not too serious an app. Teaching myself html/jquery/css etc. I will just use this app for myself for a while and see how useful it. Thank you again! – slashdottir Feb 17 '14 at 13:43