{"id":3309,"date":"2021-03-12T15:52:31","date_gmt":"2021-03-12T06:52:31","guid":{"rendered":"https:\/\/taitan916.info\/blog\/?p=3309"},"modified":"2021-03-12T15:53:58","modified_gmt":"2021-03-12T06:53:58","slug":"post-3309","status":"publish","type":"post","link":"https:\/\/taitan916.info\/blog\/archives\/3309","title":{"rendered":"SortableJS\u3092\u4f7f\u3063\u3066\u30c7\u30fc\u30bf\u306e\u4e26\u3079\u66ff\u3048&#038;\u4fdd\u5b58"},"content":{"rendered":"<p>\u3042\u308b\u30b7\u30b9\u30c6\u30e0\u3092\u62dd\u898b\u3057\u305f\u969b\u306btable\u30bf\u30b0\u5185\u306b\u4e26\u3093\u3067\u3044\u308b\u9805\u76ee\u3092\u30c9\u30e9\u30c3\u30b0&amp;\u30c9\u30ed\u30c3\u30d7\u3067\u4e26\u3079\u66ff\u3048\u3059\u308b\u6a5f\u80fd\u304c\u3042\u3063\u305f\u3002\u30d6\u30e9\u30a6\u30b6\u304b\u3089\u30bd\u30fc\u30b9\u3092\u78ba\u8a8d\u3059\u308b\u3068jQuery UI\u306eSortable\u6a5f\u80fd\u3092\u4f7f\u3063\u3066\u3044\u308b\u3063\u307d\u3044\u3002\u305f\u3060\u3001jQuery UI\u3092\u5229\u7528\u3057\u306a\u304f\u3066\u3082Sortable.js\u306f\u500b\u5225\u3067\u5229\u7528\u3067\u304d\u308b\u3088\u3046\u306a\u306e\u3067\u4ee5\u4e0b\u306b\u4f7f\u3044\u65b9\u306e\u30e1\u30e2\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u3084\u308a\u305f\u3044\u3053\u3068<\/h2>\n<p>table\u30bf\u30b0\u5185\u306e\u9805\u76ee\u3092\u30c9\u30e9\u30c3\u30b0&amp;\u30c9\u30ed\u30c3\u30d7\u3067\u5165\u308c\u66ff\u3048\u3066\u300c\u5909\u66f4\u300d\u7b49\u306e\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3059\u308b\u3068\u4e26\u3073\u9806\u304cMySQL\u306b\u4fdd\u5b58\u3055\u308c\u3001\u4fdd\u5b58\u3055\u308c\u305f\u4e26\u3073\u9806\u3067\u8868\u793a\u3055\u308c\u308b\u6a5f\u80fd\u3092\u5b9f\u88c5\u3057\u305f\u3044\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u30b5\u30f3\u30d7\u30eb<\/h2>\n<p><a href=\"https:\/\/taitan916.info\/sample\/sortable\/\" target=\"_blank\" rel=\"noopener\">https:\/\/taitan916.info\/sample\/sortable\/<\/a><\/p>\n<p>&nbsp;<\/p>\n<h2>Sortable.js<\/h2>\n<p>\u4ee5\u4e0b\u304b\u3089\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3059\u308b\u3002<\/p>\n<p><a href=\"https:\/\/github.com\/SortableJS\/Sortable\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/SortableJS\/Sortable<\/a><\/p>\n<p>\u3082\u3057\u304f\u306fCDN\u3067\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u547c\u3073\u51fa\u3057\u3066\u3082\u826f\u3044\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"default\" data-enlighter-title=\"\">&lt;script src=&quot;https:\/\/cdn.jsdelivr.net\/npm\/sortablejs@latest\/Sortable.min.js&quot;&gt;&lt;\/script&gt;<\/pre>\n<p>&nbsp;<\/p>\n<h2>\u30bd\u30fc\u30b9<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"default\" data-enlighter-title=\"\">&lt;?php\r\n\/\/MySQL\u63a5\u7d9a\r\n$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);\r\n$mysqli-&gt;set_charset(&#039;utf8&#039;);\r\n\r\n\/\/\u4e26\u3073\u9806\u5909\u66f4\u51e6\u7406\r\nif( $_POST[&#039;order&#039;] ){\r\n\t$data = json_decode($_POST[&#039;order&#039;], true);\r\n\r\n\tforeach( $data as $key =&gt; $val ){\r\n\t\t$sql = &#039;\r\n\t\t\tUPDATE sort_table  \r\n\t\t\tSET `order` = &quot;&#039; . $mysqli-&gt;real_escape_string($val) . &#039;&quot;\r\n\t\t\tWHERE id = &quot;&#039; . $mysqli-&gt;real_escape_string($key) . &#039;&quot;\r\n\t\t&#039;;\r\n\t\t$row = $mysqli-&gt;query($sql);\r\n\t}\r\n\r\n\t\/\/\u30da\u30fc\u30b8\u30ea\u30ed\u30fc\u30c9\u9632\u6b62\u7528\r\n\theader(&#039;Location:.\/&#039;);\r\n\texit();\r\n}\r\n\r\n$sql = &#039;\r\n\tSELECT * \r\n\tFROM sort_table \r\n\tORDER BY `order` ASC\r\n&#039;;\r\n$row = $mysqli-&gt;query($sql);\r\nwhile ($rs = $row-&gt;fetch_array(MYSQLI_ASSOC)) {\r\n\t$data[$rs[&#039;id&#039;]] = $rs;\r\n}\r\n?&gt;\r\n&lt;html lang=&quot;ja&quot;&gt;\r\n&lt;head&gt;\r\n&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text\/html; charset=UTF-8&quot;&gt;\r\n&lt;title&gt;Sortable \u30b5\u30f3\u30d7\u30eb&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;link href=&quot;\/\/stackpath.bootstrapcdn.com\/bootstrap\/4.3.1\/css\/bootstrap.min.css&quot; rel=&quot;stylesheet&quot;&gt;\r\n&lt;script src=&quot;\/\/netdna.bootstrapcdn.com\/twitter-bootstrap\/2.3.2\/js\/bootstrap.min.js&quot;&gt;&lt;\/script&gt;\r\n&lt;script src=&quot;\/\/code.jquery.com\/jquery-3.3.1.slim.min.js&quot;&gt;&lt;\/script&gt;\r\n&lt;script src=&quot;\/\/cdnjs.cloudflare.com\/ajax\/libs\/popper.js\/1.14.7\/umd\/popper.min.js&quot;&gt;&lt;\/script&gt;\r\n&lt;script src=&quot;\/\/stackpath.bootstrapcdn.com\/bootstrap\/4.3.1\/js\/bootstrap.min.js&quot;&gt;&lt;\/script&gt;\r\n&lt;script src=&quot;https:\/\/cdn.jsdelivr.net\/npm\/sortablejs@latest\/Sortable.min.js&quot;&gt;&lt;\/script&gt;\r\n&lt;script type=&quot;text\/javascript&quot; src=&quot;\/\/code.jquery.com\/jquery-3.5.1.js&quot;&gt;&lt;\/script&gt;\r\n&lt;style&gt;\r\ntd{\r\n\tcursor: move;\r\n}\r\n&lt;\/style&gt;\r\n&lt;form action=&quot;.\/&quot; method=&quot;post&quot; id=&quot;order_form&quot;&gt;\r\n\t&lt;table&gt;\r\n\t\t&lt;tbody id=&quot;sort-table&quot;&gt;\r\n\t\t\t&lt;?php foreach( $data as $key =&gt; $val ){ ?&gt;\r\n\t\t\t\t&lt;tr class=&quot;id_&lt;?php echo $val[&#039;id&#039;];?&gt;&quot;&gt;\r\n\t\t\t\t\t&lt;td&gt;&lt;?php echo $val[&#039;order&#039;];?&gt;&lt;\/td&gt;\r\n\t\t\t\t\t&lt;td&gt;&lt;?php echo $val[&#039;name&#039;];?&gt;&lt;\/td&gt;\r\n\t\t\t\t&lt;\/tr&gt;\r\n\t\t\t&lt;?php } ?&gt;\r\n\t\t&lt;\/tbody&gt;\r\n\t&lt;\/table&gt;\r\n\t&lt;input type=&quot;hidden&quot; name=&quot;order&quot; class=&quot;order&quot; value=&quot;&quot;&gt;\r\n\t&lt;input type=&quot;submit&quot; class=&quot;btn btn-success&quot; value=&quot;\u9806\u756a\u3092\u5909\u66f4&quot; onClick=&quot;return sortCheck();&quot;&gt;\r\n&lt;\/form&gt;\r\n&lt;script&gt;\r\n\t\/\/Sortable\r\n\tlet table = document.getElementById(&#039;sort-table&#039;);\r\n\tSortable.create(table);\r\n\r\n\t\/\/\u4e26\u3073\u9806\u306e\u51e6\u7406\r\n\tfunction sortCheck(){\r\n\t\tlet data = $(&#039;#sort-table &gt; tr&#039;);\r\n\t\tlet query = new Object();\r\n\r\n\t\tfor( let i = 0; i &lt; data.length; i++ ){\r\n\t\t\t\/\/\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306bkey = id \/ val = \u4e26\u3073\u9806\u3092\u683c\u7d0d\r\n\t\t\tquery[data[i].attributes.class.value.replace(&#039;id_&#039;, &#039;&#039;)] = i + 1;\r\n\t\t}\r\n\r\n\t\t\/\/\u30c7\u30fc\u30bf\u3092JSON\u5316\u3057\u3066input=&quot;hidden&quot;\u306b\u683c\u7d0d\r\n\t\t$(&#039;.order&#039;).val(JSON.stringify(query));\r\n\r\n\t\t\/\/\u30d5\u30a9\u30fc\u30e0\u9001\u4fe1\r\n\t\t$(&#039;#order_form&#039;).submit();\r\n\r\n\t\t\/\/IE\u306e\u4e8c\u91cd\u9001\u4fe1\u9632\u6b62\u7528\r\n\t\treturn false;\r\n\t}\r\n&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>query[data[i].attributes.class.value.replace('id_', '')]\u306e\u3068\u3053\u308d\u304c\u3061\u3087\u3063\u3068\u6c17\u6301\u3061\u60aa\u3044\u304c\u3001\u7dba\u9e97\u306b\u307e\u3068\u3081\u3089\u308c\u306a\u304b\u3063\u305f\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u6240\u611f<\/h2>\n<p>\u4eca\u307e\u3067\u4e26\u3073\u9806\u5909\u66f4\u6a5f\u80fd\u3092\u5b9f\u88c5\u3059\u308b\u5834\u5408\u3001\u30c6\u30ad\u30b9\u30c8\u30dc\u30c3\u30af\u30b9(input type=\"text\")\u306b\u6570\u5024\u3092\u5165\u529b\u3059\u308b\u5f62\u3067\u5b9f\u88c5\u3057\u3066\u3044\u305f\u3002\u305d\u3046\u3059\u308b\u3068\u540c\u3058\u6570\u5024\u3092\u5165\u529b\u3057\u3066\u3044\u305f\u5834\u5408\u3084\u3001\u6570\u5024\u304c\u5165\u3063\u3066\u3044\u306a\u3044\u5834\u5408\u306f\u3069\u3046\u3059\u308b\u304b\u3068\u304b\u8003\u3048\u308b\u5fc5\u8981\u304c\u3042\u3063\u305f\u304c\u3001Sortable.js\u3092\u4f7f\u3046\u3053\u3068\u3067\u305d\u306e\u8fba\u308a\u306e\u4f5c\u696d\u306f\u5fc5\u8981\u306a\u304f\u306a\u308a\u305d\u3046\u3002<\/p>\n<p>\u3068\u306f\u3044\u3048\u30da\u30fc\u30b8\u30f3\u30b0\u3042\u308a\u306e\u30da\u30fc\u30b8\u3060\u3068\u30c6\u30ad\u30b9\u30c8\u30dc\u30c3\u30af\u30b9\u5f62\u5f0f\u3092\u4f7f\u3046\u3057\u304b\u306a\u3055\u305d\u3046\u3002\u3042\u3068\u3001Sortable.js\u306e\u65b9\u304cUI\u7684\u306b\u89aa\u5207\u306b\u601d\u3048\u308b\u304c\u3001\u3053\u3061\u3089\u5074\u3068\u3057\u3066\u306fJS\u3092\u6ca2\u5c71\u4f7f\u3046\u3068\u30d6\u30e9\u30a6\u30b6\u306b\u3088\u3063\u3066(\u7279\u306bIE\u306f)\u6319\u52d5\u304c\u7570\u306a\u3063\u305f\u308a\u3059\u308b\u306e\u3067\u3001\u30d6\u30e9\u30a6\u30b6\u6bce\u306e\u30c1\u30a7\u30c3\u30af\u3084\u4e0d\u5177\u5408\u304c\u767a\u751f\u3057\u305f\u969b\u306e\u4fee\u6b63\u306e\u624b\u9593\u3092\u8003\u3048\u308b\u3068\u3001\u3069\u3061\u3089\u306e\u5f62\u5f0f\u3067\u3044\u304f\u306e\u304b\u60a9\u307e\u3057\u3044\u3068\u3053\u308d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3042\u308b\u30b7\u30b9\u30c6\u30e0\u3092\u62dd\u898b\u3057\u305f\u969b\u306btable\u30bf\u30b0\u5185\u306b\u4e26\u3093\u3067\u3044\u308b\u9805\u76ee\u3092\u30c9\u30e9\u30c3\u30b0&amp;\u30c9 ... <\/p>\n","protected":false},"author":1,"featured_media":1067,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2,3,5,4],"tags":[],"class_list":["post-3309","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","category-jquery","category-mysql","category-javascript"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts\/3309","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/comments?post=3309"}],"version-history":[{"count":0,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/posts\/3309\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/media\/1067"}],"wp:attachment":[{"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/media?parent=3309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/categories?post=3309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/taitan916.info\/blog\/wp-json\/wp\/v2\/tags?post=3309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}