Simpan kedudukan DIV boleh seret dalam jadual HTML dan simpan ke pangkalan data MySQL
P粉909476457
P粉909476457 2024-02-03 18:37:10
0
1
343

Saya tertanya-tanya sama ada/bagaimana saya boleh menyimpan kedudukan yang saya alihkan div boleh seret ke atas meja, jadi apabila halaman dimuat semula ia akan kembali ke tempat ia berhenti (dari pangkalan data MySQL).

Saya telah membaca beberapa artikel berkaitan, tetapi semuanya tentang menggunakan jquery (acara, ui) dengan AJAX.

Ini kod saya:

   var p = document.getElementsByTagName('p');
   var choice = document.getElementsByClassName('choice');
   var dragItem = null;
   
   for(var i of p){
        i.addEventListener('dragstart', dragStart);
        i.addEventListener('dragend', dragEnd);
   }
   function dragStart(){
        dragItem = this;
        setTimeout(()=>this.style.display = "none", 0);
   }
   function dragEnd(){
        setTimeout(()=>this.style.display = "block", 0);
        dragItem = null;
   }
   for(j of choice){
        j.addEventListener('dragover', dragOver);
        j.addEventListener('dragenter', dragEnter);
        j.addEventListener('dragleave', dragLeave);
        j.addEventListener('drop', Drop);
   }
   function Drop(){
        this.append(dragItem);
   }
   function dragOver(e){
        e.preventDefault();
        this.style.border = "2px dotted cyan";
   }
   function dragEnter(e){
        e.preventDefault();
   }
   function dragLeave(e){
        this.style.border = "none";
   }
   section{
      width: 1000px;
      height: 360px;
      margin: 100px auto;
      display: flex;
      justify-content: space-around;
   }
   h1{
      text-align: center;
   }
   div{
      width: 200px;
      height: 90%;
      padding: 20px;
      margin: 10px;
      background: #fafafa;
      box-sizing: border-box;
   }
   p{
      font-weight: bold;
      border-radius: 5px;
      padding: 5px;
      color: white;
      background: red;
   }
   table, th, td {
  border:1px solid black;
}
      button{
      width: 100px;
      height: 15px;
      padding: 15px;
      margin: 10px;
      background: gray;
      box-sizing: border-box;
   }
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>test drag & drop </title>
</head>
<body>
<h1> it's just a test for js </h1>
<section>
<div2>
 <table  style="width:100%">
  <tr>
    <th>time</th>
    <th>DIM</th>
    <th>LUN</th>
  </tr>
  <tr>
    <td>8:00 - 9:00</td>
    <td><div class="choice"></div> S1-1</td>
    <td><div class="choice"></div>S1-2</td>
  </tr>
  <tr>
    <td>9:00 - 10:00</td>
    <td><div class="choice"></div>S2-1</td>
    <td><div class="choice"></div>S2-2</td>
  </tr>
</table> 
<button>save</button>
</div2>
<div class="choice">
     <p draggable="true">MODULE 1</p>
     <p draggable="true">MODULE 2</p>
     <p draggable="true">MODULE 3</p>
     <p draggable="true">MODULE 4</p>
     <p draggable="true">MODULE 5</p>
</div>

</section>
</body> 
</html>

P粉909476457
P粉909476457

membalas semua(1)
P粉729436537

Ini mungkin jawapan yang terlalu rumit. Tetapi saya rasa cara terbaik ialah menyimpan module 对象的数组。每个 module 都有一个 position 属性,每次移动模块时都需要更新该属性。您还可以使用 data atribut html dalam JavaScript untuk menyimpan kedudukan elemen itu sendiri pada elemen (https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes ). Ini kod saya:

Javascript:

var dragItem = null;
let modules = loadDataFromServer();

//Initial UI
updateUI(modules);


function dragStart() {
    console.log("Start")
    dragItem = this;
    setTimeout(() => this.style.display = "none", 0);
}
function dragEnd() {
    console.log("End")
    setTimeout(() => this.style.display = "block", 0);
    dragItem = null;
}

function Drop() {
    console.log("Drop")
    console.log(dragItem)
    this.append(dragItem);
    moveModuleTo(modules.length - 1, modules[dragItem.dataset.position])
}
function dragOver(e) {
    e.preventDefault();
    this.style.border = "2px dotted cyan";
}
function dragEnter(e) {
    e.preventDefault();
}
function dragLeave(e) {
    this.style.border = "none";
}

//Added Code

function loadDataFromServer() {
    //return an array of modules
    return [
        {
            title: "Module 1",
            position: 0
        },
        {
            title: "Module 2",
            position: 1
        },
        {
            title: "Module 3",
            position: 2
        },
        {
            title: "Module 4",
            position: 3
        },
        {
            title: "Module 5",
            position: 4
        },
    ]
}

function moveModuleTo(position, module) {
    if (module.position  position; i--) {
            modules[i] = modules[i - 1]
            modules[i].position = i + 1;
        }
    }
    modules[position] = module
    modules[position].position = position
    console.log(modules)
    //Update UI and database
    updateUI(modules)
}

function updateUI(modules) {
    let choice = document.getElementsByClassName('choice');

    //Remove old Modules
    const draggableContainer = document.getElementById("draggableContainer")
    while (draggableContainer.firstChild) {
        draggableContainer.removeChild(draggableContainer.lastChild)
    }

    //Add updated Modules
    modules.forEach(item => {
        console.log("yeet")
        let newDraggableItem = document.createElement('p')
        newDraggableItem.innerHTML = `${item.title}`
        newDraggableItem.draggable = true
        newDraggableItem.dataset.position = item.position
        draggableContainer.appendChild(newDraggableItem);
    });

    //ReAdd Event listeners

    var p = document.getElementsByTagName('p');

    for (var i of p) {
        i.addEventListener('dragstart', dragStart);
        i.addEventListener('dragend', dragEnd);
    }

    for (j of choice) {
        j.addEventListener('dragover', dragOver);
        j.addEventListener('dragenter', dragEnter);
        j.addEventListener('dragleave', dragLeave);
        j.addEventListener('drop', Drop);
    }
}

HTML:



    
    
    
    test drag & drop 


    

it's just a test for js

time DIM LUN
8:00 - 9:00
S1-1
S1-2
9:00 - 10:00
S2-1
S2-2
sssccc

Untuk mengemas kini pangkalan data, anda boleh mengisih tatasusunan mengikut nilai write 数据库调用。然后每次加载页面时,只需根据 position setiap kali anda mengalihkan modul. Harap ini membantu!

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan