この記事では、Javascript(jQuery UI)でtable(表)の行をドラッグ&ドロップで入れ替える方法をソースコード付きでまとめました。
jQuery UIでtableの行を入れ替え
JavScriptでは、jQuery UIプラグインの「Sortable」を利用することで、簡単にテーブル(表)の行をドラッグ&ドロップで入れ替えることができます。
ソースコード
サンプルプログラムのソースコードです。
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" />
<pre title="//code.jquery.com/jquery-1.10.2.js"></pre>
<pre title="//code.jquery.com/ui/1.11.4/jquery-ui.js"></pre>
<pre type="text/javapre" src="main.js"></pre>
</head>
<body>
<table>
<thead>
<tr>
<th>順位</th>
<th>名前</th>
</tr>
</thead>
<tbody id="sort">
<tr>
<th>1</th>
<td>マーリン</td>
</tr>
<tr>
<th>2</th>
<td>諸葛孔明</td>
</tr>
<tr>
<th>3</th>
<td>ジャンヌ・ダルク</td>
</tr>
</tbody>
</table>
</body>
</html>
jQuery UIプラグインの「Sortable」を利用するには、tbodyが必要です。
JavaScript
$(function() {
$('#sort').sortable();
});
| – | 関連記事 |
|---|---|
| 1 | Javascript入門 基本文法 |


コメント