td要素の中にあるdiv要素をドラッグで別のtd要素の中に移動。div要素にはinputタグでデータを持たせクリック時に取得。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>test</title> <style> *{ margin: 0; padding: 0; } td{ border: solid 1px #ccc; width:300px; height:300px; } .container{ background-color: black; padding: 2px; height: 40px; width: 290px; display: flex; flex-wrap: wrap; } .moveArea{ width: 10%; background-color: red; } .textArea{ width: 89%; background-color: green; } .dataArea{ width: 1%; background-color: blue; } .drag { cursor: move; position: absolute; z-index: 1000; } </style> </head> <body> <table> <tr><td></td><td></td><tr> <tr><td></td><td id="start"></td><tr> </table> <input type="textbox" id="target_id"> <input type="textbox" id="target_age"> <script> window.onload = startup; function startup(){ setup_rect({"caption":"A部品","id":"1","age":"2"}); setup_rect({"caption":"B部品","id":"2","age":"3"}); } function setup_rect(value_list){ let containerElement = document.createElement("div"); containerElement.classList.add("container"); let moveAreaElement = document.createElement("div"); moveAreaElement.addEventListener('mousedown', mouse_down); moveAreaElement.innerHTML = ""; moveAreaElement.classList.add("moveArea"); containerElement.appendChild(moveAreaElement); let textAreaElement = document.createElement("div"); textAreaElement.addEventListener('click', text_click); textAreaElement.innerHTML = value_list["caption"]; textAreaElement.classList.add("textArea"); containerElement.appendChild(textAreaElement); let dataAreaElement = document.createElement("div"); dataAreaElement.innerHTML = "<input type='hidden' name='id' value='" + value_list["id"] + "'>" + "<input type='hidden' name='age' value='" + value_list["age"] + "'>"; dataAreaElement.classList.add("dataArea"); containerElement.appendChild(dataAreaElement); let target = document.querySelector("#start"); target.appendChild(containerElement); } function text_click(event){ let target_id = document.getElementById("target_id"); let target_age = document.getElementById("target_age"); for(let el of this.nextElementSibling.children){ if (el.getAttribute("name") === "id") target_id.setAttribute("value", el.getAttribute("value")); if (el.getAttribute("name") === "age") target_age.setAttribute("value", el.getAttribute("value")); } } function mouse_move(event){ let target = document.getElementsByClassName("drag")[0]; let x = event.pageX - spaceX; let y = event.pageY - spaceY; target.style.top = y + "px"; target.style.left = x + "px"; } let spaceX = 0; let spaceY = 0; function mouse_down(event){ spaceX = event.offsetX; spaceY = event.offsetY; let target = this.parentNode; target.classList.add("drag"); mouse_move(event); document.addEventListener('mousemove', mouse_move); target.addEventListener('mouseup', mouse_up); } function mouse_up(event){ spaceX = 0; spaceY = 0; this.classList.remove("drag"); document.removeEventListener("mousemove", mouse_move); this.removeEventListener('mouseup', mouse_up); let x = event.clientX; let y = event.clientY; let el = document.elementFromPoint(x,y); if (el.tagName === "TD") el.appendChild(this); } </script> </body> </html> |