How to save the results of a "drag-and-drop" in table?

Asked

Viewed 1,726 times

0

I need to do a drag-and-drop that looks like the image below, which is the system I’m developing.

In the buttons referring to "1st Period", "2nd Period" and others, I put a function that returns the disciplines referring to the period informed.

Example of table http://associacaoeua.com.br/particular/img/horario.png code =

<td><type="button" onClick="disc('1');">1º Periodo</button></td>

I activated the drag on the return of the disciplines, so I can drag you to the desired day and time with:

draggable="true" ondragover="allowDrop(event)"  ondragstart="drag(event)">

when I release the discipline in a particular location I can get the code (ID) from it follows code I used:

function drag(ev) {
    revert:true,
            ev.dataTransfer.setData("Text", ev.target.id);

}

function drop(ev) {
    ev.preventDefault();
    var data = ev.dataTransfer.getData("Text");
    ev.target.appendChild(document.getElementById(data));
    //o id da disciplina esta na variavel data.
    alert(data);
   //data é o ID     
}

I can save in the bank using Ajax, what I can’t do is take the day and time of class. I’m just getting the ID of the class.

Once discipline is unleashed I must have in

function drop(ev) {

    *var id  : ja tenho.* 
    var dia : ex.. SEGUNDA.
    var hora: ex.. 6:10 a 7:40

}

How should I do it?

  • 3

    What is your question? Please edit your question by clarifying what your specific problem is (is drag and drop? is to send the data to the server? is to save in the BD?), what have you done so far (with excerpts of preferred code), and what is causing you difficulty.

  • @user4870 I gave a general edited, why was not giving to understand. If it is wrong, just edit there...

  • Do you want to move the cells or the table? Move to where?

  • 2

    Dei -1, your question is very vague and full of questions. It is not clear.

  • I think this question could be divided into 2 questions, one asking how to create a drag-n-drop in HTML, making use of javascript and the other of how to save data from an HTML component sorting in the DOM, in a database.

  • This Html5rocks tutorial can help you http://www.html5rocks.com/pt/tutorials/dnd/basics/

  • That’s much better! I voted to reopen, @Luizpicolo, take a look too.

  • Now the question is clearer, topic re-opened.

  • Now you have more content. I withdraw my -1

Show 4 more comments

1 answer

1

jQuery’s embedding allows two parameters in the drop function, the second is the dragged element, so you can try:

function drop(ev, ui) {
    var dropped = ui.draggable;
    var texto = dropped.innerText; // dá-lhe o texto do elemento arrastado
    var coluna = $(dropped).index(); // dá-lhe o numero da coluna a começar em zero
    var linha = $(dropped).closest('tr').index(); // dá-lhe o numero da linhaa começar em zero

And so, having the index of the row/column can fetch your text() with

var textoColuna = $('table tr:first td:eq(' + coluna + ')').text();
var textoLinha = $('table tr:eq(' + linha + ') td:first').text();

In fact, within the function drop() the this refers to the destination element, so it can be used:

var coluna = $(this).index(); 
var linha = $(this).closest('tr').index();

Example

  • 1

    +1 was the same thing I thought, now just take the textContent of the elements represented by the column and row, and it will get the desired information.

  • Ola Gabriel thank you for knowing.

  • @Gabrielgartz Gabriel thank you for your knowledge. This way that you did is a little different from my right, I’m using the function of Html5, this is with jquery | droppable |? the one you sent solved the problem. Do you know if you can solve the problem with the above code? example I put above. Put var Dropped = ui.draggable; var text = Dropped.innerText; var column = $(Dropped). index(); var line = $(Dropped). Closest('tr'). index(); <br /> within my example. Or I am totally wrong

  • @user4870, can you put your code in the example I gave? So it’s easier to help. Change my code, press "update" and put the link here.

  • @Sergio fought for your help this being quite useful.

  • @Sergio my code is above. I can send my system compressed in your email?

  • @Kleber, the best thing is to put it here, in a jsfiddle, so everyone can help.

  • @Sergio thanks so much for your help.

  • @Sergio the problem was solved with a great library called Redips_drag follows the link with the download and examples for those who need. http://www.redips.net/javascript/drag-and-drop-table-content/

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.